Programming in Haskell Aug-Nov 2015

Similar documents
Introduction to Programming: Lecture 3

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Introduction to Programming, Aug-Dec 2006

Functional Programming in Haskell Part I : Basics

Programming Language Concepts: Lecture 14

Programming in Haskell Aug-Nov 2015

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

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

Programming in Haskell Aug-Nov 2015

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

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

An introduction introduction to functional functional programming programming using usin Haskell

Lecture 2: List algorithms using recursion and list comprehensions

Composite Datatypes Lists and Tuples

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CSCE 314 Programming Languages

02157 Functional Programming Tagged values and Higher-order list functions

CS 320: Concepts of Programming Languages

Haskell Types COMP360

Introduction to Programming, Aug-Dec 2008

1. (34 points) Suppose that the following definitions are made: (a) (22 points) Give the values of the following expressions.

CS 457/557: Functional Languages

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

1. (34 points) Suppose that the following definitions are made: (a) (22 points) Give the values of the following expressions.

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

Lecture 1 August 9, 2017

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Overloading, Type Classes, and Algebraic Datatypes

Lecture 10 September 11, 2017

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

Haskell An Introduction

Logical Methods in... using Haskell Getting Started

CSE 3302 Programming Languages Lecture 8: Functional Programming

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

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

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

Programming in Haskell Aug Nov 2015

Lecture 1 Functional Programming

Programming Languages Fall 2013

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

A general introduction to Functional Programming using Haskell

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Introduction to Programming: Lecture 6

Lecture #23: Conversion and Type Inference

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc Introduction to Computing

CSI33 Data Structures

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

Haskell Refresher Informatics 2D

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

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

Lecture 19: Functions, Types and Data Structures in Haskell

Advanced features of Functional Programming (Haskell)

Haskell through HUGS THE BASICS

Strings, Lists, and Sequences

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

CS 11 Haskell track: lecture 1

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

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

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

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016


Set Haskell Exercises

CSc 520. Principles of Programming Languages 11: Haskell Basics

CS1 Lecture 3 Jan. 18, 2019

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

CIS 252 Introduction to Computer Science Exam 1 (Green) October 3, Points Possible

CSCE 314 Programming Languages

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

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

CS1 Lecture 3 Jan. 22, 2018

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

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

Problem Grade Total

Structured programming. Exercises 3

Monads. COS 441 Slides 16

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

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

1. (34 points) Suppose that the following definitions are made: (a) (22 points) Give the values of the following expressions.

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Functional Programming Mid-term exam Tuesday 3/10/2017

Haskell-Tutorial. Damir Medak Gerhard Navratil. Institute for Geoinformation Technical University Vienna. February 2003

User-Defined Algebraic Data Types

Console input 26/09/2018. Background. Background

Topic 9: Type Checking

Topic 9: Type Checking

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

Lecture 2: The Basis of SML

Lecture 1. Types, Expressions, & Variables

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

Haskell Types COMP360

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Transcription:

Programming in Haskell Aug-Nov 2015 LECTURE 5 AUGUST 18, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE

The datatype Char Values are written with single quotes a, 3, %, #, Character symbols stored in a table (e.g. ASCII) Functions ord and chr connect characters and table Inverses: c == chr (ord c), j == ord (chr j) Note: import Data.Char to use ord and char

Example: capitalize Function to convert lower case to upper case Brute force, enumerate all cases capitalize :: Char -> Char capitalize a = A capitalize b = B capitalize z = Z capitalize ch = ch

Example: capitalize Can assume that a,, z and A,, Z and 0,, 9 each have consecutive ord values A more intelligent solution capitalize :: Char -> Char capitalize ch ( a <= ch && ch <= z ) = chr (ord ch + (ord A - ord a )) otherwise = ch

Strings A string is a sequence of characters In Haskell, String is a synonym for [Char] [ h, e, l, l, o ] == hello == [] Usual list functions can be used on String length, reverse,

Example: occurs Search for a character in a string occurs c s returns True if c is found in s occurs :: Char -> String -> Bool occurs c = False occurs c (x:xs) c == x = True otherwise = occurs c xs

Example: touppercase Convert an entire string to uppercase Apply capitalize to each character touppercase :: String -> String touppercase = touppercase (c:cs) = (capitalize c): (touppercase cs) Apply f to each element in a list will come back to this later

Example: position position c s : first position in s where c occurs Return length s if no occurrence of c in s position a battle axe 1 position d battle axe 10 position :: Char -> String -> Int position c = 0 position c (d:ds) c == d = 0 otherwise = 1 + (position c ds)

Example: Counting words wordc : count the number of words in a string Words separated by white space:, \t, \n whitespace :: Char -> Bool whitespace = True whitespace \t = True whitespace \n = True whitespace _ = False

Example: Counting words Count white space in a string? wscount :: String -> Int wscount = 0 wscount (c:cs) whitespace c = 1 + wscount cs otherwise = wscount cs Not enough! Consider abc d

Example: Counting words Keep track of whether we are inside a word or outside a word Outside a word: ignore whitespace, but non-whitespace starts a new word Inside a word: ignore non-whitespace, but whitespace ends current word Count the number of times a new word starts

Example: Counting words New word starts whenever a non-whitespace follows a whitespace insert initial space to catch first character wordcaux :: String -> Int wordcaux [c] = 0 wordcaux (c:d:ds) (whitespace c) && not (whitespace d) = 1 + wordcaux (d:ds) otherwise = wordcaux (d:ds) wordc :: String -> Int wordc s = wordcaux ( :s)

Tuples Keep multiple types of data together Student info: Name, ID, Date of birth ( Abhirup, 2106, Jan 1, 2000 ) List of marks in a course [( Sasha,95), ( Becky,95), ( Charlotte,98)]

Tuples.. Tuple type (T1,T2,,Tn) groups together multiple types (3,-21) :: (Int,Int) (13, True, 97) :: (Int, Bool, Int) ([1,2],73) :: ([Int],Int)

Pattern matching Use tuple structure for pattern matching Sum pairs of integers sumpairs :: (Int,Int) -> Int sumpairs (x,y) = x+y Sum pairs of integers in a list of pairs sumpairlist :: [(Int,Int)] -> Int sumpairlist [] = 0 sumpairlist (x,y):zs = x + y + sumpairlist zs

Example: Marks list List of pairs (Name,Marks) [(String,Int)] Given a name, find the marks lookup :: String -> [(String,Int)] -> Int lookup p [] = -1 lookup p ((name,marks):ms) (p == name) = marks otherwise = lookup p ms

Type aliases Tedious to keep writing [(String,Int)] Introduce a new name for this type type Marklist = [(String,Int)] Then lookup :: String -> Marklist -> Int

Type aliases A type definition only creates an alias for a type Both Marklist and [(String,Int)] are the same type String is a type alias for [Char]

Example: Point type Point2D = (Float,Float) distance :: Point2D -> Point2D -> Float distance (x1,y1) (x2,y2) = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) type Point3D = (Float,Float,Float) distance :: Point3D -> Point3D -> Float distance (x1,y1,z1) (x2,y2,z2) = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1))

Type aliases are same type Suppose f :: Float -> Float -> Point2D g :: (Float,Float) -> (Float,Float) ->Float Then g (f x1 x2) (f y1 y2) is well typed f produces Point2D that is same as (Float,Float)

Local definitions Let us return to distance type Point2D = (Float,Float) distance :: Point2D -> Point2D -> Float distance (x1,y1) (x2,y2) = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) Introduce sqr to simplify expressions

Local definitions sqr :: Float -> Float sqr x = x*x type Point2D = (Float,Float) distance :: Point2D -> Point2D -> Float distance (x1,y1) (x2,y2) = sqrt(sqr (x2-x1) + sqr (y2-y1)) But now, auxiliary function sqr is globally available

Local definitions type Point2D = (Float,Float) distance :: Point2D -> Point2D -> Float distance (x1,y1) (x2,y2) = sqrt(sqr (x2-x1) + sqr (y2-y1)) where sqr :: Float -> Float sqr x = x*x Definition of sqr is now local to distance

Local definitions Another motivation type Point2D = (Float,Float) distance :: Point2D -> Point2D -> Float distance (x1,y1) (x2,y2) = sqrt(xdiff*xdiff + ydiff*ydiff) where xdiff :: Float xdiff = x2 - x1 ydiff :: Float ydiff = y2 - y1

Local definition xdiff*xdiff vs (x2-x1)*(x2-x1) With xdiff*xdiff, (x2-x1) is only computed once In general, ensure that common subexpressions are evaluated only once

Summary Tuples allow different types to come together in a single unit Pattern matching to extract individual components type statement creates type aliases where allows local definitions