Pattern Matching Pattern matching allows us to do things like this:

Slides:



Advertisements
Similar presentations
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
TES3111 October 2001 Artificial Intelligence LISP.
Introduction to Artificial Intelligence Lecture 2: Perception & Action
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Artificial Intelligence Chapter 2 Stimulus-Response Agents
Artificial Intelligence Lecture 8. Outline Computer Vision Robots Grid-Space Perception and Action Immediate Perception Action Robot’s Perception Task.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 9: Functional Programming in a Typed Language.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
February 9, 2016Introduction to Artificial Intelligence Lecture 5: Perception & Action 1 Frege Notes In order to use many of the Java math functions in.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Functional Programming
CS314 – Section 5 Recitation 9
Functional Programming
Conditional Expressions
Recursion.
Chapter 6: User-Defined Functions I
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Functions and patterns
A lightening tour in 45 minutes
Functional Programming
Haskell.
Introduction to Functional Programming in Racket
User-Defined Functions
Control Structures – Selection
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
Haskell Dealing with impurity 30-Nov-18.
Programming Funamental slides
PROGRAMMING IN HASKELL
Artificial Intelligence Chapter 2 Stimulus-Response Agents
Type & Typeclass Syntax in function
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Computing Fundamentals
Chapter 6: User-Defined Functions I
Introduction to Functional Programming in Racket
CSE-321 Programming Languages Introduction to Functional Programming
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Haskell Dealing with impurity 8-Apr-19.
Functions and patterns
Functional Programming
Functions and patterns
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Dealing with impurity 29-May-19.
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

Pattern Matching Pattern matching allows us to do things like this: testList [] = "empty" testList (x:[]) = "single-element" testList (x:xs) = "multiple elements. First one is " ++ show x The last line demonstrates how we can use pattern matching to not only specify a pattern but also access the relevant input elements in the function expression. Since we do not use xs in that line, we could as well write testList (x:_) = "multiple elements. First one is " ++ show x September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Pattern Matching Haskell’s syntax allows us to write a quicksort algorithm very concisely and clearly: quickSort [] = [] quickSort (x:xs) = quickSort low ++ [x] ++ quickSort high where low = [y | y <- xs, y < x] high = [y | y <- xs, y >= x] Note, though, that this quicksort algorithm is not as fast as if we had implemented it, for example, in C with elements remaining in the same memory block. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Recursion Since variables in Haskell are immutable, our only way of achieving iteration is through recursion. For example, the reverse function receives a list as its input and outputs the same list but with its elements in reverse order: reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Currying As you know, you can turn any infix operator into a prefix operator by putting it in parentheses: (+) 3 4 7 Now currying allows us to place the parentheses differently: (+ 3) 4 By “fixing” the first input to (+) to be 3, we created a new function (+ 3) that receives only one (further) input. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Currying We can check this: :t (+ 3) (+ 3) :: Num a => a -> a This (+ 3) function can be used like any other function, for example: map (+ 3) [1..5] [4, 5, 6, 7, 8] Or: map (max 5) [1..10] [5, 5, 5, 5, 5, 6, 7, 8, 9, 10] September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Lambda Expressions Sometimes we just need a small local function that is not used anywhere else in the program. Then it is convenient to use a lambda expression, which is a way of defining an anonymous function directly in the place where we use it. They have the following form: \input -> output For example: (\x -> 3*x) 4 12 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Lambda Expressions More examples for lambda expressions: zipWith (+) [1..10] [11..20] [12,14,16,18,20,22,24,26,28,30] zipWith (\x y -> x^2 + y^2) [1..10] [11..20] [122,148,178,212,250,292,338,388,442,500] map (\x -> (x, x^2, x^3)) [1..10] [(1, 1, 1),(2, 4, 8),(3, 9, 27),(4, 16, 64),(5, 25, 125)] September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Folds Folds take a binary function f, a start value z, and a list: foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Folds You can think of these functions as using an accumulator that receives value z. Its value is updated by applying f to it and the next list element, and the output is the final accumulator value after the whole list has been processed. To understand these functions, it is best to do some sample computations by hand. Folds can be used to build a variety of functions, e.g.: sum = foldl (+) 0 (Point-free style: The last argument xs is identical on both sides of the equation and can be omitted.) September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

foldl Expression Evaluation foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs foldl (+) 0 [1, 2, 3] foldl (+) ((+) 0 1) [2, 3] foldl (+) 1 [2, 3] foldl (+) ((+) 1 2) [3] foldl (+) 3 [3] foldl (+) ((+) 3 3) [] foldl (+) 6 [] 6 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

The $ Operator The $ operator is defined as follows: f $ x = f x It has the lowest precendence, and therefore, the value on its right is evaluated first before the function on its left is applied to it. As a consequence, it allows us to omit parentheses: negate (sum (map sqrt [1..10])) can be written as: negate $ sum $ map sqrt [1..10] September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Function Composition Similarly, we can use function composition to make our code more readable and to create new functions. As you know, in mathematics, function composition works like this: (f  g) (x) = f(g(x)) In Haskell, we use the “.” character instead: map (\xs -> negate (sum (tail xs))) [[1..5],[3..6],[1..7]] Can be written as: map (negate . sum . tail) [[1..5],[3..6],[1..7]] September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Data Types Data types can be declared as follows: data Bool = false | true data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving Show Then we can construct values of these types like this: x = Circle 3 4 5 The addition “deriving Show” makes these values printable. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Data Types We can use pattern matching on our custom data types: surface :: Shape -> Float surface (Circle _ _ r) = 3.1416 * r ^ 2 surface (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1) surface $ Circle 10 20 10 314.16 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Records If we want to name the components of our data types, we can use records: data Car = Car {company :: String, model :: String, year :: Int} deriving Show myCar = Car {company="Ford", model="Mustang", year=1967} company myCar “Ford” September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Tree Data Type Example 3 7 8 2 5 3 9 1 4 data BinaryTree a = Leaf a | Branch (BinaryTree a) a (BinaryTree a) mytree = Branch (Branch (Leaf 2) 7 (Leaf 5)) 3 (Branch (Branch (Leaf 1) 3 (Leaf 4)) 8 (Leaf 9)) 3 7 8 2 5 3 9 1 4 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Tree Data Type Example (2) nodeCount (Leaf x) = 1 nodeCount (Branch left x right) = 1 + nodeCount left + nodeCount right treeHeight (Leaf x) = 0 treeHeight (Branch left x right) = 1 + max (treeHeight left) (treeHeight right) tree2list (Leaf x) = [x] tree2list (Branch left x right) = (tree2list left) ++ [x] ++ (tree2list right) foldTree f z (Leaf x) = f x z foldTree f z (Branch left x right) = foldTree f (f x (foldTree f z right)) left September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Input/Output with “do” Purely functional code cannot perform user interactions such as input and output, because it would involve side effects. Therefore, we sometimes have to use impure functional code, which needs to be separated from the purely functional code in order to keep it (relatively) bug-safe. In Haskell, this is done by so-called Monads. To fully understand this concept, more in-depth study is necessary. However, in this course, we do not need to perform much input and output. We can use a simple wrapper (or “syntactic sugar”) for this – the “do” notation. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Input/Output with “do” In a do-block, we can only use statements whose type is “tagged” IO so that they cannot be mixed with purely functional statements. Example for a program performing input and output: main = do putStrLn "Hello, what's your name?" name <- getLine putStrLn ("Hey " ++ name ++ ", you rock!") September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Reading For this course, you should understand the material in "Learn you a Haskell" in Chapters 1-6, Chapter 8 until the end of "type parameters“ and the "Hello, World!" section of Chapter 9. Please read this material and experiment with it as far as you get. In class we will cover most of it and work on some coding examples. Then you will be ready to tackle AI problems with some powerful programming tools. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

