Created
October 2, 2018 05:35
-
-
Save gregberns/454aa491b0ff67706fda4dc6dba7cf2a to your computer and use it in GitHub Desktop.
Intro Syntax for Haskell
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
{-# OPTIONS_GHC -fwarn-incomplete-patterns -Werror #-} | |
-- Show | |
-- p1 = | |
-- ... | |
-- method(f,f) | |
-- ... | |
-- p2 = | |
-- ... | |
-- r = f | |
-- method(f, f) | |
--After this...you will know Haskell | |
-- x is of type integer | |
x :: Integer | |
x = 99 | |
f :: Integer -> Integer | |
f a = a + 77 | |
--inline function - lambda | |
--given 'a' | |
g = \a -> a + 77 | |
--h actually takes one argument | |
--note parens - they represent a function | |
h :: Integer -> (Integer -> Integer) | |
h i j = (i + j) * 2 | |
i :: (Integer -> Integer) -> Integer | |
i k = k 66 | |
-- i (\w -> w + 2) | |
(66 + 77) | |
ii :: (Integer -> x) -> x | |
ii k = k 66 | |
-- i (\w -> [ w, w ]) | |
--prefix and infix | |
-- h 1 2 | |
--or | |
-- 1 `h` 2 | |
(.+.) :: Integer -> (Integer -> Integer) | |
(.+.) i j = (i + j) * 2 | |
j :: p -> p | |
j x = x | |
--data type created with keyword `data` | |
pie = 3 | |
--shape is an algebraic data type | |
data Shape = | |
Rectangle Integer Integer | |
| Square Integer | |
| Circle Integer | |
-- Square 22 -- error | |
-- Rectangle 1 2 == Rectangle 1 2 -- error | |
deriving (Show, Eq) | |
--theres tabs, spaces.... and haskell (spaces) | |
perimeter2 (Rectangle w h) = (w + h) * 2 | |
perimeter2 (Square x) = x * 4 | |
perimeter2 (Circle r) = r * 2 * pie | |
-- case analysis also known as pattern matching | |
perimeter :: Shape -> Integer | |
perimeter = \s -> case s of | |
Rectangle w h -> (w + h) * 2 | |
Square x -> x * 4 | |
Circle r -> r * 2 * pie | |
data OneOrTwo a = One a | Two a a | |
deriving (Eq, Show) | |
--One (ii (\n -> [n,n])) | |
-- recursive algebraic data type | |
-- this is because the definition refers to itself | |
data Natural = Zero | Successor Natural | |
deriving (Eq, Show) | |
one = Successor Zero | |
two = Successor one | |
three = Successor two | |
add :: Natural -> Natural -> Natural | |
add Zero y = y | |
add (Successor x) y = Successor (add x y) | |
--80% of haskell known | |
-- :i is the WFT | |
--things that implement this | |
class Orrd x where | |
lt :: x -> x -> Bool | |
instance Orrd Natural where | |
-- lt :: Natural -> Natural -> Bool | |
lt Zero (Successor _) = True | |
lt (Successor _) Zero = False | |
lt Zero Zero = False | |
lt (Successor x) (Successor y) = lt x y | |
--need to require a to have Ord as a 'property' of its type | |
max3 :: Orrd a => a -> a -> a -> a | |
max3 a b c = | |
if a `lt` b | |
then | |
if b `lt` c | |
then c | |
else b | |
else | |
if a `lt` c | |
then c | |
else a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment