Docsity

Understanding Recursive Functions in Haskell: Max, Replicate, Take, Reverse, Zip, Elem, Qu, Lecture notes of Cybercrime, Cybersecurity and Data Privacy

Partial preview of the text

Download Understanding Recursive Functions in Haskell: Max, Replicate, Take, Reverse, Zip, Elem, Qu and more Lecture notes Cybercrime, Cybersecurity and Data Privacy in PDF only on Docsity!

Haskell

Hello Recursion!

St´ephane Vialette

LIGM, Universit´e Gustave Eiffel

September 28, 2020

Hello Recursion!

Maximum Awesome

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 Awesome

maximum' [2,5,1] 5

A Few More Recursive Functions

A Few More Recursive Functions

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]

A Few More Recursive Functions

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 [] []

A Few More Recursive Functions

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]

A Few More Recursive Functions

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]

A Few More Recursive Functions

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

A Few More Recursive Functions

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

Quick, Sort!

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.

Quick, Sort!

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

Quick, Sort!

: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)]