































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
An introduction to recursive functions in Haskell, using examples of built-in functions such as maximum, replicate, take, reverse, zip, and elem. It also covers the quicksort algorithm for sorting lists recursively. Students will learn the concept of recursion and how it is used to define functions in Haskell.
What you will learn
Typology: Lecture notes
1 / 39
This page cannot be seen from the preview
Don't miss anything!
St´ephane Vialette
LIGM, Universit´e Gustave Eiffel
September 28, 2020
maximum' :: (Ord a) => [a] -> a maximum' [] = error "maximum of empty list" maximum' [x] = x maximum' (x : xs) | x > maxTail = x | otherwise = maxTail where maxTail = maximum' xs
maximum' [2,5,1] 5
replicate
replicate takes an Int and a value, and returns a list that has several repetitions of the same element.
replicate' :: (Num i, Ord i) => i -> a -> [a] replicate' n x | n <= 0 = [] | otherwise = x : replicate' (n-1) x
replicate' 0 5 [] replicate' 1 5 [5] replicate' 10 5 [5,5,5,5,5,5,5,5,5,5]
take
take returns a specified number of elements from a specified list.
take' :: (Num i, Ord i) => i -> [a] -> [a] take' _ [] = [] take' n (x : xs) = x : take' (n-1) xs
take' 0 ['a'..'z'] "" take' 1 ['a'..'z'] "a" take' 5 ['a'..'z'] "abcde" take' 5 [] []
reverse
reverse takes a list and return a list with the same elements, but in the reverse order.
reverse' :: [a] -> [a] reverse' [] = [] reverse' (x : xs) = reverse' xs ++ [x]
reverse' [] [] reverse' [1..10]
[10,9,8,7,6,5,4,3,2,1]
repeat
repeat takes an element and returns an infinite list composed of that element.
repeat' :: a -> [a] repeat' x = x : repeat' x
take 10 (repeat' 5) [5,5,5,5,5,5,5,5,5,5]
zip
zip takes two lists and zips them together.
zip [1,2,3] [2,3] [(1,2),(2,3)]
zip truncates the longer list to match the length of the shorter one.
How about if we zip something with an empty list? Well, we get an empty list back then.
zip' :: [a] -> [b] -> [(a,b)] zip' _ [] = [] zip' [] _ = [] zip' (x : xs) (y : ys) = (x,y) : zip' xs ys
elem
elem takes an element and a list and sees if that element is in the list.
The edge condition, as is most of the times with lists, is the empty list. We know that an empty list contains no elements, so it certainly doesn’t have the droids we’re looking for.
elem' :: (Eq a) => a -> [a] -> Bool elem' a [] = False elem' a (x : xs) = a == x || a elem'
xs
There are many approaches to recursively sorting lists.
The quicksort algorithm works like this: You select the first element (called the pivot), put all the other list elements that are less than or equal to the first element on its left side, and put all the other list elements that are greater than the first element to its right side.
Now we recursively sort all the elements that are on the left and right sides of the pivot by calling the same function on them.
quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x] biggerSorted = quicksort [a | a <- xs, a > x] in smallerSorted ++ [x] ++ biggerSorted
:t quicksort quicksort :: Ord a => [a] -> [a] quicksort [] [] quicksort [1] [1] quicksort [1,5,9,8,2,6,4,7,3] [1,2,3,4,5,6,7,8,9] quicksort "to be or not to be" " bbeenoooorttt" quicksort [(5,6),(1,2),(3,4)] [(1,2),(3,4),(5,6)]