[Haskell-beginners] Re: question about styles of recursion

Michael Mossey mpm at alumni.caltech.edu
Thu Mar 26 18:23:01 EDT 2009

7stud wrote:
> Michael Mossey <mpm <at> alumni.caltech.edu> writes:
>> In RWH, in the exercises at the end of the book,
> There are no exercises at the end of the book.

Thanks for the help everyone. I wrote this post in the middle of the 
night when I had some insomnia, and I had just taken a sleeping 
medication, so I was basically "drunk." It's like trying to program 
drunk. My apologies for screwing up so many aspects of the post, but the 
gist of my question was answered, I think.

Before I go further, let me ask again: can someone show me how to put 
the "School of Expression" code "on the library path" so I don't have to 
put it in the same directory where I'm working? I'm on Windows. I've 
tried the -i option in ghci and ghc, but ghci and ghc don't see the SOE 
code. I tried many ways of specifying the directory to -i: with quotes, 
without quotes, relative path, absolute path. ghc happily accepts every 
form I give it! But then fails to find SOE. I would like either to get 
the -i form working, or even better have ghc read an environment 
variable so it happens automatically every time it starts.

But back to the gist of my question last night: I am aware that most 
examples of recursion presented in the books so far do their processing 
"as the recursion unwinds." In other words:

length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs

This function goes deeper and deeper into the recursion before doing any 
calculation, then does all the sums "on the way back out."

Being an imperative programmer, I keep trying to write loops that 
accumulate "on the way down", like this:

length' :: Int -> [a] -> Int
length' s [] = s
length' s (x:xs) = length' (s+1) xs

length :: [a] -> Int
length xs = length' 0 xs

I suppose both forms are valid, but the first form seems to be most 
natural in most situations I've encountered in the exercises. I'm 
working with "Real World Haskell", "Haskell School of Expression," and 
"Yet Another Haskell Tutorial." My strategy is to work each book's early 
chapters before going further in any of the books, so I get multiple 
"takes" on the material.


More information about the Beginners mailing list