import Text.CSV import Graphics.Gloss data Command = Go Float | TurnLeft | TurnRight | Say String deriving Show toCommand ["GO",n] = Go (read n) toCommand ["LEFT"] = TurnLeft toCommand ["RIGHT"] = TurnRight toCommand ["SAY",something] = Say something toCommand record = error $ "Can not parse record " ++ show record {- 1. try commandsToPicture = pictures . map toPicture toPicture (Go length) = line [(0,0), (0, length)] toPicture TurnLeft = blank toPicture TurnRight = blank toPicture (Say something) = scale 0.3 0.3 (text something) -} {- 2. try commandsToPicture [] = blank commandsToPicture (Go length : cmds) = pictures [ line [(0,0), (0, length)] , translate 0 length (commandsToPicture cmds) ] commandsToPicture (TurnLeft : cmds) = rotate (-90) (commandsToPicture cmds) commandsToPicture (TurnRight : cmds) = rotate 90 (commandsToPicture cmds) commandsToPicture (Say something : cmds) = pictures [ scale 0.3 0.3 (text something) , commandsToPicture cmds ] -} {- 3. try commandsToPicture [] = blank commandsToPicture ((n,Go length) : cmds) = annotate n $ pictures [ line [(0,0), (0, length)] , translate 0 length (commandsToPicture cmds) ] commandsToPicture ((n,TurnLeft) : cmds) = annotate n $ rotate (-90) (commandsToPicture cmds) commandsToPicture ((n,TurnRight) : cmds) = annotate n $ rotate 90 (commandsToPicture cmds) commandsToPicture ((n,Say something) : cmds) = annotate n $ pictures [ scale 0.3 0.3 (text something) , commandsToPicture cmds ] -} annotate n picture = pictures [ scale 0.1 0.1 (text (show n)) , picture ] commandsToPicture = toPicture commandToPicture toPicture conv [] = blank toPicture conv ((n,x):xs) = annotate n (conv x (toPicture conv xs)) commandToPicture (Go length) pics = pictures [ line [(0,0), (0, length)] , translate 0 length pics ] commandToPicture TurnLeft pics = rotate (-90) pics commandToPicture TurnRight pics = rotate 90 pics commandToPicture (Say something) pics = pictures [ scale 0.3 0.3 (text something) , pics ] main = do csvOrError <- parseCSVFromFile "pen.csv" case csvOrError of Left error -> print error Right records -> displayInWindow "Pen demo" (400, 400) (800, 300) white $ commandsToPicture $ zip [1..] $ map toCommand $ records