Haskell 考试复习题——Tpye,function define

1.What is the most general type of the function

tail
[a] -> [a]

take
Int ->[a] ->[a]

take 3 [True,True,True,True]
[Bool]

take 3
[a]->[a]

(False, ’a’)
(Bool,‘char’)

[(False,0),(True,1)]
[(Bool,char)]

([’a’, ’b’], [False, True])
( [char],[True,True])

twice f x = f (f x)
(a->b)-> a->b

add x y = x * y
Num a =>a->a->a

evens = filter even
Integral a => [a] -> [a] – [Int] -> [Int]

foldr (+) 1 [2,3,4]
10
详解:
foldr (+) 1 [2,3,4]
= 2 + (3 + (4 + 1)) – 从右向左结合
= 2 + (3 + 5)
= 2 + 8
= 10

foldr () 1 [2,3,4]
24
详解:
foldr (
) 1 [2,3,4]
= 2 * (3 * (4 * 1)) – 从右向左结合
= 2 * (3 * 4)
= 2 * 12
= 24

foldr (-) 1 [2,3,4]
2-(3-(4-1) = 2
详解:
foldr (-) 1 [2,3,4]
= 2 - (3 - (4 - 1)) – 从右向左结合
= 2 - (3 - 3)
= 2 - 0
= 2

foldl (-) 1 [2,3,4]
((1 - 2) - 3) - 4 = -8
详解:
foldl (-) 1 [2,3,4]
= ((1 - 2) - 3) - 4 – 从左向右结合
= (-1 - 3) - 4
= -4 - 4
= -8

sum [x | x <- [1…10], even x]
30

zip [1,2] [’a’,’b’,’c’]
[(1,‘a’),(2,‘b’)]

2.What does following expressions evaluate to?
takeWhile even [2,4,6,7,8]
takeWhile函数作用为根据一个条件从列表的开头开始,取出满足该条件的所有元素,直到遇到第一个不满足条件的元素为止。一旦遇到不满足条件的元素,它就停止,并不会继续处–理列表的剩余部分。注意其会停止
[2,4,6,8]

3.Deffne a recursive function

insert :: Int -> [Int] -> [Int]

that inserts an integer into the correct position in a sorted list of
integers.
insert :: Int -> [Int] -> [Int]
insert x [] = [x]
insert x (y:ys) = if x>y then y : insert x ys
else x : y : ys

4.Deffne the following library function using recursion:

and :: [Bool] -> Bool
and [] = True
and (x:xs) = x && and xs

5.Deffne the following library function using recursion:

reverse :: [a] -> [a]
Wrong answer:reverse (x:xs) = reverse xs : x
Correct answer:reverse (x:xs) = reverse xs ++ [x]

6.Deffne the following library function using recursion:

replicate :: Int -> a -> [a]
replicate 0 _ = []
replicate n x = x : replicate (n-1) x

7.Express the sum of the squares of the integers between 1 and 100
using a list comprehension.
sum1 0 = 0
sum1 n= n^2 + sum1(n-1)
OR
sum [ n^2 | n<-[1…100]

8.Express the sum of the products of all pairs of integers between 1
and 100 using a list comprehension.

sum [ x * y | x<- [1…100], y<-[1…100] ]

9.Given the deffnitions

(f . g) x = f (g x)
map f [ ] = [ ]
map f (x:xs) = f x : map f xs

prove the following property of map using induction (for ffnite lists):

map f (map g xs) = map (f . g) xs

见Bilibili视频.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值