Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at: www.csc.liv.ac.uk/~grant/Teaching/COMP205/

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
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,... /
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
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.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
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,
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
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.
{ 12.2 Arithmetic Sequences and Series SWBAT recognize an arithmetic sequence SWBAT find the general nth term of an arithmetic sequence SWBAT evaluate.
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.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
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.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
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.
Types and Programming Languages Lecture 12a Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lesson 10.1, page 926 Sequences and Summation Notation Objective: To find terms of sequences given the nth term and find and evaluate a series.
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,
An introduction to functional programming using Haskell CENG242 –Recitation 1.
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)
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}
Conditional Expressions
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
Sequences and Series 9.1.
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
Types and Values.
A lightening tour in 45 minutes
Haskell Chapter 4.
Comp 205: Comparative Programming Languages
CSE 3302 Programming Languages
WARM UP State the pattern for each set.
Section 11.1 Sequences.
Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Sequences Overview.
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

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

Tuple Types Haskell allows composite types to be built up by "tupling", e.g. (Integer,Integer) is the type of pairs of Integer s; similarly (Integer,Char,Bool), etc. In general, (A,B) represents the type of pairs whose first component has type A and whose second component has type B.

Tuples Elements of tuple types are also written using the (_,_) notation; for example, (24,'a') is of type (Integer,Char). The (_,_) notation is an example of a special kind of operator called a constructor: all tuples can be built using this operator, and the operator is not evaluated: Prelude> (24,'a') (24,'a')

Pattern-Matching #2 -- add a pair of numbers add :: (Integer,Integer) -> Integer add (x,y) = x + y Constructors can be used in patterns: fst (x,y) = x snd (x,y) = y

Hot Stuff: Currying curriedAdd :: Integer -> Integer -> Integer curriedAdd m n = m + n Prelude> :l arithmetic... Main> :t curriedAdd curriedAdd :: Integer -> Integer -> Integer Main> :t curriedAdd 3 curriedAdd 3 :: Integer -> Integer Main> curriedAdd 3 5 8

Currying There is a one-to-one correspondence between the types (A,B) -> C and A -> (B -> C). Given a function f :: (A,B) -> C, its curried equivalent is the function curriedF :: A -> B -> C curriedF a b = f (a,b)

Curried Max maxOf2 :: Integer -> Integer -> Integer maxOf2 m n | m >= n = m | m < n = n A possible definition of maxOf2 is:

Or... maxOf2 m n | m >= n = m | otherwise = n Another possible (equivalent) definition of maxOf2 is:

Or Even... maxOf2 m n | m >= n = m | m <= n = n Another possible (equivalent) definition of maxOf2 is:

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: Tuples and Currying Guards ( | = … ) Local definitions ( where ) Next: Lists