Haskell
Haskell
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]
cap4
Using library functions, define a function
halve :: [a] -> ([a],[a]) that splits an even-
lengthed list into two halves.
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.