Difference between revisions of "99 questions/Solutions/1"
< 99 questions | Solutions
Jump to navigation
Jump to search
Line 8: | Line 8: | ||
myLast' = foldr1 (const id) |
myLast' = foldr1 (const id) |
||
+ | -- Prelude> const 1 2 |
||
⚫ | |||
+ | -- 1 |
||
+ | -- Prelude> (flip const) 1 2 |
||
+ | -- 2 |
||
+ | myLast'' = foldr1 (flip const) |
||
+ | |||
⚫ | |||
</haskell> |
</haskell> |
||
Revision as of 06:54, 8 October 2011
(*) Find the last element of a list.
myLast :: [a] -> a
myLast [x] = x
myLast (_:xs) = myLast xs
myLast' = foldr1 (const id)
-- Prelude> const 1 2
-- 1
-- Prelude> (flip const) 1 2
-- 2
myLast'' = foldr1 (flip const)
myLast''' = head . reverse
The Prelude
also provides the function last
.