Theory of Computation Lecture 4: Programs and Computable Functions II

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Overview of the Haskell 98 Programming Language
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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,
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
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.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
Laziness and Infinite Datastructures
Conditional Expressions
Recursion.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
dr Robert Kowalczyk WMiI UŁ
PROGRAMMING IN HASKELL
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
A lightening tour in 45 minutes
Haskell Chapter 4.
Revision Lecture
Functional Programming
Haskell.
CSE 3302 Programming Languages
Arrays, For loop While loop Do while loop
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 30-Nov-18.
PROGRAMMING IN HASKELL
Theory Of Computer Science
Type & Typeclass Syntax in function
The Programming Language L
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Programming Languages
PROGRAMMING IN HASKELL
Functional Programming
The Programming Language L
PROGRAMMING IN HASKELL
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:

Theory of Computation Lecture 4: Programs and Computable Functions II 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: iqGuards :: Int -> [Char] iqGuards n | n > 150 = “amazing!" | n > 100 = “cool!" | otherwise = "oh well..." September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Recursion Another example: The function zip takes two lists and outputs a list of pairs with the first element taken from the first list and the second one from the second list. Pairs are created until one of the lists runs out of elements. zip :: [a] -> [b] -> [(a,b)] zip _ [] = [] zip [] _ = [] zip (x:xs) (y:ys) = (x,y):zip xs ys September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Lambda Expressions Lambda expressions are anonymous functions that are useful whenever we need a simple helper function. Examples for lambda expressions: 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..5] [(1, 1, 1),(2, 4, 8),(3, 9, 27),(4, 16, 64),(5, 25, 125)] September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 “deriving Show” line makes these values printable by simply using the show function (object-to-string conversion) the way it is defined for the individual objects (here: floats). September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II 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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Records If we want to name the components of our data types, we can use records: data Car = Car {company :: [Char], model :: [Char], year :: Int} deriving Show myCar = Car {company="Ford", model="Mustang", year=1967} company myCar “Ford” September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

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 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II The Syntax of L Now back to our programming language L… A computation of a program P is defined to be a sequence s1, s2, …, sk of snapshots of P such that si+1 is the successor of si for i = 1, 2, …, k – 1 and sk is terminal. September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II What does it exactly mean when we say that a program computes a function? We would like to find a precise definition for this. Let P be any program in the language L and let r1, …, rm be m given numbers. We form the state  of P which consists of the equations X1 = r1, X2 = r2, …, Xm = rm, Y = 0 together with the equations V = 0 for all other variables V in P. We call this the initial state. September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Moreover, we call the snapshot (1, ) the initial snapshot. When running the program, there are two possible outcomes: Case 1: There is a computation s1, s2, …, sk of P beginning with the initial snapshot. Then we write P(m)(r1, r2, …, rm) for the value of the variable Y at the terminal snapshot sk. September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Case 2: There is no such computation; i.e., there is an infinite sequence s1, s2, s3, …beginning with the initial snapshot. In this case, P(m)(r1, r2, …, rm) is undefined. September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II Sample Computation (1, {X = r, Y = 0, Z = 0}), (4, {X = r, Y = 0, Z = 0}), (5, {X = r - 1, Y = 0, Z = 0}), (6, {X = r - 1, Y = 1, Z = 0}), (7, {X = r - 1, Y = 1, Z = 1}), (1, {X = r - 1, Y = 1, Z = 1}), . (1, {X = 0, Y = r, Z = r}), (2, {X = 0, Y = r, Z = r}), (3, {X = 0, Y = r, Z = r + 1}), (8, {X = 0, Y = r, Z = r + 1}). [A] IF X0 GOTO B (1) Z  Z+1 (2) IF Z0 GOTO E (3) [B] X  X-1 (4) Y  Y+1 (5) Z  Z+1 (6) IF Z0 GOTO A (7) We write: P(1)(r) = r. September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II

Theory of Computation Lecture 4: Programs and Computable Functions II For any program P and any positive integer m, the function P(m)(r1, r2, …, rm) is said to be computed by P. We allow any program to be run with any number of inputs. Consider the summation program from the previous lecture: P(2)(r1, r2) = r1 + r2 P(1)(r1) = r1 + 0 = r1 P(3)(r1, r2 , r3) = r1 + r2 September 14, 2017 Theory of Computation Lecture 4: Programs and Computable Functions II