Chapter 14d - Haskell. A number of recent developments in functional programming are not well-captured by the traditional languages, Common Lisp and Scheme. In this section, we introduce a more modern functional language, Haskell, whose features signal more clearly the present and future directions in functional programming research and applications. | Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Contents Functions and the Lambda Calculus Scheme Haskell Introduction Expressions Lists and List Comprehensions Elementary Types and Values Control Flow Defining Functions Tuples Example: Semantics of Clite Example: Symbolic Differentiation Example: Eight Queens Example: Semantics of Clite Program state can be modeled as a list of pairs. type State = [(Variable, Value)] type Variable = String type Value = Intval Integer | Boolval Bool deriving (Eq, Ord, Show) ., [(“x”, (Intval 1)), (“y”,(Intval 5))] Function to retrieve the value of a variable from the state: get var (s:ss) | var == (fst s) = snd s | otherwise = get var ss State transformation Function to store a new value for a variable in the state: onion :: Variable -> Value -> State -> State onion var val (s:ss) | var == (fst s) = (var, val) : ss | otherwise = s : (onion var val ss) ., onion ‘y’ (Intval 4) [(‘x’, (Intval 1)), (‘y’, (Intval 5))] = (‘x’, (Intval 1)) : onion ‘y’ (Intval 4) [(‘y’, (Intval 5))] = [(‘x’, (Intval 1)), (‘y’, (Intval 4))] Modeling Clite Abstract Syntax data Statement = Skip | Assignment Target Source | Block [ Statement ] | Loop Test Body | Conditional Test Thenbranch Elsebranch deriving (Show) type Target = Variable type Source = Expression type Test = Expression type Body = Statement type Thenbranch = Statement type Elsebranch = Statement Semantics of Statements A statement transforms a state to another state: m :: Statement -> State -> State Skip is easy! m (Skip) state = state Assignments aren’t too bad either: m (Assignment target source) state = onion target (eval source state) state Loops and Conditionals Loops are a bit trickier: m (Loop t b) state | (eval t state) == (Boolval . | Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Contents Functions and the Lambda Calculus Scheme Haskell Introduction Expressions Lists and List Comprehensions Elementary Types and Values Control Flow Defining Functions Tuples Example: Semantics of Clite Example: Symbolic Differentiation Example: Eight Queens Example: Semantics of Clite Program state can be modeled as a list of pairs. type State = [(Variable, Value)] type Variable = String type Value = Intval Integer | Boolval Bool deriving (Eq, Ord, Show) ., [(“x”, (Intval 1)), (“y”,(Intval 5))] Function to retrieve the value of a variable from the state: get var (s:ss) | var == (fst s) = snd s | otherwise = get var ss State transformation Function to store a new value for a variable .