【发布时间】:2019-04-05 06:30:07
【问题描述】:
假设我想根据其他一些预定义函数g 定义一个函数f,如下所示:
f :: Int -> Int -> Int
f 2 b = g b
f _ _ = 1
也就是说,我想定义投影,f(2,_) : Int->Int 与g(_) : Int->Int 相同。令人高兴的是,Haskell 具有一流的功能,因此像下面的 squarePlusOne 这样的定义是有效和标准的:
plusOne :: Int -> Int
plusOne i = i+1
square :: Int -> Int
square i = i*i
squarePlusOne :: Int -> Int
squarePlusOne = plusOne . Square
使用 Haskell 的柯里化(即 f 只接受一个 Int 作为输入并返回一个 (Int->Int) 类型的函数),我很惊讶我不能写
f 2 = g
为什么不呢?还是有其他语法?
【问题讨论】:
-
真的,为什么不呢?您收到任何错误消息吗?
-
糟糕!我发现我的问题完全是由另一个问题建立的。
标签: haskell currying function-composition first-class-functions