[Haskell-cafe] monad question

Scott Turner p.turner at computer.org
Sun Sep 19 20:27:11 EDT 2004


On 2004 September 19 Sunday 13:40, Andrew Harris wrote:
> handleSeeRecord :: [SeeObjInfo_type] -> RobotState -> IO (RobotState, ())
> handleSeeRecord seeobjlist p = do flaglist <- return (morphToList
> flagFinder seeobjlist)
>                                  balllist <- return (morphToList
> ballFinder seeobjlist)
>                                  friendlist <- return (morphToList
> friendFinder seeobjlist)
>                                  foelist <- return (morphToList
> foeFinder seeobjlist)
>                                  Robot e <- return (assign_flags
> (flagSpread flaglist))
>                                  Robot f <- return (assign_ball balllist)
>                                  Robot g <- return (assign_friends
> friendlist) Robot h <- return (assign_foes foelist) (r', ()) <- e p
>                                  (r'', ()) <- f r'
>                                  (r''', ()) <- g r''
>                                  h r'''

What you're looking for is something like
handleSeeRecord :: [SeeObjInfo_type] -> Robot ()
handleSeeRecord seeobjlist = do 
 let flaglist = morphToList flagFinder seeobjlist
 let balllist = morphToList ballFinder seeobjlist
 let friendlist = morphToList friendFinder seeobjlist
 let foelist = morphToList foeFinder seeobjlist
 assign_flags (flagSpread flaglist)
 assign_ball balllist
 assign_friends friendlist
 assign_foes foelist
This uses the 'do' notation with the Robot monad, whose operations are closer 
to what the code is doing than the IO monad. Also, replacing 'return' with 
'let' removes the unnecessary reference to the monad.


More information about the Haskell-Cafe mailing list