CSc 520. Principles of Programming Languages 11: Haskell Basics

Similar documents
CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

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

Standard prelude. Appendix A. A.1 Classes

Basic types and definitions. Chapter 3 of Thompson

Haskell through HUGS THE BASICS

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

CSc 372 Comparative Programming Languages

CS 320: Concepts of Programming Languages

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

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

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

A tour of the Haskell Prelude

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

Topic 1: Introduction

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 320: Concepts of Programming Languages

1 Lexical Considerations

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lexical Considerations

REVIEW. The C++ Programming Language. CS 151 Review #2

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

CS 11 Haskell track: lecture 1

Logical Methods in... using Haskell Getting Started

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Lexical Considerations

Functional Programming and Haskell

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

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

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

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

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Preview from Notesale.co.uk Page 6 of 52

Fundamentals of Programming. Lecture 11: C Characters and Strings

Introduction to Programming and 4Algorithms Abstract Types. Uwe R. Zimmer - The Australian National University

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

GHCi: Getting started (1A) Young Won Lim 5/26/17

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

CS3157: Advanced Programming. Outline

today cs3157-fall2002-sklar-lect05 1

CS 320: Concepts of Programming Languages

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

CSc 520 Principles of Programming Languages

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Haskell Types COMP360

Programming Language Concepts, cs2104 Lecture 04 ( )

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Course Outline. Introduction to java

Information Science 1

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

The PCAT Programming Language Reference Manual

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

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

A quick introduction to SML

Haskell 98 in short! CPSC 449 Principles of Programming Languages

SMURF Language Reference Manual Serial MUsic Represented as Functions

Lecture 2: List algorithms using recursion and list comprehensions

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Functional Programming in Haskell Part I : Basics

Computational Physics Operating systems

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CIS 110: Introduction to Computer Programming

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Information Science 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Introduction to Programming, Aug-Dec 2008

GOLD Language Reference Manual

Learning the Language - V

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Programming Languages Fall 2013

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Intermediate Code Generation

Introduction to Programming Using Java (98-388)

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

L-System Fractal Generator: Language Reference Manual

Functional Programming I

Getting Started With Pascal Programming

Functions. Arash Rafiey. September 26, 2017

Lecture 1 August 9, 2017

Haskell Scripts. Yan Huang

Beyond Blocks: Python Session #1

Chapter 2: Introduction to C++

The current topic: Python. Announcements. Python. Python

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Introduction to C# Applications

Transcription:

CSc 520 Principles of Programming Languages 11: Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April 22, 2005 1 Basic Types Int As we ve seen, Haskell supports the integer (Int) type. Integer Operators: ^ 8 right Exponentiation *, / 7 left Mul, Div div 7 free Division rem 7 free Remainder 2 Basic Types Int... mod 7 free Modulus +, - 6 left Add, Subtract ==,/= 4 free (In-) Equality <,<=,>,>= 4 free Relational Comparison 1-3 (1)-3 1*3 1+(2*3) 2^3^4 2^(3^4) 4==5==6 ERROR 12/6/3 ERROR 12/(6/3) 6 3 Basic Types Bool There are two boolean literals, and 1

&& 3 right logical and 2 right logical or not 9 logical not 3 < 5 && 4 > 2 (3 < 5) && (4 > 2) && ( && ) 4 Haskell Functions Here s the ubiquitous factorial function: fact :: fact n = Int -> Int if n == 0 then 1 else n * fact (n-1) The first part of a function definition is the type signature, which gives the domain and range of the function: fact :: Int -> Int The second part of the definition is the function declaration, the implementation of the function: fact n = if n == 0 then 5 Haskell Functions... The syntax of a type signature is fun name :: arg types fact takes one integer input argument and returns one integer result. The syntax of function declarations: fun name param names = fun body fact is defined recursively, i.e. the function body contains an application of the function itself. Function application examples: fact 1 1 fact 5 120 fact (3) 120 2

6 Basic Types Char Literals: a, b. Special characters: \n (newline). ASCII: \65 (decimal), \x41 (hex). toupper, isalpha, etc, are defined in the standard prelude. ord :: Char -> Int char :: Int -> Char toupper, tolower :: Char -> Char isascii,isdigit, :: Char -> Bool isupper,islower, :: Char -> Bool ord a 97 chr 65 A toupper a A isdigit a 7 Basic Types Tuples Built-in Functions: A Haskell tuple is similar to a Pascal record it is a collection of objects of (a limited number of) objects, possibly of different types. Each Pascal record elements has a unique name, whereas in Haskell you distinguish between elements by their position in the tuple. Syntax: (t 1, t 2,, t n ). type Complex = (Float,Float) mkcomplex :: Float -> Float -> Complex mkcomplex re im = (re, im) Examples: 8 Basic Types Tuples... type Complex = (Float,Float) mkcomplex :: Float -> Float -> Complex mkcomplex re im = (re im) mkcomplex 5 3 (5, 3) addcomplex :: Complex -> Complex -> Complex addcomplex (a,b) (c,d) = (a+c,b+d) addcomplex (mkcomplex 5 3) (mkcomplex 4 2) (9,5) 9 The Offside Rule When does one function definition end and the next one begin? cube x = Textual layout determines when definitions begin and end. 3

10 The Offside Rule... The first character after the "=" opens up a box which holds the right hand side of the equation: Any character to the left of the line closes the box and starts a new definition: cube x =... 11 Readings and References A free implementation ftp://ftp.dcs.gla.ac.uk/pub/haskell/gofer. Compiler and interpreter available on linux: /home/cs520/2003/bin/linux/gofer. You can also use the Haskell compiler on lectura: /home/cs520/2003/ghc-5.04.1/bin/sparc-sun-solaris2/ghci. http://dmoz.org/computers/programming/languages/haskell. 12 Summary Haskell has all the basic types one might expect: Ints, Chars, Floats, and Bools. Haskell functions come in two parts, the signature and the declaration: fun name :: argument types fun name param names = fun body Many Haskell functions will use recursion. Haskell doesn t have assignment statements, loop statements, or procedures. Haskell tuples are similar to records in other languages. 13 Homework 1. Start (Mac Unix) Haskell. 2. Enter the commaint function and try it out. 3. Enter the addcomplex and mkcomplex functions and try them out. 4. Turn on tracing (Options:Trace Reductions in MacHaskell) and try the functions again. 5. Try the standard functions fst x and snd x on complex values. What do fst and snd do? 6. Try out the Eliza application in Demos:Eliza. 4

14 Homework... Write a Haskell function to check if a character is alphanumeric, i.e. a lower case letter, upper case letter, or digit.? isalphanum a? isalphanum 1? isalphanum A? isalphanum ;? isalphanum @ 15 Homework... Define a Haskell exclusive-or function. eor :: Bool -> Bool -> Bool eor x y =? eor? eor? eor? eor 16 Homework... Define a Haskell function chartoint which converts a digit like 8 to its integer value 8. The value of non-digits should be taken to be 0. chartoint :: Char -> Int chartoint c =? chartoint 8 8? chartoint 0 0? chartoint y 0 5