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

Similar documents
Basic types and definitions. Chapter 3 of Thompson

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Standard prelude. Appendix A. A.1 Classes

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

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

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

Introduction to Programming, Aug-Dec 2006

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

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

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

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

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

Variables, Constants, and Data Types

F28PL1 Programming Languages. Lecture 11: Standard ML 1

A tour of the Haskell Prelude

Topic 1: Introduction

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Lecture 2: List algorithms using recursion and list comprehensions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 2 Tao Wang 1

A general introduction to Functional Programming using Haskell

Haskell through HUGS THE BASICS

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

MODULE 02: BASIC COMPUTATION IN JAVA

Functional Programming and Haskell

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

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

CS 320: Concepts of Programming Languages

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

CS 360: Programming Languages Lecture 10: Introduction to Haskell

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

IT 374 C# and Applications/ IT695 C# Data Structures

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Type Processing by Constraint Reasoning

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

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

Fundamentals: Expressions and Assignment

CS 320: Concepts of Programming Languages

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

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

2 nd Week Lecture Notes

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

The C++ Language. Arizona State University 1

An introduction introduction to functional functional programming programming using usin Haskell

Intro to Haskell Notes: Part 5

Computer Programming : C++

Variables and Functions. ROBOTC Software

CSC324 Principles of Programming Languages

CS 11 Haskell track: lecture 1

Introduction to Programming, Aug-Dec 2008

Haskell Scripts. Yan Huang

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

Datatypes, Variables, and Operations

CSc Introduction to Computing

Haskell Modules. Input and Output. Urtė Zubkaitė

Fundamentals of Programming. Lecture 11: C Characters and Strings

Basic data types. Building blocks of computation

Haskell An Introduction

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

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 457/557: Functional Languages

C: How to Program. Week /May/28

Outline. Data and Operations. Data Types. Integral Types

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

Case by Case. Chapter 3

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Reviewing all Topics this term

4 Programming Fundamentals. Introduction to Programming 1 1

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

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

CSI33 Data Structures

Key Differences Between Python and Java

Full file at

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Introduction to String Manipulation

Data types for mcrl2

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

Computer System and programming in C

Advances in Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages

printf( Please enter another number: ); scanf( %d, &num2);

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Type Checking and Type Equality

Chapter 8 - Characters and Strings

Transcription:

Haskell Programs We re covering material from Chapters 1-2 (and maybe 3) of the textbook. Haskell Fundamentals Prof. Susan Older A Haskell program is a series of comments and definitions. Each comment begins with -- (or appears between { and }) and serves as documentation. Each definition contains a type declaration and one or more equations: 17 January 2019 name :: t 1 -> t 2 -> -> t n -> t name x 1 x 2 x n = exp Each t i is a type, each x i is a formal parameter. The type declaration serves as a contract: What the function expects as input (x i has type t i ) What the function will deliver (exp has type t) (CIS 252) Haskell Fundamentals 17 January 2019 1 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 2 / 20 What are Types? Type = Collection of similar data values Types are very important in Haskell: Every expression has a type. Types govern what/how we can combine. Nothing gets evaluated unless the types make sense. Consider the following function definition: ispositive :: Int -> Bool ispositive num = (num > 0) ispositive expects an Int as input parameter. ispositive returns a Bool as a result. Some Very Basic Types Haskell has lots of built-in types, including: Bool Boolean values: True and False Int Integer values from -9223372036854775808 to 9223372036854775807 (i.e., 64-bit integers) [Technically: The number of bits depends on the implementation, but is guaranteed to be at least 30 bits. For GHCi, it s 64 bits] Integer ( Int!) All integer values: full-precision integers Float Floating-point numbers, such as 3.267 or -81.09 or 12345.0 We ll discuss these (and other types) in more detail later. (CIS 252) Haskell Fundamentals 17 January 2019 3 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 4 / 20

Types: Simple Examples thrice :: Int -> Int thrice n = 3*n ispositive :: Int -> Bool ispositive num = (num > 0) mystery :: Int -> Int -> Int mystery x y = (thrice x) + y What are the types of these expressions? 1 thrice 12 :: Int 2 thrice False...Type error 3 ispositive (thrice 12) :: Bool 4 mystery (thrice 12) 5 :: Int Terminology: Formal Parameters and Actual Parameters Consider the following definition: simple :: Int -> Int -> Int simple a b = a + 3*b These lines define simple to be a function that accepts two Int values and returns an Int value. The names a and b are the formal parameters of simple. They are names that appear in the function definition to represent the data that may be passed into the function. Suppose we evaluate simple (4+2) 5: (4+2) and 5 are the actual parameters (a.k.a. arguments) of simple. They are the expressions that appear in a function call/invocation. (CIS 252) Haskell Fundamentals 17 January 2019 5 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 6 / 20 Evaluating Expressions in Haskell Idea: Based on rewriting equations (just like in algebra!) Happens after types are checked: type errors mean no evaluation Lazy evaluation: expressions evaluated only when values needed Recall thrice n = 3*n from earlier: thrice (5+2) 3 * (5+2) 3 * 7 21 thrice (4 - thrice 5) 3 * (4 - thrice 5) 3 * (4-3*5) 3 * (4-15) 3 * (-11) -33 What Are the Rules for Identifiers (i.e., Names)? Identifiers begin with a letter, and can then be followed by any combination of letters, digits, underscores ( ), and single quotes ( ): Two important rules x Number a123 xy alpha 1 Names of functions and variables must begin with a lowercase letter. 2 Names of types must begin with an uppercase letter. Later on, we ll see: constructors, module names, and type classes also must begin with an uppercase letter. Convention: When names are built from multiple words, the second and subsequent words are capitalized. celsiustofahr, istoohot (CIS 252) Haskell Fundamentals 17 January 2019 7 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 8 / 20

Another Gotcha: Layout (Indentation Matters!) More about Bool Layout determines where definitions start and stop. The Rule: A definition ends at the first piece of text that lies at the same indentation as (or to the left of) the start at that definition. Guidelines: For top-level definitions, start at the leftmost column. When writing definitions, use the same indentation for each. (Emacs can help you with this task.) Exactly two values: True, False Standard operators: not :: Bool -> Bool (==) :: Bool -> Bool -> Bool (&&) :: Bool -> Bool -> Bool (/=) :: Bool -> Bool -> Bool ( ) :: Bool -> Bool -> Bool not e: evaluates to True when e evaluates to False (and vice versa) == and /= are equality and inequality (respectively) e 1 && e 2 : evaluates to True when both e 1 and e 2 evaluate to True (evaluates to False when either e 1 or e 2 evaluates to False) e 1 e 2 : evaluates to True when either e 1 or e 2 evaluates to True (evaluates to False when both e 1 and e 2 evaluate to False) (CIS 252) Haskell Fundamentals 17 January 2019 9 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 10 / 20 Integers: Int and Integer Floating-point Numbers: Float Int: integers that can be represented within 64 bits Integer: all integers (i.e., full-precision integers) Standard operators and functions (also work for Integer) (+), (*), (-), div, mod, (^) :: Int -> Int -> Int even, odd :: Int -> Bool (==), (/=) :: Int -> Int -> Bool (<), (<=), (>), (>=) :: Int -> Int -> Bool You cannot add (or multiply or...) an Int with an Integer. However, you can convert between Int and Integer: tointeger:: Int -> Integer frominteger:: Integer -> Int Float: single-precision floating-point numbers Examples include: 543.874-346.2 12.0 Some standard operators and functions (+), (*), (-), /, (**) :: Float -> Float -> Float (==), (/=) :: Float -> Float -> Bool (<), (<=), (>), (>=) :: Float -> Float -> Bool ceiling, floor, round :: Float -> Integer More functions listed in Figure 3.2 of the textbook (page 58). (CIS 252) Haskell Fundamentals 17 January 2019 11 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 12 / 20

Quick Side Discussion #1: Overloading Let s Write Some Functions! We ve seen that == and + have the following types (among others): (==):: Bool -> Bool -> Bool (==):: Int -> Int -> Bool (==):: Integer -> Integer -> Bool (==):: Float -> Float -> Bool (+):: Int -> Int -> Int (+):: Integer -> Integer -> Integer (+):: Float -> Float -> Float These are instances of overloading: The same name (or symbol) is used to represent different operations/functions on different types. As time permits, let s write these functions: average :: Float -> Float -> Float Accepts two numbers and calculates their average allpositive :: Int -> Int -> Int -> Bool Accepts three integers and determines whether they re all positive somenegative :: Int -> Int -> Int -> Bool Accepts three integers and determines whether at least one is negative Haskell determines from context which definition is needed. Overloading is handled through type classes (more on that later). (CIS 252) Haskell Fundamentals 17 January 2019 13 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 14 / 20 More Types: Char More Types: String Sample values: a, A, 3,, \n (newline), \t (tab) The module Data.Char contains lots of useful functions, including: isdigit :: Char -> Bool isalpha :: Char -> Bool isalphanum :: Char -> Bool isupper :: Char -> Bool islower :: Char -> Bool iscontrol :: Char -> Bool toupper :: Char -> Char tolower :: Char -> Char ord :: Char -> Int chr :: Int -> Char To use these functions, include the following at the top of your Haskell file: import Data.Char Strings are sequences of characters, enclosed with double quotes: "hello!" "1234" "" (empty string) "abc\ndefg\nh\ti" String concatenation: (++) :: String -> String -> String Example: "abc" ++ "1234" evaluates to "abc1234" Later we ll see: String = [Char] (i.e., strings are lists of characters) (CIS 252) Haskell Fundamentals 17 January 2019 15 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 16 / 20

Dealing with Cases: What to Do? According to SU s Bursar, tuition for new main-campus undergraduate students in 2018-9 is: Per semester (12-19 credits) $25115 Per credit (first 11) $2188 Per credit (20 or more) $1509 Conditional Equations Let s look at one solution: tuition :: Int -> Int tuition cr cr <= 0 = 0 cr <= 11 = cr * 2188 cr >= 20 = cr * 1509 otherwise = 25115 1 There are four guards (all of which must have type Bool) : Let s write a Haskell function that: Accepts as input the number of credits being taken Calculates the tuition cost of that number of credits cr <= 0 cr <= 11 cr >= 20 otherwise 2 There are four possible results (all of which must have type Int): 0 cr * 2188 cr * 1509 25115 3 Evaluation rule: Return the result associated with the first guard that evaluates to True (Note: otherwise always evaluates to True.) (CIS 252) Haskell Fundamentals 17 January 2019 17 / 20 (CIS 252) Haskell Fundamentals 17 January 2019 18 / 20 Conditional Equations: A Quick Quiz Consider the following: contrived :: Integer -> Integer -> Integer contrived m n even m && m > n = n*2 -- && is and odd n n<3 = m -- is or otherwise = m+n+1 What are the values of the following expressions? contrived 100 7 14 contrived 16 100 117 contrived 321 7 321 contrived 44 0 0 contrived 95 0 95 contrived 28 15.0 Type Error! (CIS 252) Haskell Fundamentals 17 January 2019 19 / 20 Quick Side Discussion #2: Functions versus Operators When calling a function: The function name appears before its arguments: div 17 4 tuition 16 contrived 100 7 When using operators (which always have two arguments): Operators appear between their two arguments: 6 * (3+4) "abc" ++ "123xy" If you want to treat operators like functions, put them in parentheses: (*) 6 (3+4) (*) 6 ((+) 3 4) (++) "abc" "123xy" (CIS 252) Haskell Fundamentals 17 January 2019 20 / 20