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

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

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

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

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

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Standard prelude. Appendix A. A.1 Classes

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

Topic 1: Introduction

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

Basic types and definitions. Chapter 3 of Thompson

CS 320: Concepts of Programming Languages

A tour of the Haskell Prelude

CS 320: Concepts of Programming Languages

Haskell Scripts. Yan Huang

Logical Methods in... using Haskell Getting Started

CS 11 Haskell track: lecture 1

Introduction to Programming, Aug-Dec 2008

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

Variables, Constants, and Data Types

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

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

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

CIS552: Advanced Programming

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

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages

CS 320: Concepts of Programming Languages

F28PL1 Programming Languages. Lecture 11: Standard ML 1

1 Lexical Considerations

The PCAT Programming Language Reference Manual

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Lexical Considerations

Course Outline. Introduction to java

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Algorithms and Programming I. Lecture#12 Spring 2015

SMURF Language Reference Manual Serial MUsic Represented as Functions

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

Lexical Considerations

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

Language Reference Manual simplicity

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

Programming Paradigms

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

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

CS 106 Introduction to Computer Science I

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Declaration and Memory

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

Functional Programming and Haskell

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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

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

3. Java - Language Constructs I

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

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

Jython. secondary. memory

Building Java Programs

Ex: If you use a program to record sales, you will want to remember data:

4. Inputting data or messages to a function is called passing data to the function.

The current topic: Python. Announcements. Python. Python

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

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

An introduction introduction to functional functional programming programming using usin Haskell

CIS 110: Introduction to Computer Programming

Computer Components. Software{ User Programs. Operating System. Hardware

Building Java Programs

Cellular Automata Language (CAL) Language Reference Manual

Lecture 1. Types, Expressions, & Variables

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

4 Programming Fundamentals. Introduction to Programming 1 1

INTRODUCTION TO FUNCTIONAL PROGRAMMING

The SPL Programming Language Reference Manual

CS112 Lecture: Primitive Types, Operators, Strings

Topic 9: Type Checking

Topic 9: Type Checking

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

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

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

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

SWEN-250 Personal SE. Introduction to C

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

Transcription:

1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/40 The Hugs Interpreter The Haskell implementation we will be using is called Hugs. You interact with Hugs by typing commands to the interpreter, much like you would to a powerful calculator: $ hugs > 6 * 7 42 > 126 div 3 4

3/40 The Hugs Interpreter... Haskell programs (known as scripts) are just text files with function definitions that can be loaded into the interpreter using the :load script command: $ hugs > :load file.hs Haskell scripts take the file extension.hs.

Haskell Types

5/40 Expressions When we run a Haskell program, we actually evaluate an expression, and the result of the program is the value of that expression. Unlike Java programs. Haskell programs have no statements there is no way to assign a new value to a variable for example.

6/40 Haskell Types Haskell is strongly typed. This means that every expression has exactly one type. Haskell is statically typed. This means that the type of an expression can be figured out before we run the program. The basic types in Haskell include 1 Int (word-sized integers) 2 Integer (arbitrary precision integers) 3 Float (Floating point numbers) 4 Tuples and Lists 5 Strings (really just lists) 6 Function types

7/40 Type inference In Java and most other languages the programmer has to declare what type variables, functions, etc have. We can do this too, in Haskell: > 6*7 :: Int 42 :: Int asserts that the expression 6*7 has the type Int. Haskell will check for us that we get our types right: > 6*7 :: Bool ERROR

8/40 Type inference... We can let the Haskell interpreter infer the type of expressions, called type inference. The command :type expression asks Haskell to print the type of an expression: > :type "hello" "hello" :: String > :type True && False True && False :: Bool > :type True && False :: Bool True && False :: Bool

Simple Types

10/40 Int The Int type is a 32-bit signed integer, similar to Java s int type: Prelude> (3333333 :: Int) * (44444444444444 :: Int) Program error: arithmetic overflow Some Haskell versions may instead overflow the integer (yielding a negative number).

11/40 Int Operators The normal set of arithmetic operators are available: Op Precedence Associativity Description ^ 8 right Exponentiation *, / 7 left Mul, Div div 7 free Division rem 7 free Remainder mod 7 free Modulus +, - 6 left Add, Subtract ==,/= 4 free (In-) Equality <,<=,>,>= 4 free Relational Comparison

12/40 Int... Note that the div operator has to be in backquotes when used as an infix operator: > 4*12-6 42 > 126 div 3 42 > div 126 3 42

13/40 Int... The standard precedence and associativity rules apply: 1+2-3 (1+2)-3 4==5==6 ERROR 1+2*3 1+(2*3) 12/6/3 0.666666666666667 2^3^4 2^(3^4) 12/(6/3) 6.0

14/40 Integer Haskell also has an infinte precision integer type, similar to Java s java.math.biginteger class: > (3333333 :: Integer) * (44444444444444 :: Integer) 148148133333331851852 Integers are the default integer type: > 2^64 18446744073709551616

15/40 Integer... Ints and Integers aren t compatible: > (3333333 :: Integer) * (44 :: Int) ERROR - Type error in application but we can convert from an Int to an Integer: > (tointeger (55 :: Int)) * (66 :: Integer) 3630

16/40 Float and Double Haskell also has built-in floating point numbers Float and Double: > sqrt 2 :: Float 1.414214 > sqrt 2 :: Double 1.4142135623731 sqrt is a built-in library function. Double is the default: > sqrt 2 1.4142135623731

17/40 Char Literals: a, b. Special characters: \n (newline). ASCII: \65 (decimal), \x41 (hex). There are standard functions on characters (toupper, isalpha, etc) defined in the a separate module Char: > :load Char > toupper A A > toupper a A > ord a 97

18/40 Char Built-in Functions ord :: Char -> Int chr :: Int -> Char toupper, tolower :: Char -> Char isascii,isdigit, :: Char -> Bool isupper,islower, :: Char -> Bool ord a 97 toupper a A chr 65 A isdigit a False

19/40 String Strings are really lists of characters. > "hello" "hello" > :type "hello" "hello" :: String > "hello" :: String "hello" > length "hello" 5 > "hello" ++ " world!" "hello world!" ++ does string/list concatenation.

20/40 Bool There are two boolean literals, True and False Op Precedence Associativity Description && 3 right logical and 2 right logical or not 9 logical not 3 < 5 && 4 > 2 (3 < 5) && (4 > 2) True False && True True (False && True)

Haskell Functions

22/40 Functions Here s the ubiquitous factorial function: fact :: Int -> Int fact n = 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

23/40 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+2) 120

List and Tuple Types

25/40 Lists A list in Haskell consists of a sequence of elements, all of the same type: > [1,2,3] [1,2,3] > [True,False] :: [Bool] [True,False] > :type [True,False] [True,False] :: [Bool] > :type [[ A, B ],[ C, D ],[]] [[ A, B ],[ C, D ],[]] :: [[Char]] > [1,True] ERROR > length [1,2,3] 3

26/40 Tuples A Haskell tuple is similar to a record/struct in C it is a collection of objects of (a limited number of) objects, possibly of different types. Each C struct 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 ). Examples: type Complex = (Float,Float) mkcomplex :: Float -> Float -> Complex mkcomplex re im = (re, im)

27/40 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)

Haskell Scripts

29/40 Editing and Loading Scripts :load name (or :l name) loads a new Haskell program. :reload (or :r) reloads the current script. :edit name (or :e name) edits a script. On Unix you can set the EDITOR environment variable to control which editor to use: setenv EDITOR emacs :? shows all available commands. :quit quits Hugs.

30/40 The Offside Rule When does one function definition end and the next one begin? square x = x * x +2 cube x = Textual layout determines when definitions begin and end.

31/40 The Offside Rule... The first character after the "=" opens up a box which holds the right hand side of the equation: square x = x * x +2 Any character to the left of the line closes the box and starts a new definition: square x = cube x =... x * x +2

32/40 Comments Line comments start with -- and go to the end of the line: -- This is a comment. Nested comments start with {- and end with -}: {- -} This is a comment. {- And here s another one... -}

Editing Scripts

34/40 Emacs On Unix, emacs is the editor of choice. Depending on your system, it may be called emacs or xemacs. For a list of common commands, see the links below.

35/40 Readings and References In addition to our textbook, chapters 1-3 of Programming in Haskell, by Graham Hutton, is a good introduction to Haskell: http://www.cs.nott.ac.uk/~gmh/book.html Emacs Guide: http://www.cs.arizona.edu/classes/cs372/fall03/04.html Emacs Reference Card: http://www.cs.arizona.edu/classes/cs372/fall03/emacs.html

36/40 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.

37/40 Exercise 1 Start Hugs. 2 Enter the commaint function and try it out. 3 Enter the addcomplex and mkcomplex functions and try them out. 4 Try the standard functions fst x and snd x on complex values. What do fst and snd do? 5 Try out the Eliza application in /usr/local/hugs98/lib/hugs/demos/eliza.hs on lectura.

38/40 Exercise... Write a Haskell function to check if a character is alphanumeric, i.e. a lower case letter, upper case letter, or digit.? isalphanum a True? isalphanum 1 True? isalphanum A True? isalphanum ; False? isalphanum @ False

39/40 Exercise... Define a Haskell exclusive-or function. eor :: Bool -> Bool -> Bool eor x y =? eor True True False? eor True False True? eor False True True? eor False False False

40/40 Exercise... 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