Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

  • How does the take function work recursively in Haskell?
  • What is recursion and how is it used in Haskell?
  • What is the difference between replicate and replicate' functions in Haskell?
  • How does the maximum function work recursively in Haskell?
  • What is the purpose of the reverse function in Haskell and how does it work recursively?

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

millionyoung
millionyoung 🇬🇧

4.5

(25)

242 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Haskell
Hello Recursion!
St´ephane Vialette
LIGM, Universit´e Gustave Eiffel
September 28, 2020
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

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