Skip to content

Instantly share code, notes, and snippets.

@nelsonni
Created May 21, 2015
Embed
What would you like to do?
HW4.hs
-- Hugh McDonald (931-930-585)
-- Nick Nelson (931-242-585)
-- Faith Steltzer (931-811-544)
--
-- CS 381: Homework 4
-- 5.21.15
--
module HW4 where
import MiniMiniLogo
import Render
--
-- * Semantics of MiniMiniLogo
--
-- NOTE:
-- * MiniMiniLogo.hs has the definitions of Mode, Cmd, and Prog!
-- * Render.hs has the definitions of Point and Line!
-- | A type to represent the current state of the pen.
type State = (Mode,Point)
-- | The initial state of the pen.
start :: State
start = (Up,(0,0))
-- | A function that renders the image to HTML. Only works after you have
-- implemented `prog`. Applying `draw` to a MiniMiniLogo program will
-- produce an HTML file named MiniMiniLogo.html, which you can load in
-- your browser to view the rendered image.
draw :: Prog -> IO ()
draw p = let (_,ls) = prog p start in toHTML ls
-- Semantic domains:
-- * Cmd: State -> (State, Maybe Line)
-- * Prog: State -> (State, [Line])
-- | Semantic function for Cmd.
--
-- >>> cmd (Pen Down) (Up,(2,3))
-- ((Down,(2,3)),Nothing)
--
-- >>> cmd (Pen Up) (Down,(2,3))
-- ((Up,(2,3)),Nothing)
--
-- >>> cmd (Move 4 5) (Up,(2,3))
-- ((Up,(4,5)),Nothing)
--
-- >>> cmd (Move 4 5) (Down,(2,3))
-- ((Down,(4,5)),Just ((2,3),(4,5)))
--
cmd :: Cmd -> State -> (State, Maybe Line)
cmd (Pen Up) (_,ls) = ((Up, ls), Nothing)
cmd (Pen Down) (_,ls) = ((Down, ls), Nothing)
cmd (Move x' y') (Down, (x, y)) = ((Down, (x', y')), Just ((x, y), (x', y')))
cmd (Move x' y') (Up, (x, y)) = ((Up, (x', y')), Nothing)
-- | Semantic function for Prog.
--
-- >>> prog (nix 10 10 5 7) start
-- ((Down,(15,10)),[((10,10),(15,17)),((10,17),(15,10))])
--
-- >>> prog (steps 2 0 0) start
-- ((Down,(2,2)),[((0,0),(0,1)),((0,1),(1,1)),((1,1),(1,2)),((1,2),(2,2))])
prog :: Prog -> State -> (State, [Line])
prog cs state = (progstate cs state, proglines cs state)
proglines :: Prog -> State -> [Line]
proglines [] state = []
proglines (c:cs) state = case cmd c state of
(state', Just l) -> l:(proglines cs state')
(state', Nothing) -> (proglines cs state')
progstate :: Prog -> State -> State
progstate [] state = state
progstate (c:cs) state = case cmd c state of
(state', _) -> progstate cs state'
--
-- * Extra credit
--
-- | This should be a MiniMiniLogo program that draws an amazing picture.
-- Add as many helper functions as you want.
amazing :: Prog
amazing = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment