Created
January 2, 2019 10:05
-
-
Save DonaldKellett/d08926f2a4ceb67ef3f480a615b5b726 to your computer and use it in GitHub Desktop.
PureScript by Example - 5.14 Record Puns - Exercise 1-3 Solutions
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 RecordPuns where | |
import Prelude | |
import Data.Maybe | |
-- Utilities provided in 5.12 Algebraic Data Types of PureScript by Example | |
data Shape | |
= Circle Point Number | |
| Rectangle Point Number Number | |
| Line Point Point | |
| Text Point String | |
data Point = Point | |
{ x :: Number | |
, y :: Number | |
} | |
-- Exercise 1 | |
myShape :: Shape | |
myShape = Circle (Point { x: 0.0, y: 0.0 }) 10.0 | |
-- Exercise 2 | |
doubleSize :: Shape -> Shape | |
doubleSize (Circle (Point { x, y }) r) = | |
Circle (Point { x: 2.0 * x, y: 2.0 * y }) (2.0 * r) | |
doubleSize (Rectangle (Point { x, y }) l w) = | |
Rectangle (Point { x: 2.0 * x, y: 2.0 * y }) (2.0 * l) (2.0 * w) | |
doubleSize (Line (Point { x: x1, y: y1 }) (Point { x: x2, y: y2 })) = | |
Line (Point { x: 2.0 * x1, y: 2.0 * y1 }) (Point { x: 2.0 * x2, y: 2.0 * y2 }) | |
doubleSize (Text (Point { x, y }) s) = | |
Text (Point { x: 2.0 * x, y: 2.0 * y }) s | |
-- Exercise 3 | |
textFromShape :: Shape -> Maybe String | |
textFromShape (Text _ s) = Just s | |
textFromShape _ = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment