By Casey Allred
@sbditto85
How many of us know everything about electricity?
How many of us can flip a switch to turn on the lights?
https://xkcd.com/1312/
module Main where
main :: IO ()
main = putStrLn "Hello World"
module Main where
main :: IO ()
main = putStrLn "Hello World"
//CPP from https://en.wikibooks.org/wiki/C%2B%2B_Programming/Examples/Hello_world
#include <iostream>
int main()
{
std::cout
<< "Hello World!"
<< std::endl;
return 0;
}
module Main where
main :: IO ()
main = print (addInt 3 4)
addInt :: Int -> Int -> Int
addInt x y = x + y
addThree :: Int -> Int
addThree = addInt 3
module Main where
main :: IO ()
main = print (addInt 3 4)
addInt :: Int -> Int -> Int
addInt x y = x + y
--To call a function
x `addInt` y -- is infix
addInt x y -- is prefix
x + y -- is infix
(+) x y -- is prefix
addInt :: Int -> Int -> Int
addInt x y = x + y
int addInt(int x, int y) {
return x + y;
}
The formula for the Fibonacci sequence is:
Fn = F(n-1) + F(n-2)
with F(0) = 0 and F(1) = 1
module Main where
main :: IO ()
main = print (fibN 10)
fibN 0 = 0
fibN 1 = 1
fibN n = fibN (n-1) + fibN (n-2)
-- Output: 55
module Main where
main :: IO ()
main = print (fibN 10)
fibN :: (Eq a, Num a) => a -> a
fibN 0 = 0
fibN 1 = 1
fibN n = fibN (n-1) + fibN (n-2)
module Main where
main :: IO ()
main = print (fibN 10)
fibN i =
(head . drop i) fibs
fibs =
0 : 1 : zipWith (+) fibs (tail fibs)
funOne :: Int -> String
funTwo :: String -> Bool
combined :: Int -> Bool
combined x = (funTwo . funOne) x
module Main where
main :: IO ()
main = print (factorial 5)
factorial 1 = 1
factorial n = n * factorial (n - 1)
{-
OUTPUT: 120
-}
factorial 1 = 1
factorial n = n * factorial (n-1)
module Main where
main :: IO ()
main = print (factorial 5)
factorial n = product [1..n]
{-
OUTPUT: 120
-}
module Main where
main :: IO ()
main = print (factorial 5)
factorial :: (Enum a, Num a) => a -> a
factorial n = product [1..n]
{-
OUTPUT: 120
-}
module Main where
import Control.Monad
import Lib
main :: IO ()
main = forM_ fizzBuzz putStrLn
module Lib
( fizzBuzz
) where
fizzBuzz :: [ String ]
fizzBuzz =
map fizzInt [1..100]
where
fizzInt :: Int -> String
fizzInt i =
case (i `mod` 3 == 0, i `mod` 5 == 0) of
(True, True) -> "FizzBuzz"
(True, False) -> "Fizz"
(False, True) -> "Buzz"
(False, False) -> show i
Divisible by 3 put "Fizz"
Divisible by 5 put "Buzz"
Otherwise put the number
module Main where
import Control.Monad
import Lib
main :: IO ()
main = forM_ fizzBuzz putStrLn
module Lib
( fizzBuzz
, fizzInt
) where
fizzBuzz :: [ String ]
fizzBuzz =
map fizzInt [1..100]
where
fizzInt :: Int -> String
fizzInt i
| i `mod` 3 == 0 && i `mod` 5 == 0 = "fizzbuzz"
| i `mod` 5 == 0 = "buzz"
| i `mod` 3 == 0 = "fizz"
| otherwise = show i
module Main where
import Control.Monad
import Lib
main :: IO ()
main = forM_ fizzBuzz putStrLn
module Lib
( fizzBuzz
, fizzInt
) where
fizzBuzz :: [ String ]
fizzBuzz =
map fizzInt [1..100]
fizzInt :: Int -> String
fizzInt i
| isMod3 && isMod5 = "fizzbuzz"
| isMod5 = "buzz"
| isMod3 = "fizz"
| otherwise = show i
where
isMod3 :: Bool
isMod3 = i `mod` 3 == 0
isMod5 :: Bool
isMod5 = i `mod` 5 == 0
addInt :: Int -> Int -> Int
addInt x y = x + y -- addInt 1 2 == 3
lambdaAdd :: Int -> Int -> Int
lambdaAdd = \x y -> x + y -- lambdaAdd 1 2 == 3
map (\i -> i + 1) [1..4] -- [2, 3, 4, 5]
map (+1) [1..4] -- [2, 3, 4, 5]
type String = [ Char ]
type PeoplesNames = [ String ]
type Person = ( String, String, Int )
data Bool = True | False
data WeekDays = Monday | Tuesday | Wednesday | Thursday | Friday
data Person = Person String String Int
data Worker = Employee String String Int | Manager String String Int
getFirstName (Employee firstName _ _) = firstName
data Employee = Employee { getFirstName :: String
, getLastName :: String
, getAge :: Int
}
emp = Employee "Jonny" "Jon" 40
getFirstName emp -- "Jonny"
data Maybe a = Just a | Nothing
data [a] = a : [a] | []
data List a = Cons a (List a) | Empty
data Maybe a = Just a | Nothing
a = Just "hello" -- Maybe String
b = Just 5 -- Maybe Int
c = Nothing -- Maybe a
myFunc :: (Num a) => a -> a -> a
myFunc x y = x + y
class Num a where
(+) :: a -> a -> a
(-) :: a -> a -> a
(*) :: a -> a -> a
negate :: a -> a
abs :: a -> a
{-*SNIP*-}
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
data TrafficLight = Red | Yellow | Green
instance Eq TrafficLight where
Red == Red = True
Green == Green = True
Yellow == Yellow = True
_ == _ = False
areEq :: (Eq a) => a -> a-> Bool
areEq first second = first == second
When creating a type, if its composed of types that already implement a Type Class, you can then just derive that Type Class for your new type.
data Employee = Employee { getFirstName :: String
, getLastName :: String
, getAge :: Int
} deriving ( Show, Eq )
data List a = a : [a] | []
data List a = Cons a (List a) | EmptyList
[1, 2, 3, 4, 5]
1 : 2 : 3 : 4 : 5 : []
Cons 1 (Cons 2 (Cons 3 ( Cons 4 (Cons 5 EmtpyList))))
infinteList = [1..]
oddInfiniteList = [1,3..]
finiteList = take 10 infiniteList
otherFiniteList = (take 10) . (drop 5) oddInfiniteList
list = [1, 2, 3, 4]
newList = map (+1) list
int arr[] = {1, 2, 3, 4};
int newArr[] = {0, 0, 0, 0};
int size = 4;
for(int i = 0; i < size; i++) {
newArr[i] = arr[i] + 1;
}
list = [1, 2, 3, 4]
newList = filter (<3) list
vector<int> arr = {1, 2, 3, 4};
vector<int> newArr;
for(int i = 0; i < arr.size; i++) {
if(arr[i] < 3) {
newArr.push(arr[i]);
}
}
myList = [x | x <- [1..10] ] --[1,2,3,4,5,6,7,8,9,10]
myList = [x + 1 | x <- [1..10] ] --[2,3,4,5,6,7,8,9,10,11]
myList = [x | x <- [1..10], x < 3] --[1,2]
myList = [x + 1 | x <- [1..10], x < 3] --[2,3]
class Functor f where
fmap :: (a -> b) -> f a -> f b
Apply a function to a computational context
myFunc :: Int -> Bool
mayExistInt :: Maybe Int
-- How do I use myFunc with mayExistInt?
result = fmap myFunc mayExistInt
result = myFunc <$> mayExistInt
-- which will result in
result :: Maybe Bool
Why do we care?
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
Apply a function in a context to a context
myFunc :: Int -> String -> Bool
mayExistInt :: Maybe Int
mayExistString :: Maybe String
-- How do I use myFunc with mayExistInt and mayExistString?
result = fmap myFunc mayExistInt <*> mayExistString
result = myFunc <$> mayExistInt <*> mayExistString
-- which will result in
result :: Maybe Bool
Why do we care?
What if I have a value in a context but the function takes a normal value and return a value in a context?
class Applicative m => Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
-- Applicative
pure :: a -> m a
What do you think the following program does?
module Main where
main :: IO ()
main = do
hello <- return "hello"
world <- return "world"
print (hello ++ world)
myFunc :: Int -> Maybe Bool
mayExistInt :: Maybe Int
-- How do I use myFunc with mayExistInt?
result = mayExistInt >>= myFunc
result = do
i <- mayExistInt
myFunc i
-- which will result in
result :: Maybe Bool
Why do we care?
instance Monad Maybe where
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) (Just a) f = f a
(>>=) Nothing _ = Nothing
mayExistInt >>= myFunc
-- if mayExistInt == Nothing :
Nothing
-- if mayExistInt == Just x then f == myFunc and a == x :
myFunc x :: Maybe Bool
Whats happening behind the Maybe >>= curtain?
getLine :: IO String -- from the prelude
myFunc :: String -> IO Bool
myFunc2 :: Bool -> IO String
putStrLn :: String -> IO () -- from the prelude
result = do inputString <- getLine
myBool <- myFunc inputString
computeString <- myFunc2 myBool
putStrLn computeString
-- which will result in
result :: IO () --and the appropriate IO actions
Why do we care?