将运算符转换为相应的函数

时间:2016-10-14 20:21:14

标签: haskell

有没有办法转换字符' +',' - ',' *',' /'到相应的功能?我的意思是这样的功能(显然我尝试了它并且它没有工作):

SEEK_END

1 个答案:

答案 0 :(得分:3)

您可以使用模式匹配

轻松定义部分功能
Prelude> :set +m
Prelude> let f '+' = (+)
Prelude|     f '-' = (-)
Prelude|     f '*' = (*)
Prelude|     f '/' = (/)
Prelude|
Prelude> f '*' 3 4
12.0
Prelude> f '+' 1 2
3.0
Prelude>

和推导出的类型

Prelude> :t f
f :: Fractional a => Char -> a -> a -> a