标签:replicat otherwise 反转 函数 最大值 tail pre 递归函数 模式匹配
-- 取list中的最大值
maxiMum' :: (Ord a) => [a] -> a
maxiMum' [] = error "Empty list"
maxiMum' [x] = x
maxiMum' (x:xs)
| x > maxTail = x
| otherwise = maxTail
where maxTail = maxiMum' xs
maxiMum1' :: (Ord a) => [a] -> a
maxiMum1' [] = error "Empty list"
maxiMum1' [x] = x
maxiMum1' (x:xs) = max x (maxiMum1' xs)
-- 构建含n个x的list
replicate' :: (Num i, Ord i) => i -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x:replicate' (n-1) x
-- 取list前n个
take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
| n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
-- 反转list
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
Haskell 编写几个递归函数 练习 typeclass 模式匹配等
标签:replicat otherwise 反转 函数 最大值 tail pre 递归函数 模式匹配
原文地址:https://www.cnblogs.com/vlyf/p/12530801.html