a. Using list comprehensions, define a function, countNeg
, for counting the number of negative numbers in a list of numbers.
countNeg :: [Int] -> Int
>countNeg [1, -2, 3, -5] = 2
b. Define raise
:
raise :: Int -> Int -> Int
>raise 2 4= 16
c. Pascal's triangle is a triangle of numbers
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
computed as follows: (1) The first row just contains a 1.
(2) The following rows are computed by adding together adjacent numbers in the row above, and adding a 1 at the beginning and at the end.
Write a function, pascal
, using list comprehension and ++, which maps a positive integer n to the nth row of Pascal numbers.
For example, >pascal 5 = [1, 4, 6, 4, 1]
.
Hint: define an auxiliary function pairs
which construct pairs from two consecutive integers in a list.