please me understand following definition:
newtype writer w = writer { runwriter :: (a,w) } instance (monoid w) => monad (writer w) return = writer (a,mempty) (writer (a,w)) >>= f = let (a',w') = runwriter $ f in writer (a',w `mappend` w')
why runwriter declared as
runwriter :: (a,w)
when actual type is:
runwriter :: writer w -> (a, w)
once tried ghci realized must implicit argument since type "a" has determined, happening here?
because runwriter
record field accessor on writer
. it's equivalent to
runwriter (writer x) = x
haskell has records give
- more convient syntax since sort of accessor code quite common
- the ability functional updates
- a few other extensions
eg
somewriter{runwriter = (new, values)} -- returns new writer.
if helps, think of "functional getter" in roughest sense. might not seem terribly important 1 field, can pattern match, when have 5 fields, records + functional updates super helpful. see lyah more in depth explanation.
Comments
Post a Comment