Вот пример из учебника Haskell:Haskell, получая значение от привязок
type Bindings = Map.Map String Int
isCountCorrect :: Bindings -> Bool
isCountCorrect bindings = runReader calc_isCountCorrect bindings
calc_isCountCorrect :: Reader Bindings Bool
calc_isCountCorrect = do
count <- asks (lookupVar "count")
bindings <- ask
return (count == (Map.size bindings))
lookupVar :: String -> Bindings -> Int
lookupVar name bindings = fromJust (Map.lookup name bindings)
sampleBindings = Map.fromList [("count", 4), ("1", 1), ("b", 2)]
main = do
putStr $ "Count is correct for bindings " ++ (show sampleBindings) ++ ": "
putStrLn $ show (isCountCorrect sampleBindings)
Я создал функцию, чтобы получить значение «2». Я не знаю, как сделать его общим для произвольной String. Не могли бы вы помочь мне ?
get_valueM :: Reader Bindings Int
get_valueM = asks (lookupVar "b") >>= (\value -> ask >>= (\_ -> return value))
get_value :: Bindings -> Int
get_value bindings = runReader get_valueM bindings
Кроме того, я использовал запрос, потому что >> = передает среду на две подвычисления. Поэтому я не могу заставить эту функцию получить этот аргумент (enviroment) –