You can get the head and the tail by pattern matching.  Lets say you have a
function that takes a list:

myfunction list = -- do something here

go = myfuction [1,4,2,6]

... you can write the "list" bit of the function as (x:xs), where x is the
head, or first element, of the list, and xs is the tail, or the rest of the
list:

myfunction (x:xs) = -- do something here

then you can call myfunction on xs, and compare that to x, to give the
result.

This is recursive: the function calls itself over and over again, until at
some point it's going to execute "myfuction []" or "myfunction [6]", which
is easy to handle, for example by adding the definition:

myfunction [a] = -- the value of myfunction given a

Go here for a really great tutorial:

http://www.cs.nott.ac.uk/~gmh/book.html

Recursive functions are in slides, number 6, but the tutorials are really
great: just start from 1 and work your way through.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to