Haskell 98 in short! CPSC 449 Principles of Programming Languages

Similar documents
Lecture 19: Functions, Types and Data Structures in Haskell

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Chapter 15. Functional Programming Languages

Programming Language Concepts: Lecture 14

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

15. Functional Programming

Types and Programming Languages. Lecture 5. Extensions of simple types

CS 11 Haskell track: lecture 1

Programming Languages

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

Functional Programming Languages (FPL)

Programming Language Concepts: Lecture 19

CS 457/557: Functional Languages

CS558 Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Functional Programming in Haskell Part I : Basics

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

Example Scheme Function: equal

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

Pascal: operations on basic data structures (selection) (I) 2.3 Accessing and manipulating data

Functional Programming and Haskell

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

An introduction introduction to functional functional programming programming using usin Haskell

CS 320: Concepts of Programming Languages

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

Exercise 1 ( = 22 points)

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

Typed Racket: Racket with Static Types

Haskell through HUGS THE BASICS

Chapter 11 :: Functional Languages

2. The object-oriented paradigm!

Functional Programming. Big Picture. Design of Programming Languages

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

Type-indexed functions in Generic Haskell

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

Programming Languages Fall 2013

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

(Refer Slide Time: 4:00)

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. 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

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

Polymorphism and Type Inference

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

Data Types The ML Type System

3. Functional Programming. Oscar Nierstrasz

Computer Programming

Overloading, Type Classes, and Algebraic Datatypes

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

Functional Logic Programming Language Curry

THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE

Programming Paradigms

The PCAT Programming Language Reference Manual

Functional Programming

Lists. Michael P. Fourman. February 2, 2010

CSCI-GA Scripting Languages

Topic 7: Algebraic Data Types

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

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

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

RSL Reference Manual

Introduction to Objective Caml

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Type Systems, Type Inference, and Polymorphism

CS 320: Concepts of Programming Languages

Advanced features of Functional Programming (Haskell)

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Type Checking and Type Inference

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

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

Exercise 1 ( = 24 points)

CMSC 330: Organization of Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

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

CS558 Programming Languages

Lecture #23: Conversion and Type Inference

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

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

Introduction to Programming, Aug-Dec 2006

CS 440: Programming Languages and Translators, Spring 2019 Mon

Logical Methods in... using Haskell Getting Started

Data types for mcrl2

TYPES, VALUES AND DECLARATIONS

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

The Typed Racket Guide

CSE 3302 Programming Languages Lecture 8: Functional Programming

The Algebra of Programming in Haskell

Functional Programming and Modeling

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Programming Systems in Artificial Intelligence Functional Programming

Exercise 1 (2+2+2 points)

FUNCTIONAL PROGRAMMING (1) PROF. SIMON PARSONS

Transcription:

Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has list comprehensions" F allows to define sets with a syntax very similar to"!mathematical set notation!

3.1. Representing data! n Functional programming languages cover a wide spectrum of philosophies regarding data types:! Early languages followed the λ-calculus and had only a very limited number of basic data types (with overlaps) and lists as way to define complex types! Newer languages require type declarations and strong typing, often with a static type system and type inferencing, so that a programmer cannot make any type errors without immediately being told about the error!

Basic data types in Haskell! n Haskell has the following build-in basic data types:! Bool : False or True! Char : the set of Unicode characters! Integer : arbitrary-precision integer numbers! Int : fixed-precision integer, at least [-2 29, 2 29-1]! Float : real floating-point number, single precision! Double : ", double precision!

Building more complex data types! n Haskell allows a programmer to define complex data types using the data statement:"!data Mybool = MyFalse MyTrue n We can use the following ways to define complex data types out of other data types (using data constructors) :! Enumeration! Lists! Tuples! Recursion! List comprehension!

Enumeration! n Enumeration defines directly all elements of a data type by explicitly mentioning them! n Example:" data Weekday = Mo Tu We Th Fr Sa Su n Mo, Tu, We, Th, Fr, Sa, Su are the data elements of the type Weekday.! n They are also so-called constructors (they can be used to construct the data type).! n Constructors always begin with a Capital letter!!

Lists (I)! n Usage of lists as important data structures is very common in functional programming! n Haskell treats lists somewhere between primitive and complex data types, since it already provides list constructors and does not require to explicitly define lists! n If a denotes a data type, then [a] denotes lists over this data type: [Integer] denotes all lists containing integers! n [] denotes the empty list! n The : constructor can also be used to produce lists!

Lists (II)! n Examples:" [2,3,5,1,3]" 2 : 3 : 5 : 1 : 3 (equivalent to first list)! n Traditionally, [] is also called "nil" and : "cons"! n Lists of lists of lists and so on are possible:" data Myprimitivelist = [Bool] data Mycomplexlist = [Bool] [Myprimitivelist] n For lists of numbers we can use ranges, which even allows for infinite lists:" data Positives = [0..] data Evens = [2, 4..]

Tuples! n Tuples are very similar to the record construct in imperative languages like Pascal! n Tuples can either use the tuple constructors (,,) (with n-1 commata, if we define an n-tuple) or, for parameterized tuple types, we can define our own constructors:" data Date = (Day,Month,Year) data CartProduct a = Prod a a a is here a type variable indicating the parameter of the parameterized type! n () is the so-called unit expression and (e) e!

Recursion for defining types! n In Haskell (and other functional languages) the use of parameters in type declarations and the ability to define constructors allows all kinds of recursively defined types:" data Tree a = Leaf a InnerNode (Tree a) (Tree a) The above statement defines binary trees over an arbitrary data type a.! n The parameterized definitions will require polymorphism with regard to functions working on Tree!

List comprehension (I)! n In Mathematics it is rather common to define subsets (or subtypes) of types by defining conditions on the elements of a type a that the subtype b has to fulfill:! Even numbers are all numbers that are dividable by 2 without remainder! A sorted list is a list where each element is smaller or equal to the next element! n This allows instead of writing an algorithm that produces a certain result to just give conditions and let the computer "figure out" how to get there!" F declarative programming! n List comprehension introduces this powerful concept into Haskell!

List comprehension (II)! n General form (right side of data expression):" [ e q 1,,q n ]" where e is an expression describing the base type that is kind of filtered (or extended) by the q i (so-called qualifiers)! n Qualifiers can be! Generators of the form pattern <- expr" (see later)! Guards: arbitrary expressions of type Bool! Local bindings for variables to be used in later qualifiers!

List comprehension (III)! n List comprehensions can be used not only to declare data types but in all kinds of expressions! n [n * n * n n <- [1..50]] defines a list (set) of the first 50 cubes! n factors n = [ i i <- [1..n div 2], n mod i == 0] defines a function that finds all factors of the number n! n data Squares = [n*n n <- [0..]] defines the (infinite) set of all squares"

General remarks to data types in Haskell! n The definition of a data type can already include particular computations that in other paradigms would be part of the statements in a procedure or method! n In order to allow overloading of functions, Haskell has the concept of type classes that help to keep track of what functions have to be defined for a certain type. While there are several similarities to objectoriented classes, the type classes are not describing sets of objects, they are acting more like a Java interface definition. Do not confuse them!!

3.2 Control Constructs! n As already seen in the λ-calculus, functional languages usually do not have explicit constructs that provide alternative paths of execution or loops! n Most functional languages try to follow mathematical notation for defining functions, i.e. they provide several definitions for the function that cover different cases for the input parameters! n This requires (limited) pattern matching capabilities! n Some languages nevertheless provide some explicit control constructs to please certain programmers!

Pattern matching in Haskell (I)! n Pattern matching is a generalization of a case distinction, where we do not just check for a particular value, but for a whole structure (defined by the pattern)! n Pattern in Haskell can contain variables (perhaps better termed placeholders) and either predefined or user-defined constructors! n A given value of a data type fulfills a pattern, if there is a substitution of the variables so that the instantiated pattern evaluates to the given value!

Pattern matching in Haskell (II)! n The substitution for the variables is recorded and the variables can be used within the expression containing the pattern as reference to the particular substitutions.! n The pattern [x, [y,z], [], Prod z 7, (20,x,1066)] " matches [12, [1,2], [], Prod 2 7, (20,12,1066)]" but not [5, [1,2], [], Prod 2 7, (20,12,1066)] (two different substitutions for x)" and not [13, [1,2], [], Prod 2 7, (20,13,1066)]" (if we assume that the last element is of type Date and we limited Month to 1..12)!

Case expressions! n Pattern matching can be directly used to provide several cases of the definition of a function or it can be used within a case expression! n The general form of a case expression is" case (x 1,,x n ) of (p 11,,p 1n ) -> e 1 (p m1,,p mn ) -> e m n Case e 1 of True -> e 2 False -> e 3 defines an if-then-else!

General remarks on control structures in Haskell! n We seem to miss two types of control structures from previous programming paradigms:! Combine single manipulations! Express repetitions of manipulations! n But functional languages do not do manipulations, they evaluate functions! n The do expressions allow to combine function executions, but they are commonly used only for IO (see 3.7)! n Repetitions are achieved by recursion!!