Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Haskell help, /prog/!

Name: Anonymous 2008-09-16 15:04


-- create a random tree.
randomTree :: Random r => IO (BinaryTree r)
randomTree =
     -- randomly pick the left and right sides.
  do leftIO  <- pick(randomLeaf, randomBranch)
     rightIO <- pick(randomLeaf, randomBranch)
     -- "un-IO" the sides.
     left    <- leftIO
     right   <- rightIO
     -- get a random value for the top of the branch/tree.
     value   <- randomIO
     -- create branch and return.
     return (Branch left value right)
  where
    -- return left or right at random.
    pick (left, right) =
      do useLeft <- randomRIO(True, False)
         if useLeft then return left else return right
    -- create a random leaf.
    randomLeaf =
      do value <- randomIO
         let leaf = Leaf value
         return leaf
    -- setup an alias.
    randomBranch = randomTree


This code actually works fine, but I'm wondering if there's a way to simplify the "leftIO/rightIO" stuff. Is there some sort of "double arrow" that will un-Monad twice, or is this verbosity unavoidable?

Name: Anonymous 2008-09-17 19:27

Not OP. I was considering to consider learning Haskell, but I was completely put off by this thread. I'm glad to see so many Anonymous trying to help, but if it takes several people and several iterations of Perl-like mess to write a simple tree algorithm, then I see the language is way too experimental, weird and unpractical. It's not without value — these curious abstractions and techniques are worth trying somewhere, and Haskell is the place. Once they prove good enough, more practical languages (where you actually get stuff done) like FIOC should steal them.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List