Chapter 2: Types and Functions
p39-2: Write a function, lastButOne, that returns the element before the last.
- -- file ch02/lastButOne.hs
-
lastButOne xs = if length xs == 2
-
then head xs
-
else lastButOne (tail xs)
tips: function length tells us how many elements are in a list;
function head returns the first element of a list;
function tail returns all but the head of a list, that is, a sub-list.
阅读(392) | 评论(0) | 转发(0) |