Fish Touching🐟🎣

Haskell

Nov 3, 2023

# List

# String

list of characters

# Function

functionName :: types
functionName arg1 ... argN = body


-- File   : Example.hs
-- Module : Example

module Example where

increase :: Integer -> Integer
increase x = x + 1

# Pakages

# Control Flow

describe :: Integer -> String
describe n = case n of 0 -> "zero"
                       1 -> "one"
                       2 -> "an even prime"
                       n -> "the number " ++ show n
					   
-- parse country code into country name, returns Nothing if code not recognized
parseCountry :: String -> Maybe String
parseCountry "FI" = Just "Finland"
parseCountry "SE" = Just "Sweden"
parseCountry _ = Nothing

flyTo :: String -> String
flyTo countryCode = case parseCountry countryCode of Just country -> "You're flying to " ++ country
                                                     Nothing -> "You're not flying anywhere"

# Misc

# Maybe

TypeValues
Maybe BoolNothing, Just False, Just True
Maybe IntNothing, Just 0, Just 1, …
Maybe [Int]Nothing, Just [], Just [1,1337], …
Prelude> :t Nothing
Nothing :: Maybe a
Prelude> Just "a camel"
Just "a camel"
Prelude> :t Just "a camel"
Just "a camel" :: Maybe [Char]   -- the same as Maybe String
Prelude> Just True
Just True
Prelude> :t Just True
Just True :: Maybe Bool

# Either

-- right means correct
readInt :: String -> Either String Int
readInt "0" = Right 0
readInt "1" = Right 1
readInt s = Left ("Unsupported string: " ++ s)

# Constructor

Start with capital letter.