(2A)
Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.
Based on Haskell Tutorial, Medak & Navratil ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf Yet Another Haskell Tutorial, Daume https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf 3
User Defined Types (1) Type Constructor data Bool = True False A nullary constructor: takes no arguments Data Constructor A multi-constructor The type being defined here is Bool, and it has exactly two values: True and False. data Color = Red Green Blue Type Constructor Data Constructor data Point a = Pt a a A unary constructor (one argument a) A single constructor Pt :: a -> a -> Point a Pt 2.0 3.0 Pt 'a' 'b' Pt True False :: Point Float :: Point Char :: Point Bool https://www.haskell.org/tutorial/goodies.html 4
User Defined Types (2) data Polynom = Poly Float Float Float data the keyword Polynom the name of the data type Poly the constructor function (:t Poly) Float the three arguments to the Poly constructor 5
User Defined Types (3) data Polynom = Poly Float Float Float roots2 :: Polynom -> (Float, Float) roots (Poly a b c) = function definition p1, p2 :: Polynom P1 = Poly 1.0, 2.0, 3.0 P2 = Poly 1.0, 3.0, (-5.0) 6
Recursive Definition of Lists data [a] = [ ] a : [a] Type Constructor Data Constructor Any type is ok but The type of every element in the list must be the same List = [ ] (a : List) an empty list a list with at least one element [ ] (x:xs) https://www.haskell.org/tutorial/goodies.html 7
Parameterized Data Types Parameter data List a = L a (List a) Empty L1, L2, L3 :: List Integer L1 = Empty L2 = L 1 L1 L3 = L 5 L2 L4 = L 1.5 Empty :: List Double Constructor a (a) 8
Polymorphic Type types that are universally quantified in some way over all types essentially describe families of types (forall a) [a] is the family of types consisting of, for every type a, the type of lists of a. lists of integers (e.g. [1,2,3]) lists of characters (['a','b','c']) lists of lists of integers, etc. [2,'b'] is not a valid example https://www.haskell.org/tutorial/goodies.html 9
Subset Polymorphism roots :: (Floating a) => (a, a, a) -> (a, a) https://www.haskell.org/tutorial/goodies.html 10
Parameterized Polymorphism plus :: a -> a -> a, plus :: Int -> Int -> Int, plus :: Rat -> Rat -> Rat, data List a = L a (List a) Empty listlen :: List a -> Int listlen Empty = 0 listlen (L _ list) = 1 + listlen list https://www.haskell.org/tutorial/goodies.html 11
Counting functions l1 [] = 0 l1 (x:xs) = 1 + l1 xs l2 xs = if xs == [] then 0 else 1 + l2 (tail xs) l3 xs xs == [] = 0 otherwise = 1 + l3 (tail xs) l4 = sum. map (const 1) l5 xs = foldl inc 0 xs where inc x _ = x+1 l6 = foldl (\n _ -> n + 1) 0 Pattern Matching if-then-else guard notation replace and sum local function, local counter lambda expression 12
Guard Notation sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 https://www.haskell.org/tutorial/patterns.html if (x > 0) sign = +1; else if (x == 0) sign = 0; else sign= -1 13
Anonymous Function Input Prompt> (\x -> x + 1) 4 5 :: Integer Prompt> (\x y -> x + y) 3 5 8 :: Integer addone = \x -> x + 1 λ x x 2 \ x -> x*x In Lambda Calculus Lambda Expression Lambda Abstraction Anonymous Function https://www.haskell.org/tutorial/patterns.html 14
Naming a Lambda Expression Input inc x = x + 1 inc = \x -> x + 1 add x y = x + y add = \x y -> x + y Lambda Expression https://www.haskell.org/tutorial/patterns.html 15
Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined by a certain formal syntax, and a set of transformation rules, which allow manipulation of the lambda terms. These transformation rules can be viewed as an equational theory or as an operational definition. All functions in the lambda calculus are anonymous functions, having no names. They only accept one input variable, with currying used to implement functions with several variables. 16
Composite Function (1) (.) :: (b -> c) -> (a -> b) -> (a -> c) f g f(g(x)) https://www.haskell.org/tutorial/patterns.html f. g = \ x -> f (g x) (f. g) x = f (g x) 17
Composite Function (2) p1 = (1.0,2.0,1.0) :: (Float, Float, Float) p2 = (1.0,1.0,1.0) :: (Float, Float, Float) ps = [p1,p2] newps = filter real ps rootsofps = map roots newps RootsOfPs2 = (map roots. filter real) ps 18
Local Variables lend amt bal = let reserve = 100 newbal = bal - amt in if bal < reserve then Nothing else Just newbal lend2 amt bal = if amt < reserve * 0.5 then Just newbal else Nothing where reserve = 100 newbal = bal - amt http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 19
Local Function pluralise :: String -> [Int] -> [String] pluralise word counts = map plural counts where plural 0 = "no " ++ word ++ "s" plural 1 = "one " ++ word plural n = show n ++ " " ++ word ++ "s" http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 20
Local Function roots :: (Float, Float, Float) -> (Float, Float) type PolyT = (Float, Float, Float) type RootsT = (Float, Float) roots :: PolyT -> RootsT typedef http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 21
Infix operators as functions Input (x+) = \y -> x + y Input (+y) = \x -> x + y partial application of an infix operator Inputs (+) = \x y -> x + y https://www.haskell.org/tutorial/functions.html 22
Infix operators as function values inc = (+1) add = (+) map (+) [1,2,3] [(+1),(+2),(+3)] https://www.haskell.org/tutorial/functions.html 23
Sections (+), (*) :: Num a => a -> a -> a? 3 + 4? (+) 3 4 (+) :: Num a => a -> a -> a (*) :: Num a => a -> a -> a a belongs to the Num type class http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 24
Sections (3 +) :: Num a => a -> a? map (3 +) [4, 7, 12]? filter (==2) [1,2,3,4] [2]? filter (<=2) [1,2,3,4] [1,2] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 25
List Comprehensions [x x <- [0..100], odd x] {x x [0,100],odd x}? f [2,3,4] [5,6] where f xs ys = [x*y x <- xs, y <- ys] [10,12,15,18,20,24] :: [Integer] 2 5 3 6 4 http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 26
Map as a list comprehension map f xs = [ f x x <- xs] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 27
Filter as a list comprehension filter p xs = [x x <- xs, p x] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 28
Functional & Imperative Programming c := 0 for I:=1 to n do c := c + a[i] * b[i] a belongs to the Num type class inn2 :: Num a => ([a], [a]) -> a inn2 = foldr (+) 0. map (uncurry (*)). uncurry zip uncurry zip map (uncurry (*)) foldr (+) 0 http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 29
Type Class Defining a type class by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class https://en.wikipedia.org/wiki/type_class 30
Type Class Definition a type class Eq intended to contain types that admit equality would be declared in the following way: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool type a belongs to type class Eq if functions named (==) and (/=) are defined https://en.wikipedia.org/wiki/type_class 31
Class Constraint define a function elem to determines if an element is in a list elem :: (Eq a) => a -> [a] -> Bool elem y [] = False elem y (x:xs) = (x == y) elem y xs the function elem has the type a -> [a] -> Bool with the context (Eq a), constrains the types which a can range over to those a which belong to the Eq type class (Note: Haskell => can be called a 'class constraint'.) https://en.wikipedia.org/wiki/type_class 32
References [1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf [2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf