Created
January 2, 2019 13:53
-
-
Save DonaldKellett/b34db8d4bf18495cacf8d0e51e57f9a6 to your computer and use it in GitHub Desktop.
PureScript by Example - 6.3 Show Me! - Exercise 1 Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ShowMe where | |
import Prelude | |
-- Utilities from https://github.com/paf31/purescript-book/blob/master/exercises/chapter5/src/Data/Picture.purs | |
-- required to complete this exercise | |
data Point = Point | |
{ x :: Number | |
, y :: Number | |
} | |
showPoint :: Point -> String | |
showPoint (Point { x, y }) = | |
"(" <> show x <> ", " <> show y <> ")" | |
data Shape | |
= Circle Point Number | |
| Rectangle Point Number Number | |
| Line Point Point | |
| Text Point String | |
showShape :: Shape -> String | |
showShape (Circle c r) = | |
"Circle [center: " <> showPoint c <> ", radius: " <> show r <> "]" | |
showShape (Rectangle c w h) = | |
"Rectangle [center: " <> showPoint c <> ", width: " <> show w <> ", height: " <> show h <> "]" | |
showShape (Line start end) = | |
"Line [start: " <> showPoint start <> ", end: " <> showPoint end <> "]" | |
showShape (Text loc text) = | |
"Text [location: " <> showPoint loc <> ", text: " <> show text <> "]" | |
-- Just for the sake of completeness ... | |
instance showPoint' :: Show Point where | |
show = showPoint | |
-- Exercise 1 | |
instance showShape' :: Show Shape where | |
show = showShape |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment