Skip to content

Instantly share code, notes, and snippets.

@m4c1ek
Last active August 29, 2015 14:27
Show Gist options
  • Save m4c1ek/5d27dca973d705139b78 to your computer and use it in GitHub Desktop.
Save m4c1ek/5d27dca973d705139b78 to your computer and use it in GitHub Desktop.
haskell 2

Haskell has type inference

Haskell is statically typed

:t 'a'
:t 98
:t "asd"
['a','b'] == "ab"
:t (True, 'a')

<-- String is the same as [Char] -->
removeNonUppercase :: String -> String
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]

addThree :: Int -> Int -> Int -> Int  
addThree x y z = x + y + z

<-- Int is not the same as Integer -->
factorial :: Integer -> Integer  
factorial n = product [1..n]

Int, Integer, Float, Double, Char, String, [Char], Bool
<-- Like Interfaces, Protocols -->
:t (==)
(==) :: (Eq a) => a -> a -> Bool
:t elem
:t (>)

Eq
"Ho Ho" == "Ho Ho"
3.432 == 3.432

Ord
"Abrakadabra" < "Zebra"
"Abrakadabra" `compare` "Zebra"
5 >= 2
5 `compare` 3

<-- Conforming to a Show typeclass -->
Show
show 3
show 5.3333
show True

Read
read "True" || False
read "8.2" + 3.8
read "[1,2,3,4]" ++ [3] 
<-- Why does this not work? -->
read "4"
:t read

Enum
<-- use in list ranges or with succ, pred -->
[LT .. GT]
succ 'B'

Bounded
<-- have an upper and lower bound -->
minBound :: Int
<-- check the type of minBound -->
:t minBound
maxBound :: (Bool, Int, Char)

Num
<-- can act like a number -->
:t 20
20 :: Float
20.0 :: Int
:t (*)
:t (+)

<-- includes only integral (whole) numbers. In this typeclass are Int and Integer -->
Integral
<-- notice several class constraints in the fromIntegral type -->
:t fromIntegral
<-- this resolves in an error -->
length [1,2,3,4] + 3.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment