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"
작동하지 않는 것 같습니다. 내 게시물의 편집을 참조하십시오. –
죄송합니다. 구문 분석에 실패했습니다. 업데이트 됨. –
일했습니다! 다니엘 감사합니다! –