Download presentation

Presentation is loading. Please wait.

Published byBartholomew Alexander Modified over 2 years ago

1
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions

2
5/10/20152 Contents Programming examples 1. writing an evaluator for arithmetic expressions ä using algebraic types and recursion 2. lists as functions ä using higher order functions

3
5/10/20153 Programming Example 1 Writing an evaluator for arithmetic expressions Motivation: ä a common style of programming ä a good example of using algebraic types and recursion

4
5/10/20154 Specification Write a Miranda program that: takes as input a representation of a simple arithmetic expression using the grammar given on the next page, and returns as output the value of that expression

5
5/10/20155 Grammar (BNF) expression :: constant | expression op expression | expression op expression | ‘(‘ expression ‘)’ ‘(‘ expression ‘)’ op :: ‘*’ | ‘+’ | ‘-’ | ‘/’

6
5/10/20156 Preliminaries Note that the specification asks for a “representation” of the expression to be input Therefore, we can ignore lexical analysis We will also assume that parsing has been done, so we have the syntax tree We can choose an algebraic type as our representation of that syntax tree, with appropriate constructors

7
5/10/20157 Algebraic types expression ::= Constant num | App expression operator expression | App expression operator expression | Bracketed expression Bracketed expression operator ::= Times | Plus | Minus | Divide

8
5/10/20158 Compare with the BNF: expression :: constant | expression op expression | expression op expression | ‘(‘ expression ‘)’ ‘(‘ expression ‘)’ op :: ‘*’ | ‘+’ | ‘-’ | ‘/’

9
5/10/20159 Test values || (4) * 5 test1 = App (Bracketed (Constant 4)) Times (Constant 5) || (4 + 5) * (3 – 2) test2 = App (Bracketed (App (Constant 4) Plus (Constant 5))) (Bracketed (App (Constant 4) Plus (Constant 5))) Times Times (Bracketed (App (Constant 3) Minus (Constant 2))) (Bracketed (App (Constant 3) Minus (Constant 2)))

10
5/10/201510 Evaluator code eval :: expression -> num eval (Constant x) = x eval (App e1 op e2) = evalop op (eval e1) (eval e2) eval (Bracketed e) = eval e evalop:: operator -> num -> num -> num evalop Times x y = x * y evalop Plus x y = x + y evalop Minus x y = x – y evalop Divide x y = x / y

11
5/10/201511 Programming Example 2 Lists as functions Lists as functions Motivation: Motivation: Better understanding of, and facility with, higher order functions and currying Better understanding of, and facility with, higher order functions and currying Example of how data can be implemented as a function (a “trick” - sometimes useful) Example of how data can be implemented as a function (a “trick” - sometimes useful)

12
5/10/201512 Preamble (1) Recall CURRIED functions: Recall CURRIED functions: f a b c = ( a + b ) f a b c = ( a + b ) Can help to think of binary tree giving syntax of the function applied to its arguments: Can help to think of binary tree giving syntax of the function applied to its arguments: @ @ @ b f a c

13
5/10/201513 Each @ node applies a function to an argument (f a) must be a function that can be applied to argument b Preamble (1) Recall CURRIED functions: Recall CURRIED functions: f a b c = ( a + b ) f a b c = ( a + b ) Can help to think of binary tree giving syntax of the function applied to its arguments : Can help to think of binary tree giving syntax of the function applied to its arguments : @ @ @ b f a c

14
5/10/201514 Each @ node applies a function to an argument (f a) must be a function that can be applied to argument b Preamble (1) Recall CURRIED functions: Recall CURRIED functions: f a b c = ( a + b ) f a b c = ( a + b ) Can help to think of binary tree giving syntax of the function applied to its arguments : Can help to think of binary tree giving syntax of the function applied to its arguments : @ @ @ b f a c Recall that curried functions can be partially applied, e.g. (f 3 4) is a function of type * -> num

15
5/10/201515 Preamble (2) Recall HIGHER ORDER functions: Recall HIGHER ORDER functions: f a b c = c (a b) f a b c = c (a b) c must be a function (of at least one argument) c must be a function (of at least one argument) a must be a function (of at least one argument) a must be a function (of at least one argument) e.g. f (*2) 4 (+1) e.g. f (*2) 4 (+1) = (+1) ((*2) 4) = 1 + (2 * 4) @ @ @ (* 2) f 4 (+ 1)

16
5/10/201516 Recall HIGHER ORDER functions can return functions: Recall HIGHER ORDER functions can return functions: g a b = (+ a) g a b = (+ a) main = g 3 4 5 main = g 3 4 5 Too many args? Too many args? No! No! Preamble (3) @ @ @ 4 g 3 5 @ 5 (+ 3)

17
5/10/201517 Example: Lists as Functions We have already seen how the list (1 : (2 : (3: []))) can be represented as We have already seen how the list (1 : (2 : (3: []))) can be represented as Cons 1 (Cons 2 (Cons 3 Nil)) Cons 1 (Cons 2 (Cons 3 Nil)) Where Cons and Nil are constructors of an algebraic type Where Cons and Nil are constructors of an algebraic type The difference being that this data structure is now of type mylist num and not of type [num] The difference being that this data structure is now of type mylist num and not of type [num] This assumes the algebraic type definition: This assumes the algebraic type definition: mylist * ::= Nil | Cons * (mylist *) Now we consider a new representation: Now we consider a new representation: cons 1 (cons 2 (cons 3 nil)) cons 1 (cons 2 (cons 3 nil)) where cons and nil are functions ! (as follows:) where cons and nil are functions ! (as follows:)

18
5/10/201518 Example: Lists as Functions The list (1 : (2 : (3: []))) can be represented as The list (1 : (2 : (3: []))) can be represented as cons 1 (cons 2 (cons 3 nil)) cons 1 (cons 2 (cons 3 nil)) where cons and nil are functions ! (as follows:) where cons and nil are functions ! (as follows:) cons a b f = f a b False nil f = f (error “head of nil”) (error “tail of nil”) True

19
5/10/201519 Example: Lists as Functions The list (1 : (2 : (3: []))) can be represented as The list (1 : (2 : (3: []))) can be represented as cons 1 (cons 2 (cons 3 nil)) cons 1 (cons 2 (cons 3 nil)) where cons and nil are functions ! (as follows:) where cons and nil are functions ! (as follows:) cons a b f = f a b False nil f = f (error “head of nil”) (error “tail of nil”) True x = cons ‘A’ nil is a partial application ! x = cons ‘A’ nil is a partial application ! y = nil is a partial application ! y = nil is a partial application ! Both cons and nil are partially applied – the final argument is only supplied when an element is selected or we test for nil Both cons and nil are partially applied – the final argument is only supplied when an element is selected or we test for nil To see how it works, consider the definitions of head, tail and isnil To see how it works, consider the definitions of head, tail and isnil

20
5/10/201520 Example 1. head x =x h where where h a b c = a h a b c = a tail x = x t where where t a b c = b t a b c = b isnil x = x g where where g a b c = c g a b c = c Example: x = cons ‘A’ nil y = cons ‘B’ x head y (cons ‘B’ x) h cons ‘B’ x h h ‘B’ x False ‘B’ cons a b f = f a b False nil f = f (error “head of nil”) (error “tail of nil”) True head tail isnil

21
5/10/201521 Example 1. Consider: Consider: head (tail (tail (cons a (cons b nil)))) (tail (tail (cons a (cons b nil)))) h ( (tail (cons a (cons b nil)) t) h ( ( (cons a (cons b nil)) t) t) h ( ( t a (cons b nil) False) t) h ( (cons b nil) t) h (t b nil False) h nil h h (error “head of nil”) (error “tail of nil”) True error “head of nil”

22
5/10/201522 Example 1. ä Consider: isnil nil nil g g (error “head of nil”) (error “tail of nil”) True True isnil (cons a nil) (cons a nil) g g a nil False False

23
5/10/201523 Summary Programming examples 1. an arithmetic expression evaluator (simple) 2. lists as functions (needs thought!)

Similar presentations

Presentation is loading. Please wait....

OK

1 0657.313A Programming Languages Syntax Analysis (1)

1 0657.313A Programming Languages Syntax Analysis (1)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google