Haskell
Haskell is a functional language.
The structure is the same with imperative
languages, but there are some syntax differences.
Generally functions won’t modify anything in the
outside world.
By default, Haskell evaluates something only when
it is needed.
Give another possible calculation for the result of:
double (double 2).
Anything appearing after the $ operator will take
precedence over anything that comes before.
Explicit type declaration is not imperative
Show that sum’ [x] = x for any number x .
Recursive function:
sum’ [x]
-> apply sum’
x + (sum’ [ ])
-> apply sum’
x+0
-> apply + operator
x
Define a function product that produces the product of
a list of numbers, and show using your definition that
product [2, 3, 4] = 24.
Recursive function:
2 * product [3, 4]
2 * (3 * product [4])
2 * (3 * (4 * product []))
2 * (3 * (4 * (1)))
24
The script below contains three syntactic errors. Correct
these errors and then check that your script works
properly:
N = a ’div’ length xs
where
a = 10
xs = [1, 2, 3, 4, 5]
When defining a new function, the names of the
function and its arguments must begin with a lower-
case letter.
`div` integer division truncated
What are the types of the following functions?
second xs = head (tail xs)
swap (x , y) = (y, x )
pair x y = (x , y)
double x = x ∗ 2
palindrome xs = reverse xs == xs
twice f x = f (f x)
Num, Eq are classes.
cap4
Using library functions, define a function
halve :: [a] -> ([a],[a]) that splits an even-
lengthed list into two halves.
using guarded equations
splitAt - returns a tuple where first element is xs
prefix of length n and second element is the
remainder of the list
In a similar way to && show how the disjunction
operator || can be defined in four different ways using
pattern matching.
|| is Boolean “ or " (||) :: Bool -> Bool -> Bool
Using a list comprehension, give an expression that
calculates the sum 1^2 + 2^2 + ...+ 100^2 of the first
one hundred integer squares.
the symbol | is read as such that
<- is read as is drawn from
the expression x <- [1..100] is called a generator
A positive integer is perfect if it equals the sum of all of
its factors, excluding the number itself. Using a list
comprehension and the function factors, define a
function perfects :: Int -> [Int] that returns the list of all
perfect numbers up to a given limit.
using factors for factor listing
:type an ghci to list implicit or explicit type
Show how the library function replicate :: Int -> a
-> [a] that produces a list of identical elements can be
defined using a list comprehension. For example:
> replicate 3 True
[True,True,True]
replicate creates a list of length given by the first
argument and the items having value of the second
argument
_ is the wildcard pattern
replicate’ has two parameters
How does the recursive version of the factorial function
behave if applied to a negative argument, such as (-1)?
Modify the definition to prohibit negative arguments by
adding a guard to the recursive case.
the first equation is called a base case
the second equation is called a recursive case
Define a recursive function sumdown :: Int -> Int that
returns the sum of the non-negative integers from a
given value down to zero. For example, sumdown 3
should return the result
3+2+1+0 = 6.
sumdown 4
4 > 0 = 4 + sumdown 3
sumdown 3
3 > 0 = 3 + sumdown 2
sumdown 2
2 > 0 = 2 + sumdown 1
sumdown 1
1 > 0 = 1 + sumdown 0
sumdown 0
0>0 =0
Define a recursive function euclid :: Int -> Int -> Int that
implements Euclid’s algorithm for calculating the
greatest common divisor of two non-negative integers:
if the two numbers are equal, this number is the result;
otherwise, the smaller number is subtracted from the
larger, and the same process is then repeated.
6<27 euclid (21 6)
6<21 euclid (15 6)
6<15 euclid (9 6)
6<9 euclid (3 6)
6>3 euclid (3 3)
3=3 3