[Haskell-cafe] Recursion in Haskell
P. R. Stanley
prstanley at ntlworld.com
Sun Feb 18 21:26:54 EST 2007
Brandon, Chris, Don,
gentlemen,
Thank you all for your swift and well-written answers.
I should point out that I'm coming to functional programming with a
strong background in programming in C and C-type languages. I am also
very new to the whole philosophy of functional programming. Hence my
bafflement at some of the very elementary attributes of Haskell. I
thought that would give you chaps a better idea of where I'm coming
from with my queries.
Back to mylen. Here is the definition once more:
mylen [] = 0
mylen (x:y) = 1 + mylen y
The base case, if that is the right terminology, stipulates that the
recursion ends with an empty list and returns 0. Simple though one
question - why does mylen require the parentheses even when it is
evaluating the length of [...]? I can understand the need for them
when dealing with x:... because of the list construction function
precedence but not with [2,3,etc]. I thought a [e] was treated as a
distinct token.
I'm assuming that the interpreter/compiler is equipped to determine
the type and value of xs subsequent to which it calls itself and
passes the object minus the first element as argument unless the
object is an empty list.
going back to Don's formal definition of the list data structure:
data [a] = [] | a : [a]
A list is either empty or contains an element of type a? Correct,
wrong or very wrong?
By the way, what branch of discrete math - other than the obvious
ones such as logic and set theory - does functional programming fall under?
Many thanks in advance for your help
Paul
More information about the Haskell-Cafe
mailing list