Haskell99题答案

来源:互联网 发布:三毛 知乎 编辑:程序博客网 时间:2024/05/08 16:24

1 Problem 1

(*) Find the last element of a list.

(Note that the Lisp transcription of this problem is incorrect.)

Example in Haskell

Prelude> myLast [1,2,3,4]4Prelude> myLast ['x','y','z']'z'
答案
run::[a]->arun [x]=xrun (_:xs) = run xs

run = foldl (const id)

对于上述的代码,有如下的解释
const id True 2= (const id True) 2= id 2= 2

如果要保留第一个,可以用如下的代码
foldl1 const [1,2,3,4]


0 0
原创粉丝点击