Function Application In Haskell function application has precedence

  • Slides: 11
Download presentation
Function Application In Haskell, function application has precedence over all other operations. Since the

Function Application In Haskell, function application has precedence over all other operations. Since the compiler knows how many arguments each function requires, we can do the following: func 1 x = x + 2 func 2 x y = x*x + y*y func 3 a b = func 1 a + func 2 b a No parentheses are necessary – the known signatures of func 1 and func 2 define how to compute func 3. September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 1

If – Then - Else Remember that execution of purely functional Haskell code involves

If – Then - Else Remember that execution of purely functional Haskell code involves only function evaluation, and nothing else. Therefore, there is an if – then – else function, but it always has to return something, so we always need the “else” part. Furthermore, it needs to have a well-defined signature, which means that the expressions following “then” and “else” have to be of the same type. September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 2

If – Then - Else Example: iq iq : : Int -> String n

If – Then - Else Example: iq iq : : Int -> String n = if n > 130 then "Wow!" else "Bah!" You can put everything in a single line if you like, as in this example: max’ : : Int -> Int max’ x y = if x > y then x else y September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 3

Some Functions on Lists head xs: Returns the first element of list xs tail

Some Functions on Lists head xs: Returns the first element of list xs tail xs: Returns list xs with its first element removed length xs: Returns the number of elements in list xs reverse xs: returns a list with the elements of xs in reverse order null xs: returns true if xs is an empty list and false otherwise September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 4

Some Functions on Tuples fst p: Returns the first element of pair p snd

Some Functions on Tuples fst p: Returns the first element of pair p snd p: Returns the second element of pair p This only works for pairs, but you can define your own functions for larger tuples, e. g. : fst 3 : : (a, b, c) -> a fst 3 (x, y, z) = x You can always replace variables whose values you do not need with an underscore: fst 3(x, _, _) = x September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 5

Some Functions on Tuples The Cartesian product is a good example of how to

Some Functions on Tuples The Cartesian product is a good example of how to build lists of tuples using list comprehensions: cart xs ys = [(x, y) | x <- xs, y <- ys] > cart [1, 2], [‘a’, ‘b’, ‘c’] > [(1, ‘a’), (1, ‘b’), (1, ‘c’), (2, ‘a’), (2, ‘b’), (2, ‘c’)] Note that the assignment y <- ys first goes through all elements of ys before the value of x changes for the first time. The changes are “fastest” for the rightmost assignment and propagate to the left, like the digits of a decimal number when we count, e. g. , from 100 to 999. September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 6

Pattern Matching You can define separate output expressions for distinct patterns in the input

Pattern Matching You can define separate output expressions for distinct patterns in the input to a function. This is also the best way to implement recursion, as in the factorial function: fact 0 = 1 fact n = n*fact (n – 1) Similarly, we can define recursion on a list, for example, to compute the sum of all its elements: sum’ [] = 0 sum’ (x: xs) = x + sum’ xs September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 7

Guards In pattern matching, you have to specify exact patterns and values to distinguish

Guards In pattern matching, you have to specify exact patterns and values to distinguish different cases. If you need to check inequalities or call functions in order to make a match, you can use guards instead: iq. Guards : : Int iq. Guards n | n > 150 = | n > 100 = | otherwise September 11, 2018 -> [Char] “amazing!" “cool!" = "oh well. . . " Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 8

Laziness of Haskell One important thing to know about Haskell is that it performs

Laziness of Haskell One important thing to know about Haskell is that it performs lazy evaluation of expressions. Here, “lazy” means that expressions are only evaluated when absolutely necessary for the current computation. For example, this allows us to work with infinite lists without getting stuck in an infinite computation (take n xs returns the first n elements of list xs): >take 10 [1. . ] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 9

Some Functions We Wrote in Class euclid x y = sq. X + sq.

Some Functions We Wrote in Class euclid x y = sq. X + sq. Y where sq. X = x*x sq. Y = y*y is. Prime n = n > 1 && null [x | x <- [2. . div n 2], mod n x == 0] myzip [] [] = [] (x: xs) [] = [] [] (y: ys) = [] (x: xs) (y: ys) = (x, y): myzip xs ys September 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 10

Reading For this course, you should understand the material in "Learn you a Haskell"

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 11, 2018 Introduction to Artificial Intelligence Lecture 3: Intro to Haskell II 11