Last active
September 2, 2017 17:30
-
-
Save wuct/bbcb21c33cff166fd12a82013e7ac200 to your computer and use it in GitHub Desktop.
PureScript by Example CH6 Exercises
This file contains 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 Complex where | |
import Prelude | |
import Data.Eq | |
newtype Complex = Complex | |
{ real :: Number | |
, imaginary :: Number | |
} | |
instance eqComplex :: Eq Complex where | |
eq (Complex { real: r1, imaginary: i1 }) (Complex { real: r2, imaginary: i2 }) = r1 == r2 && i1 == i2 | |
instance showComplex :: Show Complex where | |
show (Complex { real, imaginary }) = "(" <> show real <> ", " <> show imaginary <> ")" |
This file contains 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 Extended where | |
import Data.Ordering (Ordering(..)) | |
import Data.Ord | |
import Prelude | |
data Extended a = Finite a | Infinite | |
instance eqExtended :: Eq a => Eq (Extended a) where | |
eq Infinite Infinite = true | |
eq (Finite x) (Finite y) = eq x y | |
eq _ _ = false | |
instance ordExtended :: Ord a => Ord (Extended a) where | |
compare Infinite Infinite = EQ | |
compare Infinite _ = GT | |
compare _ Infinite = LT | |
compare (Finite x) (Finite y) = compare x y | |
This file contains 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 NonEmpty where | |
import Data.Array | |
import Data.Eq | |
import Data.Functor | |
import Data.Semigroup | |
import Prelude | |
data NonEmpty a = NonEmpty a (Array a) | |
instance eqNonEmpty :: Eq a => Eq (NonEmpty a) where | |
eq (NonEmpty e1 arr1) (NonEmpty e2 arr2) = e1 == e2 && arr1 == arr2 | |
instance semigroupNonEmpty :: Semigroup (NonEmpty a) where | |
append (NonEmpty e1 arr1) (NonEmpty e2 arr2) = NonEmpty e1 (append arr1 arr2) | |
instance functorNonEmpty :: Functor NonEmpty where | |
map f (NonEmpty e arr) = NonEmpty (f e) (map f arr) |
This file contains 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 Shape where | |
import Prelude | |
import Data.Hashable (hash) | |
data Point = Point | |
{ x :: Number | |
, y :: Number | |
} | |
data Shape | |
= Circle Point Number | |
| Rectangle Point Number Number | |
| Line Point Point | |
| Text Point String | |
instance showPoint :: Show Point where | |
show (Point { x, y }) = "(" <> show x <> ", " <> show y <> ")" | |
instance showShape :: Show Shape where | |
show (Circle point r) = "c = " <> show point <> ", r = " <> show r | |
show (Rectangle point w h) = "c = " <> show point <> ", w = " <> show w <> ", h = " <> show h | |
show (Line p1 p2) = "p1 = " <> show p1 <> "p2 = " <> show p2 | |
show (Text point string) = "point = " <> show point <> "text = " <> show string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment