CS 320: Concepts of Programming Languages

Size: px
Start display at page:

Download "CS 320: Concepts of Programming Languages"

Transcription

1 CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 06: Useful Haskell Syntax, HO Programming Continued o Goodbye to Bare Bones Haskell: Built-in syntax for lists & tuples o Lambda expressions and Beta-Reduction o Let and Case Expressions Reading: Hutton Ch. 4 & 7 You should be starting to look through the Standard Prelude in Appendix B, particularly the list processing functions!

2 Useful Haskell Syntax: Built-In Types We have used Bare Bones Haskell notation for Lists, Pairs, and Triples in order to emphasize the importance of pattern-matching in defining functions. However, enough is enough! Here is a more convenient syntax which is built into the basic Haskell syntax (and not just implemented as functions in the Prelude): BB Haskell Flesh and Blood Haskell

3 Useful Haskell Syntax: Built-In Types We have used Bare Bones Haskell notation for Lists, Pairs, and Triples in order to emphasize the importance of pattern-matching in defining functions. However, enough is enough! Here is a more convenient syntax which is built into the basic Haskell syntax (and not just implemented as functions in the Prelude): BB Haskell Flesh and Blood Haskell Built in to the Prelude exactly as we presented it: Bool, True, False, &&,, not Built in types Integer, Double,... Main> Main> *

4 Useful Haskell Syntax: Built-In Tuples BB Haskell Main> P 3 True P 3 True Main> (P 4 (P True (-9))) P 4 (P True (-9)) Main> (T 3 5 9) T Main> (T 9 False 2) T 9 False 2 Main> fst (P 3 True) 3 Main> snd (P 3 (P True 2)) P True 2 Main> toleft (P 4 (P True (-9))) P (P 4 True) (-9) Main> p2t (P 4 (P True (-9))) T 4 True (-9)

5 Useful Haskell Syntax: Built-In Tuples BB Haskell Main> P 3 True P 3 True Main> (P 4 (P True (-9))) P 4 (P True (-9)) Main> (T 3 5 9) T Main> (T 9 False 2) T 9 False 2 Provided in Prelude Flesh and Blood Haskell Main> (3,True) (3,True) Main> (4,(True,(-9))) 4 (True,(-9)) Main> (3,5,9) (3,5,9) Main> (9,False,2) (9,False,2) Main> fst (3,True) 3 Main> snd (3,(True,2)) (True,2) Main> toleft (4,(True,(-9))) ((4,True),-9) Main> p2t (4,(True,(-9))) (4,True,-9) Main> (2,3,True,5, a,7,4, hi,5) (2,3,True,5, a,7,4, hi,5) Tuples can be any length, but fst and snd only work on pairs.

6 Useful Haskell Syntax: Built-In Lists BB Haskell Flesh and Bones Haskell Built in as part of syntax! Provided in Prelude Main>(Cons 3 (Cons 9 Nil)) Cons 3 (Cons 9 Nil) Main> head (Cons 3 (Cons 9 Nil)) 3 Main> tail (Cons 3 (Cons 9 Nil)) Cons 9 Nil Main> length (Cons 3 (Cons 9 Nil)) 2 Main> [] [] Main> 3:9:[] [3,9] Main> 3:[9] [3,9] Main> [3,9] [3,9] Main> head [3,9] 3 Main> tail [3,9] [9] Main> length [3,9] 2

7 Useful Haskell Syntax: Built-In Lists Start to become familiar with the list-processing functions in the Prelude, there are many useful functions already defined! See Hutton pp Main> [0,1,2] ++ [3,4] [0,1,2,3,4] Main> last [0,1,2,3,4] 4 Main> init [0,1,2,3,4] [0,1,2,3] Main> take 3 [0,1,2,3,4] [0,1,2] Main> drop 3 [0,1,2,3,4] [3,4] Main> takewhile (<3) [0,1,2,3,4] [0,1,2] Main> dropwhile (<3) [0,1,2,3,4] [3,4] Main> splitat 3 [0,1,2,3,4] ([0,1,2],[3,4]) Main> replicate 5 1 [1,1,1,1,1] Main> [0,1,2] ++ [3,4] [0,1,2,3,4] Main> reverse [0,1,2,3,4] [4,3,2,1,0] Main> map (^2) [0,1,2,3,4] [0,1,4,9,16] Main> filter even [0,1,2,3,4] [0,2,4] Main> concat [[0],[1,2],[3,4]] [0,1,2,3,4] Many more advanced functions can be found in Data.List.

8 Useful Haskell Syntax: Characters and Strings Characters (Hutton p.282) Main> 'a' 'a' Main> ['h','i','!'] "hi! Main Data.Char> islower 'a' True Main Data.Char> isupper 'a' False Main Data.Char> isalpha 'a' True Main Data.Char> isdigit 'a' False Main Data.Char> ord 'a' 97 Main Data.Char> chr 97 'a' Main Data.Char> digittoint '9' 9 Main Data.Char> inttodigit 4 '4' Main Data.Char> toupper 'a' 'A' Main Data.Char> tolower 'A' 'a' Main Data.Char> nextchar 'a' 'b'

9 Useful Haskell Syntax: Characters and Strings Strings are simply lists of Characters (Hutton p.282) Main> ['h','i','!'] "hi! Main> "hi " ++ "there" ++ "!" "hi there!" Main> "hi there"!! 3 't' Main> take 5 "hi there!" "hi th Main> words "hi there!" ["hi","there!"] Main> import Data.Char Main Data.Char> map toupper "hi there!" "HI THERE!" Any list function can be used on Strings. Check out Data.List! This nifty function is provided in the Prelude

10 Case Expressions A very useful kind of conditional expression is the case expression: case expression of pattern -> result pattern -> result pattern -> result... In other languages, the case statement is an alternative to a long nested ifthen-else, but in Haskell (of course!) it is more powerful, as it does pattern matching: describe :: [a] -> String describe [] = "empty" describe [x] = "singleton" describe _ = "big!" *Main> describe [4] "singleton" describe :: [a] -> String describe xs = case xs of [] -> "empty" [x] -> "singleton" _ -> big!"

11 Case Expressions This solves the problem that lambda expressions can pattern match, but not do multiple patterns: describe :: [a] -> String describe = \xs -> case xs of [] -> "empty" [x] -> "singleton" _ -> big!"

12 Beta Reduction and Let Expressions Recall: a lambda expression represents an anonymous function: makepair :: a -> b -> (a,b) makepair x y = (x,y) makepair x = \y -> (x,y) makepair = \x -> \y -> (x,y) Main> makepair 3 True (3,True) By referential transparency, we can simply use the lambda expression and apply it directly to arguments: Main> (\x -> \y -> (x,y)) 3 True (3,True)

13 Beta Reduction and Let Expressions We will study this much more in a few weeks, when we start to think about how to implement functional languages, but for now, we just define the concept of Beta-Reduction, which is simply substituting an argument for its parameter: ((\x -> <expression>) <argument>) => <expression> with x replaced by <argument> Examples: Main> (\x -> (x,x)) 4 (4,4) Main>(\x -> [3,x,9]) 4 [3,4,9] Main>(\x -> Just x) "hi" Just "hi" Main>(\x -> 5) 6 5 Main> (\x -> (\y -> (x,y))) 5 True (5,True) Main>(\x y -> [3,x,y]) 4 9 [3,4,9] Main>(\x y -> \z -> [x,y,z]) [2,4,9] Main> (\x -> (\x -> (x,x))) 5 True??

14 Beta Reduction and Let Expressions We will study this much more in a few weeks, when we start to think about how to implement functional languages, but for now, we just define the concept of Beta-Reduction, which is simply substituting an argument for its parameter: ((\x -> <expression>) <argument>) => <expression> with x replaced by <argument> Examples: Main> (\x -> (x,x)) 4 (4,4) Main>(\x -> [3,x,9]) 4 [3,4,9] Main>(\x -> Just x) "hi" Just "hi" Main>(\x -> 5) 6 5 Main> (\x -> (\y -> (x,y))) 5 True (5,True) Main>(\x y -> [3,x,y]) 4 9 [3,4,9] Main>(\x y -> \z -> [x,y,z]) [2,4,9] Main> (\x -> (\x -> (x,x))) 5 True (True,True) Why??

15 Scope in Haskell The scope of a variable (e.g., local variable, parameter) is the region of the program where it is legal to refer to that variable. Main> x <interactive>:14:1: error: Variable not in scope: x Main> Main> x = 4 Main> x 4 In Java there are several kinds of scoping rules...

16 Digression: Scope in Java The scope of a variable (e.g., local variable, parameter) is the region of the program where it is legal to refer to that variable. Main> x <interactive>:14:1: error: Variable not in scope: x Main> Main> x = 4 Main> x 4 In Java there are several kinds of scoping rules...

17 Digression: Scope in Java The scope of a variable (e.g., local variable, parameter) is the region of the program where it is legal to refer to that variable. Main> x <interactive>:14:1: error: Variable not in scope: x Main> Main> x = 4 Main> x 4 In Java there are several kinds of scoping rules...

18 Scope in Let Expressions The scope of a lambda parameter is the expression to the right of the -> (\x -> <expression>) Scope of x To find the parameter associated with an instance of a variable in the expression, look for the closest enclosing binding of the variable: (\x -> \ys -> (length (take x ys)))

19 Scope in Let Expressions: Hole in Scope To find the parameter associated with an instance of a variable in the expression, look for the closest enclosing binding of the variable: (\x -> \ys -> (length (take x ys))) Some weird things can happen when there is more than one occurrence of the same variable: Main> (\x -> ((\x -> (take x [1,2,3,4,5]) ) 3) ++ x) [7] [1,2,3,7] Main>(\x -> (\x -> (x,x))) 5 True (True,True) Hole in scope of outer x

20 Digression: Scope in Java Java allows multiple declarations of the same variable if one is a field and one is a local variable (either a parameter or a local variable), creating a hole in the scope of the field declaration:

21 Digression: Scope in Java But Java does NOT allow multiple declarations (and hence avoids the hole in scope issue) for two local variables:

22 Digression: Scope in Java But Java does NOT allow multiple declarations (and hence avoids the hole in scope issue) for two local variables:

23 Digression: Scope in C C allows multiple declarations without many restrictions:

24 Let Expressions in Haskell In Haskell we create local variables using let: (let x = <expr1> in <expr2>) cylinder r h = let sidearea = 2 * pi * r * h toparea = pi * r ^2 in sidearea + 2 * toparea Equivalent to a lambda application: ((\x -> <expr2>) <expr1>) Except that you can have multiple bindings in the same let. Scope of local variables let sq x = x * x in (sq 5, sq 3, sq 2) => (25,9,4) => 65 let x = 5 in let y = 2 * x in let z = x + y in (\w -> x * y + z) 10

25 Let Expressions in Haskell Haskell let s you define local variables any time you want with let (and where), and therefore hole in scope issues become relevant. Notice the enormous flexibility of Haskell and the referential transparency principle: You can use these kinds of expressions nearly anywhere! (let sq = (\x -> x*x) in \x -> (x,sq x) ) 5 => (5,25) (\x -> case x of 1 -> \x -> x > \x -> x * 2 _ -> \x -> x ) 2 6 => 12

Standard prelude. Appendix A. A.1 Classes

Standard prelude. Appendix A. A.1 Classes Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 03: Bare-Bones Haskell Continued: o Function Application = Rewriting by Pattern Matching o Haskell

More information

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 5 Function properties Don Sannella University of Edinburgh Part I Booleans and characters Boolean operators not :: Bool -> Bool (&&), ( ) :: Bool -> Bool ->

More information

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read. EDAF40 2nd June 2017 14:00-19:00 WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read. DO NOT WRITE WITH OTHER COLOUR THAN BLACK - coloured text

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

A tour of the Haskell Prelude

A tour of the Haskell Prelude A tour of the Haskell Prelude Bernie Pope 2001 1 Haskell The Haskell language was conceived during a meeting held at the 1987 Functional Programming and Computer Architecture conference (FPCA 87). At the

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

Lecture 2: List algorithms using recursion and list comprehensions

Lecture 2: List algorithms using recursion and list comprehensions Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns

More information

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}

More information

Functional Programming TDA 452, DIT 142

Functional Programming TDA 452, DIT 142 Chalmers Göteborgs Universitet 2018-01-11 Examiner: Thomas Hallgren, D&IT, Answering questions at approx 15.00 (or by phone) Functional Programming TDA 452, DIT 142 2018-01-11 14.00 18.00 Samhällsbyggnad

More information

Shell CSCE 314 TAMU. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can

More information

A general introduction to Functional Programming using Haskell

A general introduction to Functional Programming using Haskell A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell

More information

State Monad (3D) Young Won Lim 9/25/17

State Monad (3D) Young Won Lim 9/25/17 Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee 1 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as

More information

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

More information

Haskell Modules. Input and Output. Urtė Zubkaitė

Haskell Modules. Input and Output. Urtė Zubkaitė Haskell Modules. Input and Output Urtė Zubkaitė Modules in Haskell A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

Functional Programming TDA 452, DIT 142

Functional Programming TDA 452, DIT 142 Chalmers Göteborgs Universitet 2016-04-07 Examiner: David Sands dave@chalmers.se. Answering questions on the day of the exam (at approx 15.00): Gregoire Detrez (tel: 073 55 69 550) and at other times by

More information

Shell CSCE 314 TAMU. Higher Order Functions

Shell CSCE 314 TAMU. Higher Order Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Higher Order Functions 2 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as a result.

More information

Haskell through HUGS THE BASICS

Haskell through HUGS THE BASICS Haskell through HUGS THE BASICS FP for DB Basic HUGS 1 Algorithmic Imperative Languages variables assignment if condition then action1 else action2 loop block while condition do action repeat action until

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona 1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The

More information

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 372 Comparative Programming Languages. 4 : Haskell Basics CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011

More information

CSc 520. Principles of Programming Languages 11: Haskell Basics

CSc 520. Principles of Programming Languages 11: Haskell Basics CSc 520 Principles of Programming Languages 11: Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 02: Bare Bones Haskell Syntax: Data == Abstract Syntax Trees Functions == Rewrite Rules on ASTs

More information

CS 320 Homework One Due 2/5 11:59pm

CS 320 Homework One Due 2/5 11:59pm Name: BU ID (no dashes): CS 320 Homework One Due 2/5 11:59pm Write your answers to the problems in the space indicated. Scan your solution and submit to Gradescope as a PDF file. You will receive an email

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs. EXAM FUNCTIONAL PROGRAMMING Tuesday the 1st of October 2016, 08.30 h. - 10.30 h. Name: Student number: Before you begin: Do not forget to write down your name and student number above. If necessary, explain

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

Haskell Syntax in Functions

Haskell Syntax in Functions Haskell Syntax in Functions http://igm.univ-mlv.fr/~vialette/?section=teaching Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée December 14, 2015 Syntax in Functions Pattern Matching Pattern

More information

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

CSCE 314 Programming Languages. Functional Parsers

CSCE 314 Programming Languages. Functional Parsers CSCE 314 Programming Languages Functional Parsers Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]

More information

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible Name: CIS 252 Introduction to Computer Science Section M013: March 29, 2018 Question Points Possible Points Received 1 18 2 18 3 12 4 6 5 34 6 12 Total 100 Instructions: 1. This exam is a closed-book (and

More information

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers 1 CSCE 314: Programming Languages Dr. Flemming Andersen Functional Parsers What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]

More information

Shell CSCE 314 TAMU. Functions continued

Shell CSCE 314 TAMU. Functions continued 1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]

More information

CS 360: Programming Languages Lecture 12: More Haskell

CS 360: Programming Languages Lecture 12: More Haskell CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell: Haskell Programs We re covering material from Chapters 1-2 (and maybe 3) of the textbook. Haskell Fundamentals Prof. Susan Older A Haskell program is a series of comments and definitions. Each comment

More information

Programming Languages Fall 2013

Programming Languages Fall 2013 Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy

More information

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lectures 15 and 16 IO and Monads Don Sannella University of Edinburgh Part I The Mind-Body Problem The Mind-Body Problem Part II Commands Print a character putchar

More information

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam. Programming Languages Fall 2013 Midterm Exam 10/22/13 Time Limit: 100 Minutes Name (Print): Graduate Center I.D. This is a closed-book, closed-notes exam. Various definitions are provided in the exam.

More information

Lecture 10 September 11, 2017

Lecture 10 September 11, 2017 Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 10 September 11, 2017 Combining elements sumlist :: [Int] -> Int sumlist [] = 0 sumlist (x:xs) = x + (sumlist xs) multlist :: [Int]

More information

CSc 372 Comparative Programming Languages

CSc 372 Comparative Programming Languages CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

More information

Functional Programming for Logicians - Lecture 1

Functional Programming for Logicians - Lecture 1 Functional Programming for Logicians - Lecture 1 Functions, Lists, Types Malvin Gattinger 4 June 2018 module L1 where Introduction Who is who Course website: https://malv.in/2018/funcproglog/ Malvin Gattinger

More information

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona 1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next

More information

Introduction to Programming, Aug-Dec 2006

Introduction to Programming, Aug-Dec 2006 Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components

More information

Patterns The Essence of Functional Programming

Patterns The Essence of Functional Programming Patterns The Essence of Functional Programming Up to now we have defined functions in a very traditional way: function name + variable name parameters Read Chap 7 In functional programming we can exploit

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

CS 320 Midterm Exam. Fall 2018

CS 320 Midterm Exam. Fall 2018 Name: BU ID: CS 320 Midterm Exam Fall 2018 Write here the number of the problem you are skipping: You must complete 5 of the 6 problems on this exam for full credit. Each problem is of equal weight. Please

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction. Practical Practical An introduction to functional programming July 21, 2011 Contents Practical Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning

More information

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G. Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Pink) February 22, Points Possible

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Pink) February 22, Points Possible Name: CIS 252 Introduction to Computer Science Section M013: February 22, 2018 Question Points Possible Points Received 1 34 2 16 3 16 4 10 5 12 6 12 Total 100 Instructions: 1. This exam is a closed-book

More information

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Blue) February 22, Points Possible

CIS 252 Introduction to Computer Science Section M013: Exam 1 (Blue) February 22, Points Possible Name: CIS 252 Introduction to Computer Science Section M013: February 22, 2018 Question Points Possible Points Received 1 34 2 16 3 16 4 10 5 12 6 12 Total 100 Instructions: 1. This exam is a closed-book

More information

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza Haskell 101 (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell

More information

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

CIS 252 Introduction to Computer Science Section M019: Exam 1 (Yellow) February 22, Points Possible

CIS 252 Introduction to Computer Science Section M019: Exam 1 (Yellow) February 22, Points Possible Name: CIS 252 Introduction to Computer Science Section M019: February 22, 2018 Question Points Possible Points Received 1 34 2 16 3 16 4 10 5 12 6 12 Total 100 Instructions: 1. This exam is a closed-book

More information

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012 News CSE 130: Spring 2012 Programming Languages On webpage: Suggested HW #1 PA #1 (due next Fri 4/13) Lecture 2: A Crash Course in ML Please post questions to Piazza Ranjit Jhala UC San Diego Today: A

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh Informatics 1 Functional Programming 19 Tuesday 23 November 2010 IO and Monads Philip Wadler University of Edinburgh The 2010 Informatics 1 Competition Sponsored by Galois (galois.com) List everyone who

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Efficient Mergesort. Christian Sternagel. August 28, 2014

Efficient Mergesort. Christian Sternagel. August 28, 2014 Efficient Mergesort Christian Sternagel August 28, 2014 Abstract We provide a formalization of the mergesort algorithm as used in GHC s Data.List module, proving correctness and stability. Furthermore,

More information

CSCE 314 Programming Languages. Monadic Parsing

CSCE 314 Programming Languages. Monadic Parsing CSCE 314 Programming Languages Monadic Parsing Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a string of characters (or a set of tokens) as input and determines its syntactic structure.

More information

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions Higher-Order Functions CSc 520 Principles of Programming Languages 16: Haskell Higher-Order Functions Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright

More information

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona CSc 372 Comparative Programming Languages 11 : Haskell Higher-Order Functions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2010 Christian Collberg Higher-Order Functions

More information

Theorem Proving Principles, Techniques, Applications Recursion

Theorem Proving Principles, Techniques, Applications Recursion NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1 CONTENT Intro & motivation, getting started with Isabelle Foundations & Principles Lambda Calculus Higher Order Logic,

More information

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 52/37/19/6 (2:30,3:35,11:15,7:30) PS2 due Thursday 9/22 11:59PM Quiz #1 back in section Monday Quiz #2 at start of class on Thursday 9/22 o HOP s, and lots

More information

Programming Languages 3. Definition and Proof by Induction

Programming Languages 3. Definition and Proof by Induction Programming Languages 3. Definition and Proof by Induction Shin-Cheng Mu Oct. 22, 2015 Total Functional Programming The next few lectures concerns inductive definitions and proofs of datatypes and programs.

More information

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia

More information

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

INTRODUCTION TO FUNCTIONAL PROGRAMMING

INTRODUCTION TO FUNCTIONAL PROGRAMMING INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham adapted by Gordon Uszkay 1 What is Functional Programming? Opinions differ, and it is difficult to give a precise definition,

More information

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona 1/20 CSc 372 Comparative Programming Languages 15 : Haskell List Comprehension Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/20 List Comprehensions

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona Defining Functions CSc 372 Comparative Programming Languages 5 : Haskell Function Definitions Department of Computer Science University of Arizona collberg@gmail.com When programming in a functional language

More information

Graphical Untyped Lambda Calculus Interactive Interpreter

Graphical Untyped Lambda Calculus Interactive Interpreter Graphical Untyped Lambda Calculus Interactive Interpreter (GULCII) Claude Heiland-Allen https://mathr.co.uk mailto:claude@mathr.co.uk Edinburgh, 2017 Outline Lambda calculus encodings How to perform lambda

More information

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences] GADTs Advanced functional programming - Lecture 7 Wouter Swierstra and Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree

More information

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple

More information

Monads. Functional Programming (CS4011) Monads

Monads. Functional Programming (CS4011) Monads Monads Functional Programming (CS4011) Andrew Butterfield Glenn Strong Foundations & Methods Group, Discipline of Software Systems Trinity College, University of Dublin {Andrew.Butterfield,Glenn.Strong}@cs.tcd.ie

More information

Programming Languages

Programming Languages CSE 130: Spring 2010 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1 PA #1 (due next Wed 4/9) Please post questions to WebCT Today: A crash

More information

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences] GADTs AFP Summer School Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: 3 A datatype data

More information

CSCE 314 Programming Languages. Monadic Parsing

CSCE 314 Programming Languages. Monadic Parsing CSCE 314 Programming Languages Monadic Parsing Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a string of characters (or a set of tokens) as input and determines its syntactic structure.

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

More information

Functional Programming - 2. Higher Order Functions

Functional Programming - 2. Higher Order Functions Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

More information

2. (18 points) Suppose that the following definitions are made (same as in Question 1):

2. (18 points) Suppose that the following definitions are made (same as in Question 1): 1. (18 points) Suppose that the following definitions are made: import Data.Char data Roadway = Avenue Char Lane [Integer] String Parkway Float Int doze :: Integer -> Bool doze m = even m m > 10 dream

More information

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm. Quick announcement Midterm date is Wednesday Oct 24, 11-12pm. The lambda calculus = ID (λ ID. ) ( ) The lambda calculus (Racket) = ID (lambda (ID) ) ( )

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 74/2/70/17 (2:30,2:30,3:35,7:30) PS2 due Thursday 9/20 11:59PM Guest lecture on Tuesday 9/25 o No RDZ office hours next Friday, I am on travel A brief comment

More information