The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: comp.lang.haskell
From: daucl...@gmail.com
Date: Wed, 14 Nov 2007 17:30:13 -0000
Local: Wed, Nov 14 2007 12:30 pm
Subject: State Monad style
Hello, all,
Working at state monad again. Wikipedia (http://en.wikipedia.org/wiki/ > a :: Integer -> Integer -> Integer After running it, I immediately thought of using the State monad to > a m n | m == 0 = n + 1 > | m > 0 && n == 0 = a (m - 1) 1 > | m > 0 && n > 0 = a (m - 1) (a m (n - 1)) memo intermediate results (sort of a top-down dynamic programming approach): > type AckMap = Map (Integer, Integer) Integer now we need to recursively update the map > type AckMapS = State AckMap Integer > ackify :: Integer -> Integer -> AckMapS > ackify m n = do mappo <- get > case Map.lookup (m, n) mappo of > Just num -> return num > Nothing -> ackify' m n > ackify' :: Integer -> Integer -> AckMapS ... and that works fine (ackify' is my monadic version of my Ackermann > ackify' m n | m == 0 = return (n + 1) >>= ackify'' m n > | m > 0 && n == 0 = ackify (m - 1) 1 >>= ackify'' m n > | m > 0 && n > 0 = > ackify m (n - 1) >>= ackify (m - 1) arg >>= ackify'' m n > ackify'' :: Integer -> Integer -> Integer -> AckMapS > ackify'' m n ans = do mappo <- get > put (Map.insert (m, n) ans mappo) > return ans function 'a'), speeding up the computation exponentially, it seems to me. My question is, am I violating any principles of monadic style or Sincerely, BTW, wikipedia has a HO functional definition that transliterates to > ack :: Integer -> Integer -> Integer (perhaps the wikipedia def has an error, but that's not a propos to > ack 0 = succ > ack m = iter $ ack (m - 1) > iter :: (Integer -> Integer) -> Integer -> Integer > iter f 0 = f 1 > iter f n = f $ iter f (n - 1) this discussion) You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||