














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Recursive functions in Haskell through the example of the factorial function and its extension to list operations such as product, length, reverse, and zipping. Recursive functions are useful for defining functions that can be defined in terms of themselves, and properties of these functions can be proved using mathematical induction.
What you will learn
Typology: Summaries
1 / 22
This page cannot be seen from the preview
Don't miss anything!
fac :: Int ® Int fac n = product [1..n]
fac 0 = 1 fac n = n * fac (n-1)
fac 3 3 * fac 2
3 * (2 * fac 1)
3 * (2 * (1 * fac 0))
product :: Num a Þ [a] ® a product [] = 1 product (n:ns) = n * product ns
length :: [a] ® Int length [] = 0 length (_:xs) = 1 + length xs
length [1,2,3] 1 + length [2,3]
1 + (1 + length [3])
1 + (1 + (1 + length []))
reverse [1,2,3] reverse [2,3] ++ [1]
(reverse [3] ++ [2]) ++ [1]
((reverse [] ++ [3]) ++ [2]) ++ [1]
zip :: [a] ® [b] ® [(a,b)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys
qsort :: Ord a Þ [a] ® [a] qsort [] = [] qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger where smaller = [a | a ¬ xs, a £ x] larger = [b | b ¬ xs, b > x]
and :: [Bool] ® Bool
concat :: [[a]] ® [a]
(!!) :: [a] ® Int ® a
elem :: Eq a Þ a ® [a] ® Bool
replicate :: Int ® a ® [a]