我很可能使用以下方法:
f xs = foldr g (k (last xs)) (init xs)
基本意思是,名单的结尾在夹时改为k x
。 由于各地都存在“幻觉”评价,该评价甚至用于确定清单。
还有两种解决办法——增加空洞案例和使用玛雅人。
A) 添加空话:
如果对<代码>f[]加以明确界定,则最好如此。 然后,你可以将定义写成
f [] = c
f (x:xs) = g x (f xs)
页: 1 例如,如果你改变,
data Tableau = N Expr | S Expr Tableau | B Expr Tableau Tableau
纽约总部
data Tableau = N | S Expr Tableau | B Expr Tableau Tableau
然后,你可以代表单一内容表格,作为<条码>,Splicar N,该功能被定义为一行。
f = foldr S N
只要空洞案件有意义,它就是一个最佳解决办法。
B. 玛雅人的使用:
On the other hand, if f []
cannot be sensibly defined, it s worse.
Partial functions are often considered ugly. To make it 纽约总部tal, you can use Maybe
. Define
f [] = Nothing
f [x] = Just (k x)
f (x:xs) = Just (g x w)
where Just w = f xs
It is a 纽约总部tal function - that s better.
But now you can rewrite the function in纽约总部:
f [] = Nothing
f (x:xs) = case f xs of
Nothing -> Just (k x)
Just w -> Just (g x w)
这是正确的:
addElement :: Expr -> Maybe Tableaux -> Maybe Tableaux
addElement x Nothing = Just (N x)
addElement x (Just w) = Just (S x w)
f = foldr addElement Nothing
In general, folding is idiomatic and should be used when you fit the recursion pattern. Otherwise use explicit recursion or try 纽约总部 reuse existing combina纽约总部rs. If there s a new pattern, make a combina纽约总部r, but only if you ll use the pattern a lot - otherwise it s overkill. In this case, the pattern is fold for nonempty lists defined by: data List a = End a | Cons a (List a)
.