Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Tree Recursion Traditional Approach. Tree Recursion Consider the Fibonacci Number Sequence: Time: , 1, 1, 2, 3, 5, 8, 13, 21,... /
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Comp 205: Comparative Programming Languages Lazy Evaluation Errors Evaluation Strategies Infinite Lists…. Lecture notes, exercises, etc., can be found.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
Formal Models of Computation Part II The Logic Model
Operators, Functions and Modules1 Pattern Matching & Recursion.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
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,
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
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)
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.
© 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.
Advanced Functional Programming 2010
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
Conditional Expressions
Theory of Computation Lecture 4: Programs and Computable Functions II
CS 326 Programming Languages, Concepts and Implementation
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Installation and exercises
PROGRAMMING IN HASKELL
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Theory of Computation Lecture 4: Programs and Computable Functions II
Programming Languages
PROGRAMMING IN HASKELL
Presentation transcript:

Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can be found at:

Nested Definitions maxOf3 :: Int -> Int -> Int -> Int maxOf3 x y z = maxOf2 u z where u = maxOf2 x y "Local" definitions can be made using the where keyword: maxOf3 x y z = maxOf2 (maxOf2 x y) z which is equivalent to:

The Fibonnacci Sequence -- nth number in the Fibonnacci sequence fib n = fib1 where (fib1, fib2) = fibs n -- (nth, (n+1)th) in Fib. seq. fibs 0 = (1,1) fibs n = (f2, f1 + f2) where (f1,f2) = fibs (n-1)

... Or fib n = fib1 where (fib1, fib2) = fibs n fibs n | n <= 0 = (1,1) | n > 0 = (f2, f1 + f2) where (f1,f2) = fibs (n-1)

The F sequence where F = Fibonacci -- file: fib.hs fib n = fib1 where (fib1, fib2) = fibs n fibs m | m < 1 = (1,1) | 0 < m = (f2, f1 + f2) where (f1,f2) = fibs (m-1),

The F sequence where F = Fibonacci -- file: fib.hs fib n = f1 where (f1, f2) = fibs n fibs n | n < 1 = (1,1) | 0 < n = (f2, f1 + f2) where (f1,f2) = fibs (n-1),

Local is Hidden Main> :l fib.hs... Main> fibs 5 ERROR - Undefined variable "fibs" Main>

Summary Key topics: Local definitions ( where ) Next: Lists

Lists bread milk butter onions orange juice t-bone steaks chocolate Attendance Sheets League tables top tens the Fibonnacci sequence...

Lists in Haskell A list is a sequence of values, in a particular order

Lists in Haskell A list is a sequence of values, in a particular order Lists are homogeneous: all the elements in a list (in Haskell) have the same type

Lists in Haskell A list is a sequence of values, in a particular order Lists are homogeneous: all the elements in a list (in Haskell) have the same type Lists are dynamic: unlike arrays in imperative languages, the length of a list is not constrained (and Haskell allows infinite lists)

Notations for Lists Haskell has three notations for lists: constructors: 1 : 1 : 2 : 3 : 5 : 8 : [] "square brackets" notation: [1, 1, 2, 3, 5, 8] strings (only for lists of chars): ['h', 'e', 'l', 'l', 'o'] = "hello"

Functions on Lists Prelude> length [1, 1, 2, 3, 5, 8] 6 Prelude> length "" 0 Prelude> [1, 1, 2, 3] ++ [5, 8] [1, 1, 2, 3, 5, 8] Prelude> [] ++ [1] ++ [2] [1,2] Length: Concatenation (append):

More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> Heads and tails

More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> Heads and tails

More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> head [] ERROR:... Prelude> Heads and tails

More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> head [] ERROR:... Prelude> tail [] ERROR:... Heads and tails

Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude>

Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude>

Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude> reverse 'p':'o':'o':'l':[] "loop" Prelude>

Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude> reverse 'p':'o':'o':'l':[] "loop" Prelude> zip [1,2,3,4,5] "cat" [(1,'c'),(2,'a'),(3,'t')]

List Constructors I A list is either: empty or an element of a given type together with a list of elements of the same type in BNF: [a] ::= [] | a : [a]

List Constructors II "[]" and ":" are constructors for lists: they are primitive operators (i.e., not evaluated) all lists are built from these operators (and elements of the "parameter type")

Constructors and Pattern-Matching Constructors can be used in Pattern-Matching: isempty [] = True isempty anythingElse = False length [] = 0 length (x : xs) = 1 + length xs head [] = error "head []" head (x:xs) = x

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) 

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] }

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])  { x  2, xs  4 : [] }

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])  { x  2, xs  4 : [] } length (4 : []) 

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])  { x  2, xs  4 : [] } length (4 : [])  { x  4, xs  [] } length [] 

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + (length xs) length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])  { x  2, xs  4 : [] } length (4 : [])  { x  4, xs  [] } length []  { }

Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + (length xs) length (1 : 2 : 4 : [])  { x  1, xs  2 : 4 : [] } 1 + length (2 : 4 : [])  { x  2, xs  4 : [] } length (4 : [])  { x  4, xs  [] } length []  { }

length without Pattern-Matching length xs | xs == [] = 0 |otherwise = 1 + length (tail xs) length can be defined without pattern-matching, but the definition is more difficult to read:

Summary Key topics: Lists (3 notations) Constructors Pattern-matching Recursion Next: Recursion and List Comprehensions