CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.

Slides:



Advertisements
Similar presentations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Advertisements

0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
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.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Problem-solving on large-scale clusters: theory and applications Lecture 1: Introduction and Theoretical Background.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Lecture 1 - Introduction, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Smalltalk (cont.) Lecture 16 – OO Programming, Spring CSE3302 Programming Languages, UT-Arlington.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Lecture 1 - Introduction, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
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.
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,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
© 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.
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}
Conditional Expressions
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
COP4020 Programming Languages
PROGRAMMING IN HASKELL
Higher Order Functions
CSE 341 : Programming Languages Section 4 Haskel #2
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
Lazy Programming Lazy evaluation:
HIGHER ORDER FUNCTIONS
Programming Languages
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Lambda Expressions Cases
CS 403: Programming Languages
PROGRAMMING IN HASKELL
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Grading Homework (HW): 25%. (HW1, HW2, HW3, HW4, HW5) Machine Problems (MP): 20%. (MP1, MP2) Essays (ES): 10%. Midterm exam: 20%. Final exam: 25%. Bonus points: 5%. Based on class participation. some additional bonus points (in MP2, HW5) Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Letter Grades curve-based The cutoffs for letter grades are based on your performance. Bonus point: can only increase your grade Example: cutoff for A: 88.5 your raw score: 86 bonus points: 3 your grade: B -> A (86+3>88.5) Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

What’s ahead HW4: due by April 21 st HW5: due by May 2nd Essay: have you started yet? (due at May 1 st ) Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Grades on WebCT Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Foldl and Foldr foldl :: (a -> b -> a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Foldr:  is right-associative Foldl:  is left-associative Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, foldr (-) 1 [2,3,4] foldl (-) 1 [2,3,4] (section in the tutorial)

Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, foldr  v [x0,x1, …, xn] = x0  (x1  (…(xn  v)…)) foldl  v [x0,x1, …, xn] = (…((v  x0)  x1)…)  xn

Lambda Expressions A function can be constructed without giving it a name by using a lambda expression. \x -> x+1 The nameless function that takes a number x and returns the result x+1. Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Why Are Lambda's Useful? Lambda expressions can be used to give a formal meaning to functions defined using currying. For example: add x y = x+y add = \x -> (\y -> x+y) means Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Another example means Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, compose f g x = f (g x) compose f g = \x -> f (g x)

Exercises 1.Write a recursive function sum n that returns …. + n Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 2.Write a recursive function genlist n that returns [ 1, 2, …, n ]. Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 2. (cont.)Check to make sure n>0, otherwise return empty list. Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 3.Check if an element is a member of a list. Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 4.Implement ++ Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 4. (cont.) Merge two lists and return a list with elements sorted Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 5.A triple (x,y,z) of positive integers is pythagorean if x^2+y^2=z^2. Using list comprehension to define a function pyths::Int->[(Int, Int, Int)] that returns the list of all pythagorean triples whose components are at most a given limit. For example: >pyths 10 [(3,4,5), (4,3,5), (6,8,10), (8,6,10)] Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 5. (cont.) Make sure x <= y <= z Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 6. Define list comprehension [ f x | x <- xs, p x] using map and filter. Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Exercises 7. Define map f and filter p using foldr Lecture 20 – Functional Programming, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,