Aug*_*son 2 monads haskell scope functional-programming module
所以我最近一直在研究monad,我对Haskell编程语言很新.
这是我的代码:
import Data.List
import System.IO
f :: Int -> (Int, [Char])
f x = (x, ['a'])
g :: Int -> (Int, [Char])
g y = (y, ['b'])
(>>=) :: (Int -> (Int, [Char])) -> (Int -> (Int, [Char])) -> Int -> (Int, [Char])
(>>=) f1 f2 a = f1 (fst (f2 a))
h :: Int -> (Int, [Char])
h x = (>>=) g f x
Run Code Online (Sandbox Code Playgroud)
GHCI编译器显示:错误:
Ambiguous occurrence `>>='
It could refer to either `Prelude.>>='
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我是否以正确的方式使用monad?
您正在定义一个绑定,(>>=)该绑定已经被前奏绑定(如上面的注释中所指出的).
因此,您需要消除对它的每个引用的歧义.
为您的模块命名
module M where
Run Code Online (Sandbox Code Playgroud)
然后,而不是写作
h x = (>>=) g f x
Run Code Online (Sandbox Code Playgroud)
使用其中之一
h x = (M.>>=) g f x
Run Code Online (Sandbox Code Playgroud)
要么
h x = (Prelude.>>=) g f x
Run Code Online (Sandbox Code Playgroud)
选择>>=你想要的版本.
但是,我认为这不是你真正想做的事情.这(>>=)将是一个与Monad课程无关的约束.
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |