Software System Design and Implementation

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

CSC324 Principles of Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS 11 Haskell track: lecture 1

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

Functional Programming and Haskell

CSCI-GA Scripting Languages

Lists. Michael P. Fourman. February 2, 2010

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

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

Overloading, Type Classes, and Algebraic Datatypes

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Haskell An Introduction

CS558 Programming Languages

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

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

CS558 Programming Languages

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

An introduction introduction to functional functional programming programming using usin Haskell

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages Fall 2013

Introduction. chapter Functions

Lecture 8: Summary of Haskell course + Type Level Programming

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Haskell through HUGS THE BASICS

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

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

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

Imperative languages

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

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

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

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

Simon Peyton Jones Microsoft Research August 2012

CSCE 314 Programming Languages

Programming with Math and Logic

A general introduction to Functional Programming using Haskell

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

Lecture 4: Higher Order Functions

Functional Programming

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

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

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

Lecture 19: Functions, Types and Data Structures in Haskell

INTRODUCTION TO HASKELL

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Recursive Types and Subtyping

3. Functional Programming. Oscar Nierstrasz

CS558 Programming Languages

Chapter 11 :: Functional Languages

Type Systems, Type Inference, and Polymorphism

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

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

Introduction to Functional Programming and Haskell. Aden Seaman

Logic - CM0845 Introduction to Haskell

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

Example: Haskell algebraic data types (1)

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

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CS 11 Ocaml track: lecture 2

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Software System Design and Implementation

CS457/557 Functional Languages

Algebraic Types. Chapter 14 of Thompson

Type Checking and Type Inference

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CMSC 330: Organization of Programming Languages

Structural polymorphism in Generic Haskell

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CS 440: Programming Languages and Translators, Spring 2019 Mon

Functional Programming

CS 320: Concepts of Programming Languages

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

Functional Programming Lecture 1: Introduction

CS 457/557: Functional Languages

Programming Systems in Artificial Intelligence Functional Programming

User-Defined Algebraic Data Types

Programming Paradigms

Programming Languages Lecture 14: Sum, Product, Recursive Types

Some Advanced ML Features

Lecture 2: List algorithms using recursion and list comprehensions

Haskell Types COMP360

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Higher Order Functions in Haskell

Introduction to Functional Programming in Haskell 1 / 56

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

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

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

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

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Data Types The ML Type System

Dependencies by case or by function?

Graphical Untyped Lambda Calculus Interactive Interpreter

Transcription:

Software System Design and Implementation Functional Programming Gabriele Keller The University of New South Wales School of Computer Science and Engineering Sydney, Australia COMP3141 16s1

Course software How to install Haskell (see 'Course Software' on COMP3141 website) If you re using a Mac and want the Haskell for Mac IDE, please reply to my email

What is functional programming?

Swift Agda Common properties of functional languages HasKell Functions are the main device to structure programs Racket Based on the lambda calculus Standard ML OCaml The use of higher-order functions is encouraged F# Lisp Side effects are used in a disciplined manner (pure functions) Idris Sophisticated type systems (though, the Lisp family is dynamically typed) Clojure Scala Maple Scheme Scheme

Functional programming with Haskell

Haskell Broad-spectrum programming language Widely used with over 2500 open-source libraries and tools Haskell is a principled language Haskell B. Curry Purely functional Strictly isolating side effects Strongly typed with a surprisingly expressive type system http://haskell.org/

Simple Functions in Haskell harmonicmean x y = (2 * x * y)/(x + y) function name parameters body

Functions in Haskell Optional type annotation harmonicmean :: Double -> Double -> Double harmonicmean x y = (2 * x * y)/(x + y) C double harmonicmean(double x, double y) { return (2 * x * y/(x + y)); }

C double harmonicmean(double x, double y) { double prod = 2 * x * y; double sum = x + y; return (prod / sum); } Haskell Demo Optional, but considered good style harmonicmean:: Double -> Double -> Double harmonicmean x y = prod / sum where prod :: Double prod = 2 * x * y sum :: Double sum = x + y Can not be updated! Just like in math. Usually omitted

Simple functions Selection between multiple equations is by pattern matching or guards Patterns fact :: Int -> Int fact 0 = 1 fact n = n * fact (n - 1) Guards fact :: Int -> Int fact n n == 0 = 1 otherwise = n * fact (n - 1) int fact(int n) { int res = 1; while (n > 0) { res = res * n; n ; } return (res); }

Constants are simply functions with no arguments theanswer :: Int theanswer = 2 * 21

-> is right associative, that is is the same as harmonicmean :: Double -> Double -> Double harmonicmean :: Double -> (Double -> Double) can be interpreted as a function that takes one Double value, and returns a new function as result harmonicmean :: Double -> Double -> Double harmonicmean x y = (2 * x * y)/(x + y) janesfinal :: Double -> Double janesfinal exammark = harmonicmean 89 exammark

-> is right associative, that is is the same as harmonicmean :: Double -> Double -> Double harmonicmean :: Double -> (Double -> Double) can be interpreted as a function that takes one Double value, and returns a new function as result harmonicmean :: Double -> Double -> Double harmonicmean x y = (2 * x * y)/(x + y) janesfinal :: Double -> Double janesfinal exammark = harmonicmean 89 exammark jebsfinal :: Double -> Double jebsfinal = harmonicmean 75

Type inference Compiler can work out (nearly) all types by itself You can prototype, leaving out the types, and add them later Type signatures are documentation The compiler makes sure the documentation is in sync with the code Type signatures catch bugs early compiler complains if they are wrong! This is the best of both worlds!

Higher-order functions applytwice :: (Double -> Double) -> Double -> Double applytwice fn x = fn (fn x) > applytwice sqrt 81 > applytwice (\x -> x + 5) 81 Lambda expression

Parametric polymorphism Polymorphism has a different meaning in FP than in OO Java's generics correspond to parametric polymorphism Type variable a a a a applytwice :: (Double -> Double) -> Double -> Double applytwice fn x = fn (fn x)

Types

Pre-defined types We already saw function types: a -> b We also saw elementary types: Int, Float, Double, Char, and so on Tuples group multiple types: (), (a, b), (a, b, c), and so on harmonicmeant :: (Double, Double) -> Double harmonicmeant (x, y) = (2 * x * y)/(x + y) harmonicmeant :: (Double, Double) -> Double harmonicmeant pxy = (2 * (fst pxy) * (snd pxy))/(fst pxy) + (snd pxy)) fst :: (a, b) -> a functions defined in Prelude snd :: (a, b) -> b

Lists Pronounced "nil" Lists are either empty: [] or consist of a head and a tail: x : xs Pronounced "cons" Lists are homogenous all elements in one list have the same type List are parametric different lists may contain elements of different type

Some operations on lists Length of a list Concatenating two lists Reversing the elements of a list Mapping a function over a list In Haskell!

User-defined types Type synonyms (typedefs in C) type Point = (Float, Float) type Path = [Point] Algebraic data types Combination of structs and unions together with pointers in C

Data types can be like structs in C (we call those data types product types) data Point2 = MkPoint2 Float Float this is called a data constructor fields are not named, characterised by position in the definition typedef struct { unsigned int x, y; } Point2; The corresponding definition in C

data Point2 = MkPoint2 Float Float point :: Point2 point = MkPoint2 1.3 2.45 typedef struct { unsigned int x, y; } Point2; Point2 point = {1.3, 2.45}; // or Point2 point; point.x = 1.3; point.y = 2.45

data Point2 = MkPoint2 { xpoint :: Float, ypoint :: Float } fields can also be named point :: Point2 point = MkPoint2 1.3 2.45 or point = MkPoint2 {ypoint = 2.45, xpoint = 1.3} typedef struct { unsigned int x, y; } Point2; Point2 point = {1.3, 2.45}; // or Point2 point; point.x = 1.3; point.y = 2.45

data Point2 = MkPoint2 { xpoint :: Float, ypoint :: Float } distance :: Point2 -> Point2 -> Float distance (MkPoint2 x1 y1) (MkPoint2 x2 y2) = sqrt ((x2 - x1)^2 + (y2 - y1)^2)

Problem: define a type to model hostnames, which can be either symbolic (string) or numeric address (4 integer values) Data types can be like unions in C (we call those data types sum types) data Host = MkNumericIP Int Int Int Int MkSymbolicIP String enum tag {NUMERIC_IP, SYMBOLIC_IP}; struct mknumericip { enum tag thetag; unsigned short a, b, c, d; } struct mksymbolicip { enum tag thetag; char *hostname; } typedef union { struct mknumericip anumericip; struct mksymbolicip asymbolicip; } Host; The definition in C

Product-Sum Types We call Haskell's data types also product-sum types They can be recursive as well In contrast to data types in C, but much like generics in Java and C#, Haskell data types can be parameterised Type parameter data Maybe a = Nothing Just a

Identifiers in Haskell Alphanumeric with underscores (_) and prime symbols (') Case matters Functions & variables lower case map, pi, (+), (++) Data constructors Upper case True, Nothing, (:) Type variables lower case a, b, c, elttype Type constructors Upper case Int, Bool, IO

A larger example: Fractal trees