withstanding slings and arrows of chapter 6

11 views
Skip to first unread message

Chip Grandits

unread,
Feb 18, 2009, 10:24:13 AM2/18/09
to Real World Haskell Book Club
I thought I would go through the exercises at the end of Chapter 6.
Although they seem to be somewhat of an afterthought, not really
relevant to all of the material just introduced.

But I took a swing at it anyway. I'm not sure why, after a somewhat
exhausting chapter, arrows are snuck in at the last moment and are the
topic of the first exercise. Unless one already knows about arrows, I
can't see how anyone could approach the first exercise, but to simply
stop reading Real World Haskell and go out and read about arrows.

The first strategy I learned, getting the type of second:
arrow a => a b c -> a (d b) (d c)
is quite perplexing to the inexperienced.

In order to even understand how that understand that type signature I
found my way to
http://en.wikibooks.org/wiki/Haskell/Understanding_arrows

It seems that the task could have been reasonably easily achieved
without introducing arrows. I'm curious what anyone things of my
arrowless implementation using what seems like a very straight forward
lambda function.

--easy to comprehend version of second with no arrow
na_second f = (\(a,b) -> (a, f b))

instance (JSON a) => JSON (JObj a) where
toJValue = JObject . JObj . map (na_second toJValue) . fromJObj

fromJValue (JObject (JObj o)) = whenRight JObj (mapEithers unwrap
o)
where unwrap (k,v) = whenRight ((,) k) (fromJValue v)
fromJValue _ = Left "not a JSON object"


--Chip

Reply all
Reply to author
Forward
0 new messages