Com S 541 Lecture -*- Outline -*- > module BuiltInTypes where > import Prelude hiding (fst, snd, head, tail, filter, zip) * Built-in types of Haskell ad: these are needed for all programming in Haskell also similar to the types used in semantics effect: understand built-in types and their operations go quickly, however ** Fundamental classification of objects *** simple (atomic) types (Thompson 3.1-2, 3.5-6, Davie 2.7) Bool, Char, and Integer also types Int (machine ints), Float, Double, ... Tell them to read about the following, skip to next section ------------------------------------------ HASKELL BOOLEANS Bool Values: + abstract values: true and false + printed: True, False Operations: + constructors: True, False + functions: &&, ||, not, ==, /= + syntax: if _ then _ else _ HASKELL CHARACTERS Char Values: + abstract values: a, b, c, d, ... + printed: 'a', 'b', 'c', ... Operations: + constructors: 'a', 'b', ..., '\n', ... + functions: ord, chr, isSpace, .. ==, /=, <, <=, ... ------------------------------------------ && and || are short-circuit Bool is of the class Eq, hence it has == and /= (actually it's also a member of many other classes, e.g., Ord, so False < True) look in the Prelude.hs file to see the definition; search for "Boolean type" also the syntax if _ then _ else is special for Bool use C style escapes for chars Char is a member of the classes Eq and Ord ------------------------------------------ HASKELL INTEGERS Integer Values: + abstract values: 0, 1, -1, ... + printed: 0, 1, -1, ... Operations: + constructors: 0, 1, 2, 3, ... + functions: +, -, *, negate, abs, signum, quot, rem, div, mod, ==, /=, <, <=, ... ------------------------------------------ Integer is a member of the classes Eq, Ord, Num, Real, Integral, and a bunch of others Float literals must have digits on both sides of the decimal point *** structured types (Thompson 5, Davie 2.8, 3.11, 2.10) make a table as discuss the following: type constructors ____________________ (a,b) (,) [a] [], : a -> b \ -> **** pairs, tuples, and unit (Thompson 5.2, Davie 2.10) ------------------------------------------ TUPLES IN HASKELL (a,b), (a,b,c), ..., and () Values: + abstract values: pairs of a & b, triples of a & b & c, ... an empty tuple + printed: (1,True), (3, 4, 5), () Operations: + constructor (,), (,,), ... + fst, snd EXAMPLE FUNCTIONS OVER TUPLES > fst :: (a,b) -> a > fst (a,_) = a > snd :: (a,b) -> b > snd (_,b) = b ------------------------------------------ there is a way to define record-like types in Haskell also... ------------------------------------------ CONSTRUCTING TUPLES Prelude> (1,True) Prelude> (1,2,3) Prelude> (1,(2,3)) Prelude> (1,(True,2.8)) Prelude> ((1,True),2.8 + 4) Prelude> (1) Prelude> () Prelude> ("zero tuple:",()) ------------------------------------------ Q: What is the type of each? so ( , ) makes pairs, (,,) makes triples, etc. Why this notation? idea is that in a functional language want only 1 argument and result for functions f(x,y) is interpreted as f applied to the pair (x,y) **** functions (Thompson 10) ------------------------------------------ FUNCTIONS a -> b Values: + abstract values: partial functions from a to b Operations: + constructor: \ var -> expression + syntax: f x y = expression means roughly f = \x -> \y -> expession + functions: (.), flip, curry, uncurry Examples: id :: a -> a id = \x -> x (.) :: (b -> c) -> (a -> b) -> (a -> c) (f . g) x = f (g x) flip :: (a -> b -> c) -> b -> a -> c flip f x y = f y x ------------------------------------------ Note: id x = x is shorthand for the above definition \x -> x is just like Smalltalk's [:x | x] ** binding, pattern matching, simple functions (Thompson p. 74, chapter 7, Davie 2.9, 3.5) Generally, use pattern matching to extract parts of (algebraic) types ------------------------------------------ PATTERN MATCHING AND BINDING Prelude> let (x,y,z) = (1,2,3) in x Prelude> let (x,y,z) = (1,2,3) in z Prelude> let (_,y,_) = (1,2,3) in y Prelude> let (a:as) = 1:2:3:[] in a 1 Prelude> let (a:as) = [1,2,3] in as [2,3] ------------------------------------------ Q: What's the general rule for this kind of pattern matching? ------------------------------------------ PATTERNS IN FUNCTION DEFINITION Suppose we define > yodaize (subject, verb, adjective) = > (adjective, subject, verb) Then we have Prelude> yodaize ("food", "is", "good") Prelude> yodaize ("study", "you", "will") Another example: Problem: write a function to take max of 3 arguments ------------------------------------------ ... ("good","food","is") :: ([Char],[Char],[Char]) ... ("will","study","you") :: ([Char],[Char],[Char]) > max3 :: Ord a => (a, a, a) -> a > max3 (x,y,z) = (max x (max y z)) Can omit the following if they're getting it... ------------------------------------------ FOR YOU TO DO 1. Define functions fst3 :: (a, b, c) -> a snd3 :: (a, b, c) -> b thd3 :: (a, b, c) -> c such that for all t :: (a, b, c) t = (fst3 t, snd3 t, thd3 t) 2. Define a function average :: (Float, Float) -> Float such that, for example average (1.0, 3.0) = 2.0 average (3.0, 50.0) = 26.5 ------------------------------------------