这个函数连接如何与一阶函数一起工作?
How does this function concatenate work with first order functions?
let concat (l : string list) : string = fold_right (fun a x -> a ^ x) l ""
所以通过它,我看到
- let concat 接受一个包含字符串的列表,returns 接受一个字符串。
- fold right需要3个参数,
- 1 第一个是连接两个字符串的函数,它接受 a 和 x,然后使用 ^ 连接它们。
- 2 第二个参数是列表,
- 3,最后第三个是累加器,每次传递都添加到它。
但是,func a x
怎么知道 a 是列表的第一个元素,x 是第二个元素。
当它再次通过时,它怎么知道a是列表的第三个元素,x是第四个,依此类推?
accumulator
是第一个参数,a
到fold function
,x
是列表的set to each element
依次为fold
从上面走过。
fold function
的 return 值是 accumulator
的 new value
。
accumulator
的初始值为 empty string
,因此第一个串联的结果成为 new accumulator
,与 first element
的值相同] 的名单。
然后 next element
连接到那个,依此类推,直到到达列表的末尾,此时 fold
returns final value of the accumulator
,完全连接的字符串。
let concat (l : string list) : string = fold_right (fun a x -> a ^ x) l ""
所以通过它,我看到
- let concat 接受一个包含字符串的列表,returns 接受一个字符串。
- fold right需要3个参数,
- 1 第一个是连接两个字符串的函数,它接受 a 和 x,然后使用 ^ 连接它们。
- 2 第二个参数是列表,
- 3,最后第三个是累加器,每次传递都添加到它。
但是,func a x
怎么知道 a 是列表的第一个元素,x 是第二个元素。
当它再次通过时,它怎么知道a是列表的第三个元素,x是第四个,依此类推?
accumulator
是第一个参数,a
到fold function
,x
是列表的set to each element
依次为fold
从上面走过。
fold function
的 return 值是 accumulator
的 new value
。
accumulator
的初始值为 empty string
,因此第一个串联的结果成为 new accumulator
,与 first element
的值相同] 的名单。
然后 next element
连接到那个,依此类推,直到到达列表的末尾,此时 fold
returns final value of the accumulator
,完全连接的字符串。