Haskell Chapter 1, Part II.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
Haskell Chapter 5, Part I. Topics  Higher Order Functions  map, filter  Infinite lists Get out a piece of paper… we’ll be doing lots of tracing.
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.
12 Haskell.  ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip  Haskell is  Lazy evaluated  Case-sensitive.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
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,
Haskell Chapter 3, Part I. Pattern Matching  Pattern matching with tuples  Pattern matching with list comprehensions  As-patterns.
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 Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 1 Functional Programming Symbolic AI is based.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Dan Johnson.  Functional language that started development in  Committee designed language  Pure and Lazy  Compiled or Interpreted  Named after.
Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka.
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.
Overview of the Haskell 98 Programming Language
Functional Programming Lecture 5 - Tuples. Packaging several values together Sometimes you need to package up several values into a single object –A function.
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.
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.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© 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.
Lecture 14: Advanced Topic: 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
Haskell: Syntax in Functions
Recursion.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Chapter 2.
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
MPCS 51400: Functional Programming
Functions and patterns
A lightening tour in 45 minutes
Haskell.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
JavaScript: Functions.
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
An aggregation mechanism
PROGRAMMING IN HASKELL
Advanced Algorithms Analysis and Design
Type & Typeclass Syntax in function
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Presentation transcript:

Haskell Chapter 1, Part II

List Comprehension List comprehensions are a way to filter, transform and combine lists Similar to mathematical set comprehensions {2 * x | x e N, X <= 10} In Haskell: [x * 2 | x <- [1..10]] “draw” our elements from the list [1..10] so x takes on each value from 1 to 10 part before the pipe (|) is the output

With a predicate [x * 2 | x <- [50..100], x `mod` 7 == 3] Using a predicate in this way is called filtering Can separate predicates with a comma [x | x <- [10..20], x /= 13, x /= 15, x /= 19] Can draw from several lists [x+y| x<-[1,2,3], y <- [10,100, 1000]] result: [11,101,1001,12,102,1002,13,103,1003]

More list comprehensions Can use a temporary variable length' xs = sum [1 | _ <- xs] Can be used with strings (they’re lists too) removeNonUppercase st = [c | c <- st, c `elem` ['A'..'Z']] Nested list comprehensions -- let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]] removeOdd xxs = [[x | x <- xs, even x] | xs <- xxs] Function definitions – must load, not just interpret. Use let to bind.

Tuples Used to store several heterogeneous elements as a single value Tuples have a fixed size Elements surrounded by parentheses (1,3) (3, ‘a’, “hello”) (50, 50.4, “hello”, ‘b’) tuple of size 2 is a different type from tuple of size 3 tuples with different member elements are different types

More tuples Storing pairs is common in Haskell Useful functions to manipulate: fst snd zip [1,2,3] [4,5,6] => [(1,4),(2,5),(3,6)] zip [1..] ["apple", "orange", "banana"] => [(1,"apple"),(2,"orange"),(3,"banana")]

Tuples in list comprehensions Generate tuples triples = [(a,b,c) | c <- [1..10], a<-[1..10], b<-[1..10]] Generate tuples with filter rightTriangle = [(a,b,c) | c <- [1..10], a<-[1..c], b<-[1..a], a^2 + b^2 == c^2]

Play and Share evenOddPairs [1..4][20, 17, 23, 42] evenCubes [1..20] [8,64,216,512,1000,1728,2744,4096,5832,8000] onlyBig [200,30,50,20,120] 100 [200,120] noDiagonal [1..4] [(1,2),(1,3),(1,4),(2,1),(2,3),(2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)] diagonal 10 [(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10)] countOdd [1..30] 15 evenOddPairs [1..4][20, 17, 23, 42] [(2,17),(4,17),(2,23),(4,23)] removeDigits "abc1d23A.98" "abcdA.“ ends [[4,5,6],[1,2],[7,1,0]] [6,2,0] * These are parameters