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

Recursive Functions in Haskell: Factorial and List Operations, Summaries of Cybercrime, Cybersecurity and Data Privacy

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

  • How is the factorial function defined recursively in Haskell?
  • What is the role of base cases in recursive functions?
  • How can recursion be used to define functions on lists in Haskell?
  • What is the difference between recursive and iterative functions?
  • What is the quicksort algorithm and how is it implemented recursively in Haskell?

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

marcyn
marcyn 🇬🇧

4.3

(12)

228 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
0
PROGRAMMING IN HASKELL
Chapter 6 -Recursive Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Recursive Functions in Haskell: Factorial and List Operations and more Summaries Cybercrime, Cybersecurity and Data Privacy in PDF only on Docsity!

PROGRAMMING IN HASKELL

Chapter 6 - Recursive Functions

Introduction

As we have seen, many functions can naturally be

defined in terms of other functions.

fac :: Int ® Int fac n = product [1..n]

fac maps any integer n to the product

of the integers between 1 and n.

Recursive Functions

In Haskell, functions can also be defined in terms of

themselves. Such functions are called recursive.

fac 0 = 1 fac n = n * fac (n-1)

fac maps 0 to 1, and any other

integer to the product of itself and

the factorial of its predecessor.

For example:

fac 3 3 * fac 2

3 * (2 * fac 1)

3 * (2 * (1 * fac 0))

Why is Recursion Useful?

❚ Some functions, such as factorial, are simpler to

define in terms of other functions.

❚ As we shall see, however, many functions can

naturally be defined in terms of themselves.

❚ Properties of functions defined using recursion

can be proved using the simple but powerful

mathematical technique of induction.

Recursion on Lists

Recursion is not restricted to numbers, but can also

be used to define functions on lists.

product :: Num a Þ [a] ® a product [] = 1 product (n:ns) = n * product ns

product maps the empty list to 1,

and any non-empty list to its head

multiplied by the product of its tail.

Using the same pattern of recursion as in product

we can define the length function on lists.

length :: [a] ® Int length [] = 0 length (_:xs) = 1 + length xs

length maps the empty list to 0,

and any non-empty list to the

successor of the length of its tail.

For example:

length [1,2,3] 1 + length [2,3]

1 + (1 + length [3])

1 + (1 + (1 + length []))

For example:

reverse [1,2,3] reverse [2,3] ++ [1]

(reverse [3] ++ [2]) ++ [1]

((reverse [] ++ [3]) ++ [2]) ++ [1]

(([] ++ [3]) ++ [2]) ++ [1]

[3,2,1]

Multiple Arguments

Functions with more than one argument can also

be defined using recursion. For example:

❚ Zipping the elements of two lists:

zip :: [a] ® [b] ® [(a,b)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys

Quicksort

The quicksort algorithm for sorting a list of values

can be specified by the following two rules:

❚ The empty list is already sorted;

❚ Non-empty lists can be sorted by sorting the

tail values £ the head, sorting the tail values >

the head, and then appending the resulting

lists on either side of the head value.

Using recursion, this specification can be translated

directly into an implementation:

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]

❚ This is probably the simplest implementation of

quicksort in any programming language!

Note:

Exercises

(1) Without looking at the standard prelude, define

the following library functions using recursion:

and :: [Bool] ® Bool

❚ Decide if all logical values in a list are true:

concat :: [[a]] ® [a]

❚ Concatenate a list of lists:

(!!) :: [a] ® Int ® a

❚ Select the nth element of a list:

elem :: Eq a Þ a ® [a] ® Bool

❚ Decide if a value is an element of a list:

replicate :: Int ® a ® [a]

❚ Produce a list with n identical elements: