简体   繁体   English

Haskell用匿名函数折叠

[英]Haskell Fold with anonymous function

I have a problem with one of the Haskell basics: Fold + anonymous functions 我有一个Haskell基础知识的问题:折叠+匿名函数

I'm developing a bin2dec program with foldl . 我正在用foldl开发一个bin2dec程序。
The solution looks like this: 解决方案如下所示:

bin2dec :: String -> Int
bin2dec = foldl (\x y -> if y=='1' then x*2 + 1 else x*2) 0

I understand the basic idea of foldl / foldr but I can't understand what the parameters xy stands for. 我理解foldl / foldr的基本思路,但我无法理解xy代表的参数。

See the type of foldl 查看foldl的类型

foldl :: (a -> b -> a) -> a -> [b] -> a

Consider foldl fz list 考虑foldl fz list

so foldl basically works incrementally on the list (or anything foldable), taking 1 element from the left and applying fz element to get the new element to be used for the next step while folding over the rest of the elements. 所以foldl基本上在列表上递增(或任何可折叠的),从左边取1个元素并应用fz element以获得新元素用于下一步,同时折叠其余元素。 Basically a trivial definition of foldl might help understanding it. 基本上,foldl的一个简单定义可能有助于理解它。

 foldl f z []     = z
 foldl f z (x:xs) = foldl f (f z x) xs

The diagram from Haskell wiki might help building a better intuition. 来自Haskell wiki的图表可能有助于建立更好的直觉。

foldl f z

Consider your function f = (\\xy -> if y=='1' then x*2 + 1 else x*2) and try to write the trace for foldl f 0 "11" . 考虑你的函数f = (\\xy -> if y=='1' then x*2 + 1 else x*2)并尝试为foldl f 0 "11"写入跟踪。 Here "11" is same as ['1','1'] 这里"11"['1','1']

  foldl f 0 ['1','1'] 
= foldl f (f 0 '1') ['1']

Now f is a function which takes 2 arguments, first a integer and second a character and returns a integer. 现在f是一个带有2个参数的函数,第一个是整数,第二个是一个字符并返回一个整数。 So In this case x=0 and y='1' , so fxy = 0*2 + 1 = 1 所以在这种情况下, x=0y='1' ,所以fxy = 0*2 + 1 = 1

= foldl f 1 ['1']
= foldl f (f 1 '1') []

Now again applying f 1 '1' . 现在再次应用f 1 '1' Here x=1 and y='1' so fxy = 1*2 + 1 = 3 . 这里x=1y='1'因此fxy = 1*2 + 1 = 3

= foldl f 3 [] 

Using the first definition of foldl for empty list. 使用foldl的第一个定义为空列表。

= 3 

Which is the decimal representation of "11". 这是“11”的十进制表示。

Use the types! 使用类型! You can type :t in GHCi followed by any function or value to see its type. 您可以在GHCi中键入:t ,后跟任何函数或值以查看其类型。 Here's what happens if we ask the for the type of foldl 如果我们询问foldl的类型,会发生什么

Prelude> :t foldl
foldl :: (a -> b -> a) -> a -> [b] -> a

The input list is of type [b] , so it's a list of b s. 输入列表的类型为[b] ,因此它是b的列表。 The output type is a , which is what we're going to produce. 输出类型是a ,这是我们要生成的。 You also have to supply an initial value for the fold, also of type a . 您还必须提供折叠的初始值,也是类型a的初始值。 The function is of type 该功能属于类型

a -> b -> a

The first parameter ( a ) is the value of the fold computed so far. 第一个参数( a )是到目前为止计算的折叠的值。 The second parameter ( b ) is the next element of the list. 第二个参数( b )是列表的下一个元素。 So in your example 所以在你的例子中

\x y -> if y == '1' then x * 2 + 1 else x * 2

the parameter x is the binary number you've computed so far, and y is the next character in the list (either a '1' or a '0' ). 参数x是你到目前为止计算的二进制数, y是列表中的下一个字符( '1''0' )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM