私は一般的にMonadsとHaskellに不慣れであり、それらを使用するときに値を返す方法を理解しようとしています。私のコードは次のようになります。
foo :: A -> B
foo a = do b <- fooC a (C 0)
-- want to return just (B "b")
fooC :: A -> C -> State MyState B
fooC a c = return (B "b")
を使ってみsnd (snd b)
ましたが、どうやらState MyState B
タプルではないのですか?どうすれば希望の値を返すことができます(B "b")
か?
編集:ダニエルのアドバイスを考慮に入れると、書き直しは次のようになります。
data MyState = MyState String
data C = C Int
foo :: String -> String
-- want to return just "b"
foo a = evalState (fooC a) (C 0)
fooC :: String -> Int -> State MyState String
fooC a c = return "b"
それでもコンパイルエラーが発生します。
Couldn't match expected type `State s0 String'
with actual type `Int -> State MyState String'
In the return type of a call of `fooC'
Probable cause: `fooC' is applied to too few arguments
In the first argument of `evalState', namely `(fooC a)'
In the expression: evalState (fooC a) (C 0)
編集2:修正済み!最終バージョンは次のようになります。
import Control.Monad.State
data MyState = MyState String
data C = C Int
foo :: String -> String
-- want to return just (B "b")
foo a = evalState (fooC a (C 0)) (MyState "whatever")
fooC :: String -> C -> State MyState String
fooC a c = return "b"
main = print(foo("test"))
-- prints "b"