我正在我的人工智能实验室学习Prolog,从源头学习Prolog Now!.
在第5章中,我们来了解累加器.作为示例,给出了这两个代码片段. 查找列表的长度
没有累加器:
len([],0).
len([_|T],N) :- len(T,X), N is X+1.
Run Code Online (Sandbox Code Playgroud)
与累加器:
accLen([_|T],A,L) :- Anew is A+1, accLen(T,Anew,L).
accLen([],A,A).
Run Code Online (Sandbox Code Playgroud)
我无法理解,这两个片段在概念上有何不同?累加器到底有什么不同?有什么好处?
蓄能器听起来像中间变量.(如果我错了,请纠正我.)到目前为止,我已经在我的程序中使用过它们,所以它真的是一个很大的概念吗?
我正在学习haskell,其中一个练习要求我写一个相当于的函数enumFromTo.
我想出了以下两个实现:
eft' :: Enum a => a -> a -> [a]
eft' x y = go x y []
where go a b sequence
| fromEnum b < fromEnum a = sequence
| otherwise = go (succ a) b (sequence ++ [a])
eft :: Enum a => a -> a -> [a]
eft x y = go x y []
where go a b sequence
| fromEnum b < fromEnum a = sequence
| otherwise = go …Run Code Online (Sandbox Code Playgroud)