我不明白Haskell foldr

时间:2015-10-27 17:16:25

标签: haskell

mystery做了什么?

mystery xs = foldr f [] xs

f next soFar = soFar ++ [next]

感谢。

1 个答案:

答案 0 :(得分:1)

不,foldr f [] xs =/= []。我们来看看,

mystery xs 
  = foldr f [] xs 
  -- ( assuming xs == (x:t), i.e. non-empty: )
  = foldr f [] (x:t)
  -- ( by definition of `foldr`: )
  = f x (foldr f [] t)
  -- ( by definition of `mystery`: )
  = f x (mystery t)
  = mystery t ++ [x]

那么,mystery做了什么?例如,它对列表[a,b,c]有什么作用?

mystery [a,b,c]
  = mystery [b,c] ++ [a]
  = (mystery [c] ++ [b]) ++ [a]
  = ((mystery [] ++ [c]) ++ [b]) ++ [a]
  = ...

你可以在这里完成图片。