A Trip to Grid-Space World Grid-space world is an extremely simple model of our own world. It is a three-dimensional space with a floor that is divided into cells by a two-dimensional grid. The cells can be empty or contain objects or agents. There can be walls between sets of cells. The agents are confined to the floor and can move from cell to cell. A robot in grid-space world can sense whether neighboring cells are empty or not. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Perception and Action Organisms in the real world have to do two basic things in order to survive: They have to gather information about their environment (perception) and based on this information, they have to manipulate their environment (including themselves) in a way that is advantageous to them (action). The action in turn may cause a change in the organism’s perception, which can lead to a different type of action. We call this the perception-action cycle. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Perception and Action Complex organisms do not just perceive and act, but they also have an internal state that changes based on the success of previous perception-action cycles. This is the mechanism of learning. We will first consider a very simple robot that lives in grid-space world and has no internal state. The grid has no tight spaces, that is, spaces between objects and boundaries that are only one cell wide. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Perception and Action The robot is supposed to find a cell next to a boundary or object and then follow that boundary forever. As we said, the robot can perceive the state of its neighboring cells: s1 s2 s3 s8 s4 s7 s6 s5 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Perception and Action The robot can move to a free adjacent cell in its column or row. Consequently, there are four possible actions that it can take: north: moves the robot one cell up east: moves the robot one cell to the right south: moves the robot one cell down west: moves the robot one cell to the left September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Immediate Perception-Action Now that we specified the robot’s capabilities, its environment, and its task, we need to give “life” to the robot. In other words, we have to specify a function that maps sensory inputs to movement actions so that the robot will carry out its task. Since we do not want the robot to remember or learn anything, one such function would be sufficient. However, it is useful to decompose it in the following way (next slide): September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Immediate Perception-Action Feature vector X 1 … Sensory input Perceptual processing Action function Action September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Immediate Perception-Action The functional decomposition has two advantages: Multiple action functions can be added that receive the same feature vector as their input, It is possible to add an internal state to the system to implement memory and learning. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

The Robot’s Perception For our robot, we define four different features x1, …, x4 that are important to it. Each feature has value 1 if and only if at least one of the shaded cells is not free: s5 s6 s7 s4 s8 s3 s2 s1 x1 s5 s6 s7 s4 s8 s3 s2 s1 x2 s5 s6 s7 s4 s8 s3 s2 s1 x3 s5 s6 s7 s4 s8 s3 s2 s1 x4 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

The Robot’s Action To execute action, we define an ordered set of rules: if x1 = 0 and x2 = 0 and x3 = 0 and x4 = 0 move north if x1 = 1 and x2 = 0 move east if x2 = 1 and x3 = 0 move south if x3 = 1 and x4 = 0 move west if x4 = 1 and x1 = 0 move north s5 s6 s7 s4 s8 s3 s2 s1 x1 s5 s6 s7 s4 s8 s3 s2 s1 x2 s5 s6 s7 s4 s8 s3 s2 s1 x3 s5 s6 s7 s4 s8 s3 s2 s1 x4 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Task Completion Rules 1 2 1 1 3 1 3  2 4 4 4 4 4 4 4 5 1 3 3 5 1 3 2 2 2  3 3 5 2 5 1 3 3 5 3 5 1 4 4 4 3 3 2 1 3 2 2 2 1 3  4 4 4 5 3 5 3  5 4 4 3 September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Production Systems Production systems are a standardized way to represent action functions. A production system consists of an ordered list of production rules (productions). Each rule is written in the form condition  action. A production system is therefore written like: c1  a1 c2  a2  cm  am The action of the first rule whose condition evaluates to 1 is executed. September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III

Production Systems Using Boolean notation, the production system for our boundary-following robot looks like this: x4x1  north x3x4  west x2x3  south x1x2  east 1 north September 13, 2018 Introduction to Artificial Intelligence Lecture 4: Intro to Haskell III