Programming Languages Fall 2013

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

A general introduction to Functional Programming using Haskell

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

Lecture 19: Functions, Types and Data Structures in Haskell

An introduction to functional programming. July 23, 2010

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

Overloading, Type Classes, and Algebraic Datatypes

Programming Languages Fall Prof. Liang Huang

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Haskell through HUGS THE BASICS

Functional Programming in Haskell Part I : Basics

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

CSC324 Principles of Programming Languages

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

CS 320: Concepts of Programming Languages

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

An introduction introduction to functional functional programming programming using usin Haskell

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Programming in Haskell Aug-Nov 2015

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell An Introduction

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Advanced features of Functional Programming (Haskell)

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Haskell Types, Classes, and Functions, Currying, and Polymorphism

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

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

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

Haskell Types COMP360

INTRODUCTION TO HASKELL

CS 11 Haskell track: lecture 1

Topic 9: Type Checking

Topic 9: Type Checking

CS 360: Programming Languages Lecture 12: More Haskell

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Standard prelude. Appendix A. A.1 Classes

CSc 372 Comparative Programming Languages

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Haskell Overview III (3A) Young Won Lim 10/4/16

Introduction to Haskell

Functional programming with OCaml. Programming with OCaml. Fall Software Foundations CIS 500. OCaml and this course. Announcements. features.

Type Checking and Type Inference

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

Haskell Overview II (2A) Young Won Lim 8/23/16

Extended Static Checking for Haskell (ESC/Haskell)

Abstract Types, Algebraic Types, and Type Classes

Programming Paradigms

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Principles of Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Lecture #23: Conversion and Type Inference

Shell CSCE 314 TAMU. Functions continued

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

CS 320: Concepts of Programming Languages

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CSCE 314 Programming Languages

CS558 Programming Languages

Type-indexed functions in Generic Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

CS558 Programming Languages

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

CSC324 Principles of Programming Languages

Programming Paradigms

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

CS 209 Functional Programming

Haskell Syntax in Functions

Lecture 2: List algorithms using recursion and list comprehensions

CSCI-GA Scripting Languages

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.

CS 457/557: Functional Languages

Functional Programming

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Course year Typeclasses and their instances

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

301AA - Advanced Programming

Software System Design and Implementation

Introduction to Programming: Lecture 6

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

Haskell Types COMP360

Imperative languages

More fun with recursion

Exercise 1 ( = 24 points)

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

CSE 3302 Programming Languages Lecture 8: Functional Programming

Functional Programming and Haskell

Lecture 1 Functional Programming

301AA - Advanced Programming [AP-2017]

Programming Languages Fall 2013

Functional Programming I *** Functional Programming and Interactive Theorem Proving. Ulrich Berger Michaelmas Term 2006

Programming Language Concepts: Lecture 14

Introduction to Programming, Aug-Dec 2006

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

3. Functional Programming. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages

Advances in Programming Languages

Transcription:

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 evaluation list and list comprehension recursion pattern matching 2

HW1 discussions -- 1. length mylength1 [] = 0 mylength1 (x:xs) = 1 + (mylength1 xs) mylength2 xs = (sum [1 _ <- xs]) -- note _ -- 2. forall forall p [] = True forall p (x:xs) = (p x) && (forall p xs) -- 3. app app [] a = a app (x:xs) a = x:(app xs a) -- bad: app (x:xs) a = [x] ++ (app xs a) 3

HW1 interleave ghci> interleave [1,2] [3,4] [[1,2,3,4],[1,3,2,4],[1,3,4,2],[3,1,2,4],[3,1,4,2],[3,4,1,2]] ghci> interleave [1,2] [] [[1,2]] ghci> length (interleave "ab" "cdef") 15 interleave [] xs = [xs] interleave xs [] = [xs] interleave (x:xs) (y:ys) = [x:s s <- (interleave xs (y:ys))] ++ [y:s s <- (interleave (x:xs) ys)] 4

HW1 quickselect ghci> qselect 2 [3,10,4,7,19] 4 quickselect i [] = error Bad -- raise Exception quickselect i (p:xs) = if lenleft >= i then quickselect i left else if (lenleft + 1) == i then p else quickselect (i-1-lenleft) right where left = filter (< p) xs right = filter (>= p) xs lenleft = length left using guards notation quickselect i (p:xs) lenleft >= i = quickselect i left (lenleft + 1) == i = p otherwise = quickselect (i-1-lenleft) right where left = filter (< p) xs right = filter (>= p) xs lenleft = length left 5

The guards notation f x x > 0 = 1 otherwise = 0 f x = if x > 0 then 1 else 0 quickselect i (p:xs) lenleft >= i = quickselect i left (lenleft + 1) == i = p otherwise = quickselect (i-1-lenleft) right where left = filter (< p) xs right = filter (>= p) xs lenleft = length left 6

HW1 mergesort ghci> mergesort [2,4,1,5,3] [1,2,3,4,5] ghci> mergesorted [1,2] [3,4,5] [1,2,3,4,5] mergesorted xs [] = xs mergesorted [] ys = ys mergesorted (x:xs) (y:ys) = if x<=y then x:(mergesorted xs (y:ys)) else y:(mergesorted (x:xs) ys) mergesort [] = [] mergesort [x] = [x] mergesort xs = mergesorted (mergesort left) (mergesort right) where (left, right) = (splitat xs ((length xs) `div` 2)) splitat xs 0 = ([], xs) splitat [] i = ([], []) splitat (x:xs) i = (x:left, right) where (left, right) = splitat xs (i-1) 7

HW1 mergesort (guards) ghci> mergesort [2,4,1,5,3] [1,2,3,4,5] ghci> mergesorted [1,2] [3,4,5] [1,2,3,4,5] mergesorted xs [] = xs mergesorted [] ys = ys mergesorted (x:xs) (y:ys) x<=y = x:(mergesorted xs (y:ys)) otherwise = y:(mergesorted (x:xs) ys) mergesort [] = [] mergesort [x] = [x] mergesort xs = mergesorted (mergesort left) (mergesort right) where (left, right) = (splitat ((length xs) `div` 2) xs) splitat 0 xs = ([], xs) splitat i [] = ([], []) splitat i (x:xs) = (x:left, right) where (left, right) = splitat (i-1) xs 8

another mergesort (much better) splitting the list in an alternating fashion: 1-3-5-7; 2-4-6-8 much better for linked-lists (as in all functional languages) Greg will receive extra credit for this elegant solution mergesort [] = [] mergesort [x] = [x] mergesort xs = mergesorted (mergesort left) (mergesort right) where (left, right) = newsplit xs newsplit [] = ([], []) newsplit [x] = ([x], []) newsplit (x:y:xs) = (x:left, y:right) where (left, right) = newsplit xs 9

HW1 permutation ghci> perm [1,2,3] [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] perm [] = [] perm xs = [ x:ys x <- xs, ys <- perm (delete x xs) ] delete x [] = [] delete x (y:ys) x == y = ys otherwise = y:(delete x ys) 10

HW1 tail-recursion fibonacci fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) fibaux prev sum 0 = sum fibaux prev sum n = fibaux sum (prev + sum) (n - 1) fib n = fibaux 1 0 n 11

Today polymorphism and different approaches to typing types, type variables, and type classes currying defining new types recursive data types 12

Types and Type Variables :t command; the a in [a] is a type variable Prelude> :t 'a' 'a' :: Char Prelude> :t True True :: Bool Prelude> :t "HELLO!" "HELLO!" :: [Char] Prelude> :t (True, 'a') (True, 'a') :: (Bool, Char) Prelude> :t 4 == 5 4 == 5 :: Bool Prelude> :t head head :: [a] -> a Prelude> :t last last :: [a] -> a Prelude> :t fst fst :: (a, b) -> a Prelude> :t snd snd :: (a, b) -> b 13

Side Note: Int vs. Integer Int is [system] 32 or 64 bits; Integer is arbitrary precision Prelude> (12345678901234567890 :: Integer, 12345678901234567890 :: Int) (12345678901234567890,-350287150) 14

Type Classes Num is the numeric typeclass (interface) members of Num class function as numbers Eq is the class that supports equality test Ord is the class that has an ordering Ord assumes Eq Prelude> let f x = x + 1 Prelude> :t f f :: Num a => a -> a Prelude> :t (<) (<) :: Ord a => a -> a -> Bool Prelude> :t (==) (==) :: Eq a => a -> a -> Bool Prelude> let f x = x < 5 Prelude> :t f f :: (Num a, Ord a) => a -> Bool Prelude> let f x = x < 5 && x == 3 Prelude> :t f f :: (Num a, Ord a) => a -> Bool 15

Multi-parameter Functions Prelude> let f (x,y) = x+y Prelude> :t f f :: Num a => (a, a) -> a Prelude> let g x y = x+y Prelude> :t g g :: Num a => a -> a -> a Prelude> :t (g 5) (g 5) :: Num a => a -> a 16

Approaches to Typing strongly typed: types are strictly enforced. no implicit type conversion. preventing invalid memory access - weakly typed: not strictly enforced statically typed: type-checking done at compile-time - dynamically typed: types are checked at runtime weak strong static C/C++ Java, ML, Haskell dynamic Perl, VB Python, Lisp/Scheme 17

Polymorphism parametric polymorphism: Haskell, ML functions such as last work uniformly over different arguments ad hoc polymorphism: overloading (C++, Java) operations behave differently when applied to different types subtype polymorphism: Java 18

Defining New Types how to simulate OOP to ensure the type is correct? using keyword data and constructors (like in C++/Java) areaofcircle ( d) = d ^ 2 -- bad surface (Circle ( d)) = d ^ 2 -- good data Bool = False True data Shape = Circle Float Float Float Square Float Float Float Rectangle Float Float Float Float constructors are analogous to functions (very similar to Java) Prelude> :t Circle Circle :: Float -> Float -> Float -> Shape Prelude> :t Rectangle Rectangle :: Float -> Float -> Float -> Float -> Shape surface :: Shape -> Float surface (Circle r) = pi * r ^ 2 surface (Square r) = r ^ 2 surface (Rectangle x1 y1 x2 y2) = (abs (x2 - x1)) * (abs (y2 - y1)) 19

A heterogenous list -- shape.hs data Shape = Circle Float Float Float Square Float Float Float Rectangle Float Float Float Float surface :: Shape -> Float surface (Circle r) = pi * r ^ 2 surface (Square r) = r ^ 2 surface (Rectangle x1 y1 x2 y2) = (abs (x2 - x1)) * (abs (y2 - y1)) Prelude> :l "shape.hs" [1 of 1] Compiling Main ( shape.hs, interpreted ) Ok, modules loaded: Main. *Main> let l = [ Circle 5 5 5, Square 5 6 6 ] *Main> map surface l [78.53982,36.0] 20

Recursive Data Types (trees) data Ast = ANum Integer APlus Ast Ast ATimes Ast Ast eval (ANum x) = x eval (ATimes x y) = (eval x) * (eval y) eval (APlus x y) = (eval x) + (eval y) Prelude> eval (ATimes (APlus (ANum 5) (ANum 6)) (ANum 7)) 77 -- this tree corresponds to the expression ((5+6)*7) 21