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

Similar documents
Haskell An Introduction

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

Functional Programming and Haskell

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

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

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

Imperative languages

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

Lecture 19: Functions, Types and Data Structures in Haskell

Programming Languages Fall 2013

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Principles of Programming Languages

Exercises on ML. Programming Languages. Chanseok Oh

An introduction introduction to functional functional programming programming using usin Haskell

An introduction to functional programming. July 23, 2010

CS 11 Haskell track: lecture 1

SML A F unctional Functional Language Language Lecture 19

A general introduction to Functional Programming using Haskell

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

Haskell through HUGS THE BASICS

Software System Design and Implementation

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

Functional Programming for Logicians - Lecture 1

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSCI-GA Scripting Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

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.

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

Programming Languages Fall Prof. Liang Huang

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Types COMP360

LECTURE 16. Functional Programming

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to Haskell

CSCE 314 Programming Languages

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

The type checker will complain that the two branches have different types, one is string and the other is int

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

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

301AA - Advanced Programming [AP-2017]

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CS 320: Concepts of Programming Languages

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Standard prelude. Appendix A. A.1 Classes

INTRODUCTION TO HASKELL

CSE 3302 Programming Languages Lecture 8: Functional Programming

Background Type Classes (1B) Young Won Lim 6/14/18

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Background Type Classes (1B) Young Won Lim 6/28/18

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

Lecture #23: Conversion and Type Inference

CS 360: Programming Languages Lecture 12: More Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

Programming Paradigms

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

G Programming Languages - Fall 2012

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

3. Functional Programming. Oscar Nierstrasz

CSC324 Principles of Programming Languages

Lists. Michael P. Fourman. February 2, 2010

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Overloading, Type Classes, and Algebraic Datatypes

Exercise 1 ( = 24 points)

Advances in Programming Languages

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

Functional Programming in Haskell Part I : Basics

Logic - CM0845 Introduction to Haskell

Logical Methods in... using Haskell Getting Started

Programming Systems in Artificial Intelligence Functional Programming

Chapter 15. Functional Programming Languages

Introduction to Functional Programming and Haskell. Aden Seaman

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Lazy Functional Programming in Haskell

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Programming Paradigms

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Little and not so big bits of Haskell

CSCE 314 Programming Languages

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

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Transcription:

Lecture 7: Haskell CSC 131 Fall, 2014 Kim Bruce According to Larry Wall (designer of PERL): a language by geniuses for geniuses He s wrong at least about the latter part though you might agree when we talk about monads Haskell 98 Haskell (cont) Purely functional Functions are first-class values Statically scoped Strong, static typing via type inference - Type-safe Parametric polymorphism Type classes Rich type system including support for ADT s Non-strict (lazy) evaluation Imperative features emulated using monads. Garbage collection Compiled or interpreted. Named after Haskell Curry -- early contributor to lambda calculus and combinatory logic

Read Haskell Tutorials Using GHC All on links page from course web page I like Learn you a Haskell for greater good O Reilly text: Real World Haskell free on-line Print Haskell cheat sheet Use The Haskell platform, available at - http://www.haskell.org/ to enter interactive mode type: ghci - :load myfile.hs -- :l also works - after changes type :reload myfile.hs - Control-d to exit - :set +t -- prints more type info when interactive - it is result of expression Built-in data types Unit has only ( ) Bool: True, False with not, &&, Int: 5, -5, with +, -, *, ^, =, /=, <, >, >=,... - div, mod defined as prefix operators (`div` infix) - Int fixed size (usually 64 bits) - Integer gives unbounded size Float, Double: 3.17, 2.4e17 w/ +, -, *, /, =, <, >, >=, <=, sin, cos, log, exp, sqrt, sin, atan. Char: n More Basic Types String = [Char], not really primitive - "hello"++" there", length list of Char - No substring, but `isinfixof` for all lists - Also isprefixof`, `issuffixof Prefix op w/out `` import Data.List Type classes (later) provide relations between classes.

Interactive Programming with ghci Lists Type expressions and run-time will evaluate Define abbreviations with let - let double n = n + n - let seven = 7 let not necessary at top level in programs loaded from files Lists - [2,3,4,9,12]: [Integer] - [] -- empty list - Must be homogenous - Functions: length, ++, :, map, rev also head, tail, but normally don t use Polymorphic Types Pattern Matching [1,2,3]:: [Integer] [ abc, def ]:: [[Char]],... []:: [a] map:: (a b) ([a] [b]) Use :t exp to get type of exp Decompose lists: - [1,2,3] = 1:(2:(3:[])) Define functions by cases using pattern matching: prod [] = 1 prod (fst:rest) = fst * (prod rest)

Pattern Matching Type constructors Desugared through case expressions: - head' :: [a] -> a head' [] = error "No head for empty lists" head' (x:_) = x equivalent to - head' xs = case xs of [] -> error "No head for empty lists" (x:_) -> x Tuples - (17, abc, True) : (Integer, [Char], Bool) - fst, snd defined only on pairs Records exist as well More Pattern Matching Static Typing Strongly typed via type inference (x,y) = (5 `div` 2, 5 `mod` 2) hd:tl = [1,2,3] hd:_ = [4,5,6] - _ is wildcard. - head:: [a] a tail:: [a] [a] - last [x] = x last (hd:tail) = last tail System deduces most general type, [a] -> a - Look at algorithm later

Static Scoping What is the answer? let x = 3" let g y = x + y" g 2" let x = 6" - g 2" What is the answer in original LISP? (define x 3)" (define (g y) (+ x y))" (g 2)" (define x 6)" - (g 2) What is the answer? let x = 3" let g y = x + y" g 2" let x = 6" - g 2 Static Scoping What is the answer in original LISP? (define x 3)" (define (g y) (+ x y))" (g 2)" (define x 6)" - (g 2) { const x = 3 { g(y) = x + y { print (g 2) const x = 6 { print (g 2) } } } } Local Declarations roots (a,b,c) = " let -- indenting is significant" disc = sqrt(b*b-4.0*a*c)" in" ((-b + disc)/(2.0*a),(-b - disc)/(2.0*a))" *Main> roots(1,5,6)" (-2.0,-3.0)" or roots' (a,b,c) = ((-b + disc)/(2.0*a), (-b - disc)/(2.0*a))" where disc = sqrt(b*b-4.0*a*c) Anonymous functions dble x = x + x abbreviates" dble = \x -> x + x

Defining New Types Type abbreviations - type Point = (Integer, Integer) - type Pair a = (a,a) data definitions - create new type with constructors as tags. - generative data Color = Red Green Blue See more complex examples later Type Classes Intro Specify an interface: - class Eq a where (==) :: a -> a -> Bool -- specify ops (/=) :: a -> a -> Bool x == y = not (x /= y) -- optional implementations x /= y = not (x == y) - data TrafficLight = Red Yellow Green instance Eq TrafficLight where Red == Red = True Green == Green = True Yellow == Yellow = True _ == _ = False Common Type Classes Eq, Ord, Enum, Bounded, Show, Read - See http://www.haskell.org/tutorial/stdclasses.html data defs pick up default if add to class: - data... deriving (Show, Eq) Can redefine: - instance Show TrafficLight where show Red = "Red light" show Yellow = "Yellow light" show Green = "Green light" More Type Classes class (Eq a) => Num a where... - instance of Num a must be Eq a Polymorphic function types can be prefixed w/ type classes - test x y = x < y has type (Ord a) => a -> a -> Bool - Can be used w/ x, y of any Ord type." More later..." - Error messages often refer to actual parameter needing to be instance of a class -- to have an operation.

Higher-Order Functions Functions that take function as parameter - Ex: map:: (a b) ([a] [b]) Build new control structures - listify oper identity [] = identity listify oper identity (fst:rest) = oper fst (listify oper identity rest) - sum' = listify (+) 0 mult' = listify (*) 1 and' = listify (&&) True or' = listify ( ) False Quicksort partition (pivot, []) = ([],[]) " partition (pivot, first : others) = " let " (smalls, bigs) = partition(pivot, others) " in " if first < pivot " " then (first:smalls, bigs) " " else (smalls, first:bigs)" Type is: " partition :: (Ord a) => (a, [a]) -> ([a], [a]) Quicksort qsort [] = [] " qsort [singleton] = [singleton] " qsort (first:rest) = " let " """ (smalls, bigs) = partition(first,rest) " ""in " """ qsort(smalls) ++ [first] ++ qsort(bigs)" Type is: " qsort :: (Ord t) => [t] -> [t] Quicksort - parametrically partition (pivot, []) lthan = ([],[]) " partition (pivot, first : others) lthan = " let " (smalls, bigs) = partition(pivot, others) lthan in " if (lthan first pivot) " then (first:smalls, bigs) " else (smalls, first:bigs) " partition :: " (t, [a]) -> (a -> t -> Bool) -> ([a], [a])" *Main> partition(6,[8,4,6,3])(>)"