I m Learning Haskell, and come over the following Code:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
在如何工作方面,我有点麻烦。 我理解,这种说法非常不必要,但我很想知道,哈斯凯尔如何在我写时设法“填入”纤维:
take 50 fibs
任何帮助?
感谢!
I m Learning Haskell, and come over the following Code:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
在如何工作方面,我有点麻烦。 我理解,这种说法非常不必要,但我很想知道,哈斯凯尔如何在我写时设法“填入”纤维:
take 50 fibs
任何帮助?
感谢!
我只想解释一下它如何在内部运作。 首先,你们必须认识到,Haskell在价值上使用了“。 A thunk基本上是一个尚未计算的价值——认为它是一种零论据的功能。 每当Haskell想要时,它就可以评估(或部分评价)th,将其变为实际价值。 如果只有部分<>评价th,那么由此产生的数值就会增加。
例如,考虑这一表述:
(2 + 3, 4)
以普通语文,这一数值将储存在<代码>(5, 4)上,但储存在Haskell,作为<代码>(<thunk 2 + 3>4)。 如果你要求列入该图的第二个要素,就会告诉你“4”,而不会把2和3加在一起。 只有当你要求了解该图的第一点时,才会评价该图,并相信该图是5。
有了假想,它就变得更加复杂,因为它是休妻的,但我们也可以使用同样的想法。 缩略语 没有任何论点,Haskell将永久储存已经发现的任何清单内容——这一点很重要。
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
It helps to visualise Haskell s current knowledge of three expressions: fibs
, tail fibs
and zipWith (+) fibs (tail fibs)
. We shall assume Haskell starts out knowing the following:
fibs = 0 : 1 : <thunk1>
tail fibs = 1 : <thunk1>
zipWith (+) fibs (tail fibs) = <thunk1>
Note that the 2nd row is just the first one shifted left, and the 3rd row is the first two rows summed.
www.un.org/Depts/DGACM/index_french.htm Haskell的确没有必要对上述情况作进一步评估。
如欲查阅<代码>第3条fibs和Haskell将获得0和1,然后将其需要部分评价<>。 为了充分评价<代码>zipWith (+) fibs (tail fibs),它需要总结头两行——但它不能完全做到这一点,但可以做到:begin,将头两行合并起来:
fibs = 0 : 1 : 1: <thunk2>
tail fibs = 1 : 1 : <thunk2>
zipWith (+) fibs (tail fibs) = 1 : <thunk2>
请注意,我以第三行的“1”填满,而且自动出现在第一和第二行,因为所有三行都共用同一座椅(认为这三行是一纸空话)。 由于它没有完成评价,因此,如果需要,它就创建了一个包含名单rest<>>>的新的星号。
但是,由于<代码>接收了3 fibs:[0,1,<>/code>,因此不需要。 但现在,你要求<代码>接收50个纤维代码>; Haskell已经记得0、1和1。 但需要保持下去。 因此,它继续召集头两行:
fibs = 0 : 1 : 1 : 2 : <thunk3>
tail fibs = 1 : 1 : 2 : <thunk3>
zipWith (+) fibs (tail fibs) = 1 : 2 : <thunk3>
...
fibs = 0 : 1 : 1 : 2 : 3 : <thunk4>
tail fibs = 1 : 1 : 2 : 3 : <thunk4>
zipWith (+) fibs (tail fibs) = 1 : 2 : 3 : <thunk4>
因此,在填满第三行48栏之前,先确定头50名。 Haskell评估的频率与需要相同,在需要时,将顺序的无限“恢复”作为th子。
请注意,如果你随后要求<代码>接收25 fibs,Haskell将不再评价——它只是从已经计算的清单中抽出头25个数字。
<>Edit:为避免混淆而增加每一根基的独一号。
I wrote an article on this a while back. You can find it here.
As I mentioned there, read chapter 14.2 in Paul Hudak s book "The Haskell School of Expression", where he talks about Recursive Streams, using Fibonacci example.
Note:tail of a sequence is the sequence without the first item.
|---+---+---+---+----+----+----+----+------------------------------------| | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | Fibonacci sequence (fibs) | |---+---+---+---+----+----+----+----+------------------------------------| | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | tail of Fib sequence (tail fibs) | |---+---+---+---+----+----+----+----+------------------------------------|
添加两栏:add fibs (tail fibs),以获取tail of fib序列
|---+---+---+---+----+----+----+----+------------------------------------| | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | tail of tail of Fibonacci sequence | |---+---+---+---+----+----+----+----+------------------------------------|
add fibs (tail fibs) can be written as zipWith (+) fibs (tail fibs)
Now, all we need to do prime the generation by starting with the first 2 fibonacci numbers to get the complete fibonacci sequence.
1:1: zipWith (+) fibs (tail fibs)
Note: This recursive definition will not work in a typical language that does eager evaluation. It works in haskell as it does lazy evaluation. So, if you ask for the first 4 fibonacci numbers, take 4 fibs, haskell only computes enough of sequence as required.
A very related example run through can be found here, although I haven t gone over it completely it maybe of some help.
I am not exactly sure of the implementation details, but I suspect they should be on the lines of my argument below.
Please take this with a pinch of salt, this maybe inaccurate implementationally but just as an understanding aid.
Haskell将不评价任何东西,除非它被迫,即所谓的“拉齐评价”,这是一个美丽的概念。
因此,让人们假设我们只被要求做take 3 fibs
。 Haskell Sp the fibs
list as 0:1:another_list
,因为我们可能也认为,该编码作为fibs = 0:1:x:another_list
和 (tail fibs) = 1:x:another_list
和0: 1:+_zpWith (+) fibs (tail fibs)
。 (1+x) : (x+head another_list) ...
根据对Haskell公司的了解,x = 0 + 1
因此,我们走向<代码>0:1:<>。
如果有人知道某种适当的执行细节,我真心感兴趣。 我可以理解,“拉齐”评价技术可能相当复杂。
希望有助于理解。
强制宣布: 请用一套盐类来看待这一点,这可能不准确执行,而只是作为一种理解援助。
Let s take a look at definition of zipWith
zipWith f (x:xs) (y:ys) = f x y : zipWith xs ys
Our fibs is:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
For take 3 fibs
substituting the definition of zipWith
and xs = tail (x:xs)
we get
0 : 1 : (0+1) : zipWith (+) (tail fibs) (tail (tail fibs))
For take 4 fibs
substituting once more we get
0 : 1 : 1 : (1+1) : zipWith (+) (tail (tail fibs)) (tail (tail (tail fibs)))
等等。
I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...
I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...
or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...
我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...