2.17
{- Prelude.last
-- | Extract the last element of a list, which must be finite and non-empty.
last :: [a] -> a
#ifdef USE_REPORT_PRELUDE
last [x] = x
last (_:xs) = last xs
last [] = errorEmptyList "last"
#else
-- eliminate repeated cases
last [] = errorEmptyList "last"
last (x:xs) = last' x xs
where last' y [] = y
last' _ (y:ys) = last' y ys
#endif
-}
-- Because it assure you a nonempty list
lastPair [x] = x
lastPair (x:xs) = lastPair xs
2.18
{- Prelude.reverse
-- | 'reverse' @xs@ returns the elements of @xs@ in reverse order.
-- @xs@ must be finite.
reverse :: [a] -> [a]
#ifdef USE_REPORT_PRELUDE
reverse = foldl (flip (:)) []
#else
reverse l = rev l []
where
rev [] a = a
rev (x:xs) a = rev xs (x:a)
#endif
-}
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
2.19
cc amount coinValues
| amount == 0 = 1
| amount < 0 = 0
| coinValues == [] = 0
| otherwise = (cc amount (tail coinValues)) + (cc (amount - (head coinVa
lues)) coinValues)
2.20
不知道怎么定义任意个参数的函数。。
2.21
squareList = map (\x -> x*x)
2.22略
2.23
forEach = mapM_
2.24 ……
2.25 好像不是很好操作,Haskell里面List不能这么写,Tuple应该用模式匹配。
2.26 略
2.27
deepReverse xs = reverse $ map reverse xs
2.28 concat
2.29 略
185

被折叠的 条评论
为什么被折叠?



