List 下面的函数做什么?(加拿大)

List 下面的函数做什么?(加拿大),list,haskell,List,Haskell,我真的不明白在这个例子中,scanl(\sf->fsg做了什么。什么是f data Grid = Grid [Line] data CellState = Dead | Alive deriving (Eq) data Cell = Cell CellState Int deriving (Eq) data Line = Line [Cell] Int deriving (Eq) run :: Grid -> Int -> [Grid] run g n = scanl (\s f

我真的不明白在这个例子中,
scanl(\sf->fsg
做了什么。什么是
f

data Grid = Grid [Line]
data CellState = Dead | Alive deriving (Eq)
data Cell = Cell CellState Int deriving (Eq)
data Line = Line [Cell] Int deriving (Eq)

run :: Grid -> Int -> [Grid]
run g n = scanl (\s f -> f s) g $ replicate n playRound

playRound :: Grid -> Grid

从以下文件中:

scanl与foldl类似,但从左侧返回连续缩减值的列表:

scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
注意
last(scanl f z xs)==foldl f z xs

因此,
scanl(\sf->fsg
的行为如下:

scanl (\s f -> f s) g [x1, x2, ...] == [g, x1 g, x2 (x1 g), ...]
由于
\s f->f s
是一个接受两个参数并将第二个参数应用于第一个参数的参数:

 λ> (\s f -> f s) 2 (+1)
 3
 λ> (\s f -> f s) "Hello" length
 5
请注意,
\sf->fs
可以写成
翻转($)


因此,具体而言:

run g n = scanl (\s f -> f s) g $ replicate n playRound
可以看作:

run g n = [g, playRound g, playRound (playRound g), ...] -- Repeated n times.

从以下文件中:

scanl与foldl类似,但从左侧返回连续缩减值的列表:

scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
注意
last(scanl f z xs)==foldl f z xs

因此,
scanl(\sf->fsg
的行为如下:

scanl (\s f -> f s) g [x1, x2, ...] == [g, x1 g, x2 (x1 g), ...]
由于
\s f->f s
是一个接受两个参数并将第二个参数应用于第一个参数的参数:

 λ> (\s f -> f s) 2 (+1)
 3
 λ> (\s f -> f s) "Hello" length
 5
请注意,
\sf->fs
可以写成
翻转($)


因此,具体而言:

run g n = scanl (\s f -> f s) g $ replicate n playRound
可以看作:

run g n = [g, playRound g, playRound (playRound g), ...] -- Repeated n times.
如果我们看看这些数据,我们会发现:

注意

last (scanl f z xs) == foldl f z xs.
因此,它从n个初始值
z
开始,每次它对
z
和给定列表的下一个元素应用一个函数

这里的初始值是
g
,列表是
replicate n playRound
(这意味着
n
项的列表,每个项都是
playRound
)。这里的函数接受累加器
s
,列表中的一个元素(这里总是
playRound
),而
fs
(在本例中是
playRound s
)的结果是列表中的下一项

因此,它将生成一个列表:

[g, playRound g, playRound (playRound g), ...]
该列表将包含
n+1

也许更优雅的方法应该是:

run :: Grid -> Int -> [Grid]
run g n = take (n+1) $ iterate playRound g
如果我们看看这些数据,我们会发现:

注意

last (scanl f z xs) == foldl f z xs.
因此,它从n个初始值
z
开始,每次它对
z
和给定列表的下一个元素应用一个函数

这里的初始值是
g
,列表是
replicate n playRound
(这意味着
n
项的列表,每个项都是
playRound
)。这里的函数接受累加器
s
,列表中的一个元素(这里总是
playRound
),而
fs
(在本例中是
playRound s
)的结果是列表中的下一项

因此,它将生成一个列表:

[g, playRound g, playRound (playRound g), ...]
该列表将包含
n+1

也许更优雅的方法应该是:

run :: Grid -> Int -> [Grid]
run g n = take (n+1) $ iterate playRound g

f
是lambda表达式的第二个参数。
f
是lambda表达式的第二个参数。