You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.5 KiB
1.5 KiB
22 Reader
22.2 Short Exercise: Warming up
22.5 Excercise: Ask
22.6 Exercise: Reading Comprehension
22.7 Exercise: Reader Monad
22.9 You can change what comes below, but not above
Trying to understand the following quote:
You can swap in a different type or value of
r
for function that you call, but not for functions that call you.
Perhaps a better way of saying this would be:
You can can choose the input for a function you are calling, but cannot change the input from within the function.
The input being the context of a Reader
. This makes sense, consider:
f :: a -> b
When we call f
, we can choose any input value or type to use. However, within
f
, the value and type are fixed. Immutable. The same applies of course for
Reader a b
as this is just a newtype
for f
.
f :: Reader Integer String
f = do
r <- ask
-- we cannot change r, but we can do operations on them
return $ show (r * r)
-- However, we can change the input for f when we call it:
g :: Reader Integer String
g = do
r <- ask
-- either via:
-- return $ runReader f (r + 1)
-- or:
withReader (+1) f
22.11 Chapter exercises
A warmup stretch
Rewriting Shawty
No idea if this is what was requested, but it works: