Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I know it's not the point of what you were mentioning, but I wanted to briefly mention that reap/sow are not unlike Haskell's so-called Writer Monad:

    newtype Farm a b = Farm (b, [a]) deriving (Eq,Show)

    instance Monad (Farm b) where
      return x = Farm (x, [])
      Farm (b, as) >>= f =
        let Farm (b', as') = f b in Farm(b', as ++ as')

    reap :: Farm a b -> (b, [a])
    reap (Farm x) = x

    sow :: a -> Farm a ()
    sow a = Farm ((), [a])

    -- prints ("e", ["a","c","d"])
    main = print (reap (do sow "a"
                           return "b"
                           sow "c"
                           sow "d"
                           return "e"))
(Reimplemented in a simple way here to show the underlying machinery; usually, instead of lists, a writer is parameterized by an arbitrary monoid.)


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: