[Haskell-beginners] return (user defined type) confusion?

Lorenzo Bolla lbolla at gmail.com
Thu Apr 12 17:30:36 CEST 2012


It has to do with the scope of "where" in your mainloop.
See comments inline.

On Thu, Apr 12, 2012 at 04:08:10PM +0100, umptious wrote:
> 
> 
> import Data.Map as Map
>    
> data Pt2 = Pt2 {x::Float, y::Float} deriving (Show, Read)
> 
> data Shape =   Circle   {origin::Pt2, radius::Float}
>              | Square   {origin::Pt2, side  ::Float}
>              | Rect     {origin::Pt2, other ::Pt2}
>                deriving (Show, Read)
> 
> enterCircle :: IO(Shape)
> enterCircle = return (r) -- needs to rtn io (shape) because will take input
> here
>        where r = Circle{origin=Pt2{x=1,y=2}, radius=4}
>             
> 
> mainLoop :: [Shape] -> IO (String)           
> mainLoop shapes = do
>     putStrLn "?"
>     cmd <- getLine
>     if cmd=="q"
>       then
>         return ("done")
>       else do
>         y <- enterCircle

This "y" shades the Pt2 accessor "y".

>         mainLoop shapes'
>     where shapes' = y : shapes

This where thinks that "y" is actually the Pt2 accessor "y".

An easy fix would be to rewrite this line as:
>         mainLoop $ y: shapes

Or mess with tabs to get the right scope for "where"...

hth,
L.


-- 
Lorenzo Bolla
http://lbolla.info



More information about the Beginners mailing list