7
내가 Learn you a haskell 책을 통해 갈거야, 그리고 제 8 장에서이 작품이하스켈 - case 문
data LockerState = Taken | Free deriving (Eq, Show)
type Code = String
type LockerMap = Map.Map Int (LockerState, Code)
lookup' :: Int -> LockerMap -> Either String Code
lookup' num_ map_ =
case (Map.lookup num_ map_) of
Nothing -> Left $ "LockerNumber doesn't exist!"
Just (state, code) -> if state == Taken
then Left $ "LockerNumber already taken!"
else Right $ code
과 같은 코드의 조각이 내부 가드. 그러나 if/else 블록을 다음과 같이 보호해야합니다.
lookup' :: Int -> LockerMap -> Either String Code
lookup' num_ map_ =
case (Map.lookup num_ map_) of
Nothing -> Left $ "LockerNumber doesn't exist!"
Just (state, code) ->
| state == Taken = Left $ "LockerNumber already taken!"
| otherwise = Right $ Code
이것은 컴파일되지 않습니다. 하스켈에서 경비원의 사용은 매우 제한적/비 직관적 인 것으로 보인다. SO Ex1SO Ex2. 경비원을 사용할 수있는 장소를 알려주는 확실한 출처가 있습니까?
멋진 대답을! 특히'가드는 단순한 패턴의 제약이다. ' – skgbanga