Contents | Up | Prev | Next |

-- Type declaration : -- [a] - a linked list of any type -- x -> y - function that accepts x and returns y mylen :: [a] -> Integer -- Declare the length of the empty list to be 0 mylen [] = 0 -- x:xs == the list whose first item (the head) is x and the rest (the tail) -- is xs. mylen (x :xs) = 1 + (mylen xs) |

fib :: Integer -> Integer fib 0 = 0 fib 1 = 1 fib a = (fib (a - 1)) + (fib (a - 2)) |

-- (a,b) is a tuple whose first element is a and its second is b fibo_helper :: Integer -> (Integer,Integer) fibo_helper 0 = ( 0, 1) -- Notice the use of the closure (where) to assign two temporary values fibo_helper n = (b,a +b) where (a,b) = fibo_helper (n - 1) -- (fst (a,b)) = a fibo n = (fst (fibo_helper n)) |

Contents | Up | Prev | Next |

Written by Shlomi Fish