我正在学习haskell并且有点困惑函数应用程序运算符$ curry的.
根据GHC,$的类型是
*Main>:t ($)
($) :: (a->b) -> a -> b
Run Code Online (Sandbox Code Playgroud)
但我可以输入以下代码
*Main>map ($ 2) [(*2), (+2), (/2)]
[4.0,4.0,1.0]
Run Code Online (Sandbox Code Playgroud)
根据$的签名,虽然我认为我需要使用翻转函数,因为$的第一个参数是(a-> b).
例如,我不能做以下事情
curry_test :: Integer -> String -> String
curry_test x y = (show x) ++ " " ++ y
*Main> let x = curry_test "123"
Couldn't match expected type `Integer' with actual type `[Char]'
In the first argument of `curry_test', namely `"123"'
In the expression: curry_test "123"
In an equation for `x': x = curry_test "123"
Run Code Online (Sandbox Code Playgroud)
但我能做到
let …Run Code Online (Sandbox Code Playgroud) 我正在学习Haskell,正在阅读Tying the Knot关于如何构建循环链表.在代码中
data DList a = DLNode (DList a) a (DList a)
mkDList :: [a] -> DList a
mkDList [] = error "must have at least one element"
mkDList xs = let (first,last) = go last xs first
in first
where go :: DList a -> [a] -> DList a -> (DList a, DList a)
go prev [] next = (next,prev)
go prev (x:xs) next = let this = DLNode prev x rest
(rest,last) = go this …Run Code Online (Sandbox Code Playgroud)