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.

Slides:



Advertisements
Similar presentations
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
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.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
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.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
1 Functional Programming Lecture 6 - Algebraic Data Types.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
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,
Applied Algebra B Lesson: 2 – 3 Adding Integers Objective: Learn to add integers.
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.
Functional Programming Lecture 3 - Lists Muffy Calder.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Pigeonhole Principle. If n pigeons fly into m pigeonholes and n > m, then at least one hole must contain two or more pigeons A function from one finite.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
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.
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
PROGRAMMING IN HASKELL
ML: a quasi-functional language with strong typing
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
COP4020 Programming Languages
CSc 110, Autumn 2017 Lecture 31: Dictionaries
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
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
Solving Systems of Equations
L8-2 Notes: Adding Integers
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
When we multiply by 10 we are making the number ten times bigger.
PROGRAMMING IN HASKELL
2 4 −4 −2 −5 −3 −
PROGRAMMING IN HASKELL
Sample lecture slides.
Step 1: Put the equations in Standard Form. Standard Form: Ax + By = C
Presentation transcript:

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 of lists into a list – concat [[1,2], [], [3], [4,5]] = [1,2,3,4,5]

sum' [] = 0 sum' (x:xs) = x+ sum' xs flatten' [] = [] flatten' (x:xs) = x++ flatten' xs

More Exercises – don’t use built in functions return the first element in a list, giving an error if the list is empty e.g., head "string" = 's' take two lists and make a list of pairs -- e.g., zip [1,2] [3,4,5] = [(1,3),(2,4)]

head' [] = error "no head" head' (x:xs) = x zip' [] x = [] zip' x [] = [] zip' (x:xs) (y:ys) = [(x,y)] ++ (zip' xs ys) or zip’ (x:xs) (y:ys) = (x,y) : (zip1 xs ys)

Use list comprehensions to do these For a list of tuples, add each pair in a list addPairs :: [(Integer,Integer)] -> [Integer] [(2,3), (4,0), (5,5)] yields [5,4,10] Write a function to select only the items in a list which are bigger than 10 List all pairs of numbers from [1,3,5] X [2,4,6] in which the second component is bigger

addPairs list = [x+y| (x,y)<-list] select x = [a| a 10] filter (>10) list (using the built-in function) bigger x y = [(a,b)|a<- x, b<- y, a < b]

Write a function delete to remove an element from a list delete 4 [1,3,4,6] yields [1,3,6] delete 5 [1,3,4,6] yields [1,3,4,6] Write a function to remove all items from the first list that are also in the second list diff [1,3,4,5,7] [3,9] yields [1,4,5,7]

remove y [] = [] remove y (x:xs) = if y==x then remove y xs else x:(remove y xs) or remove x list = [y|y <-list, y/=x] setDif z [] = z setDif xs (y:ys) = setDif (remove y xs) ys

Using list comprehensions Create a list which multiplies all combinations of elements of the two lists together mult [1,2,3,4] [3,5,7,9] yields [3,5,7,9,6,10,14,18,9,15,21,27,12,20,28,36] Create a list which tells whether each item is odd or even oddEle [1..9] yields [True,False,True,False,True,False,True,False,True]

mult xs ys = [a*b | a<- xs, b<- ys] oddEle xs = [odd x| x<- xs]

Factors Find the factors of a number less than itself factors 24 yields [1,2,3,4,6,8,12]

factors x = [n|n<-[1..(x `div` 2)], x `mod` n ==0]

Generate all possible permutations of a list perms [1,2,3] yields [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2],[3,2,1]] Note: If you have two generators it treats them as nested: [(i,j) | i <- [1,2], j <- [10..14]] yields the result [(1,10), (1,11), (1,12),(1,13),(1,14),(2,10),(2,11), (2,12),(2,13),(2,14)]

remove y [] = [] remove y (x:xs) = if y==x then remove y xs else x:(remove y xs) perms [] = [[]] perms xs = [(x:y) | x <- xs, y <- perms (remove x xs)]

Quicksort Pick an element, called a pivot, from the list.pivot Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. Recursively

qsort :: [Int]  [Int] qsort [] = [] qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger where smaller = [a | a  xs, a  x] larger = [b | b  xs, b  x]

User defined types data Student = USU String Float Suppose you have a list of students (using the above type definition), create a list of students with a GPA > 3.0 Note: String is the same as [Char] What is the TYPE of your function?

data Student = USU String Float myclass = [USU "Mike" 3.7, USU "Steve" 3.9, USU "Fred" 2.9, USU "Joe" 1.5] selectBest :: [Student] -> [String] selectBest [] = [] selectBest ((USU name gpa):list) = name : selectBest list

Write a linked list insertion data List = Nil | Node Int List deriving (Show) Do a regular linked list insertion. Try an ordered linked list insertion.

data List = Nil | Node Int List deriving (Show) myList :: List -- constant function myList = (Node 5 (Node 13 (Node 20 Nil))) addList x rest = (Node x rest) addOrdered x Nil = Node x Nil addOrdered x (Node y rest) = if x < y then (Node x (Node y rest)) else Node y (addOrdered x rest)