有人可以用简单的话是什么这两个运营商做给我解释一下:
$
\
有人可以用简单的话是什么这两个运营商做给我解释一下:
$
\
\
不是运算符,它是字面语法的一部分。更准确地说,它是两个字面语法的一部分:它表示一个lambda字面值,它用作字符串文字中的转义字符。
操作$
在前奏定义为
($) :: (a -> b) -> a -> b
f $ x = f x
换句话说,它恰好同样的事情不空白,即只是简单的功能的应用程序。 但是,虽然函数应用程序是左关联的并且具有高优先级(实际上最高),但$
是右关联的并且具有低优先级。
这可以让你当你有像“f
应用于g
应用于h
应用于x
”,这不$
运营商将这样写
f (g (h x))
,但与运营商可以链省略括号写成
f $ g $ h x
如果要将函数应用程序运算符本身作为参数传递给另一个函数,它也很有用。假设您有函数列表和值列表,并且您希望将列表中的每个函数应用到另一个列表中的相应值:
zipWith ($) fs xs
($)::(A - > B) - > a - > b base Prelude,base Data.Function Application operator。这个操作符是多余的,因为普通的应用程序(f x)的含义与(f $ x)相同。但是,$具有较低的右联合绑定优先级,因此它有时会允许省略括号;
关键字\ 反斜杠 “\” 在多行字符串被用来>在lambda函数为 “foo \> \栏”>>> \ X - > X + 1
什么这两个运营商做: $ \
第一个,($)
,是运营商,其定义为:
-- | Application operator. This operator is redundant, since ordinary
-- application @(f x)@ means the same as @(f '$' x)@. However, '$' has
-- low, right-associative binding precedence, so it sometimes allows
-- parentheses to be omitted; for example:
--
-- > f $ g $ h x = f (g (h x))
--
-- It is also useful in higher-order situations, such as @'map' ('$' 0) [email protected],
-- or @'Data.List.zipWith' ('$') fs [email protected]
($) :: (a -> b) -> a -> b
f $ x = f x
它可以让你用更少的parenthesese写入功能。
第二个标记\
是lambda abstractions的匿名函数的Haskell语法的一部分。
因此,例如
\x -> x + 1
是一个将其参数加1的函数。 lambda抽象的语法是described in the Haskell Report。
反斜杠是在特定上下文中具有特殊含义的符号。 '$'是[前奏](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-Base.html#%24)中定义的函数。 – 2011-04-07 21:06:22
另请参阅http://stackoverflow.com/questions/2787543/haskell-what-is-the-difference-between-dollar-and-dollar-exclamation-poi – 2011-04-24 19:36:02