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

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 520. Principles of Programming Languages 11: Haskell Basics

Recursion and Induction

CS 320: Concepts of Programming Languages

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CS 320: Concepts of Programming Languages

Introduction to Programming, Aug-Dec 2008

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

Haskell through HUGS THE BASICS

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Lecture 3 Tao Wang 1

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

Haskell Scripts. Yan Huang

Lecture 19: Functions, Types and Data Structures in Haskell

Basic types and definitions. Chapter 3 of Thompson

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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

Topic 1: Introduction

Relational Database: The Relational Data Model; Operations on Database Relations

Standard prelude. Appendix A. A.1 Classes

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

Operators & Expressions

ECE 122 Engineering Problem Solving with Java

Introduction. chapter Functions

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

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

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

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

Logical Methods in... using Haskell Getting Started

Quick Reference Guide

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

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

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

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

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

Information Science 1

Declaration and Memory

Lecture 3. More About C

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

CSc 372 Comparative Programming Languages

JAVA OPERATORS GENERAL

Prof. Navrati Saxena TA: Rochak Sachan

C: How to Program. Week /Mar/05

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter Two MIPS Arithmetic

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Introduction to Programming: Lecture 10

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

LECTURE 3 C++ Basics Part 2

Information Science 1

A tour of the Haskell Prelude

Chapter 2 - Introduction to C Programming

COMP 110 Introduction to Programming. What did we discuss?

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Building Java Programs

Introduction to Programming, Aug-Dec 2006

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

Scheme: Expressions & Procedures

Building Java Programs

CS457/557 Functional Languages

SML A F unctional Functional Language Language Lecture 19

Lecture 1 August 9, 2017

The PCAT Programming Language Reference Manual

Lecture 2: List algorithms using recursion and list comprehensions

Data types for mcrl2

Lexical Considerations

Lecture 4: Higher Order Functions

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Programming Paradigms

9 Using Equation Networks

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

The SPL Programming Language Reference Manual

ITP 342 Mobile App Dev. Code

Functional Programming in Haskell Part I : Basics

Full file at

Learning the Language - V

Lexical Considerations

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

1 Lexical Considerations

Lecture 6: Arithmetic and Threshold Circuits

Chapter 17. Fundamental Concepts Expressed in JavaScript

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

UNIT- 3 Introduction to C++

Course Outline. Introduction to java

Transcription:

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin

Haskell Functional programming languages are particularly well-suited for recursive programming Haskell is a modern functional programming language In this module, we discuss recursive programming in the context of Haskell In the first few lectures we introduce some basic elements of the Haskell programming language

Primitive Data Types We make use of the following built-in Haskell data types: integer (called Int) boolean (called Bool) character (called Char) string (called String) In the next few slides we discuss each of these types in turn

Int Usual infix arithmetic operators: addition (+), subtraction (-), multiplication (*), exponentiation (^) Other standard binary operators such as maximum (max), minimum (min), modulus (mod), integer division (div), and integer remainder (rem) can be written in one of two ways: The function name can precede the arguments, e.g., the expression mod 17 4 evaluates to 1 The function name can be surrounded by back quotes and used in an infix manner, as in 17 mod 4 The arithmetic relations are < <= == /= > >= Note that == is used for equality and /= is used for inequality The result of an expression such as 3 < 5 is the boolean True

Bool The two boolean constants are written True and False The boolean operators are negation (not) and (&&) or ( ) equivalence (==) inequivalence, also known as exclusive or (/=)

Bool Here is a short session with hugs, the Haskell interpreter: Prelude> (3 > 5) (5 > 3) True Prelude> (3 > 3) (3 > 3) False Prelude> (2 mod (-3)) == ((-2) mod 3) False Prelude> even 3 odd 3 True

Char A character is enclosed within single quotes Two functions are used to convert between characters and integers Function ord(c) returns the value of the character c in the internal coding table; it is a number between 0 and 255 Function chr converts a number between 0 and 255 to the corresponding character chr(ord(c))=c, for all characters c

Char Here is a hugs session involving ord and chr: Prelude> ord( a ) 97 Prelude> chr(97) a Prelude> ord(chr(103)) 103 Prelude> chr(255) \255 Prelude> (ord 9 )- (ord 0 ) 9 Prelude> (ord a )- (ord A ) 32

Char You can compare two characters using arithmetic relations; the outcome is determined by their integer ord values Prelude> a < b True Prelude> A < a True It is illegal to compare a character directly with an integer Prelude> a < 3 ERROR - Illegal Haskell 98 class constraint in inferred type *** Expression : a < 3 *** Type : Num Char => Bool

String String constants are enclosed in double quotes Prelude> "a b c" "a b c" Prelude> "a, b, c" "a, b, c" A string is implemented as a list of characters Later on in the module we discuss lists in detail All the rules governing lists apply to strings

Writing Function Definitions Functions cannot be defined at the hugs prompt Instead, function definitions are typed into a separate file using a text editor The resulting file of function definitions is then loaded into the hugs interpreter

Loading Program Files Prelude> :l 337.hs Reading file "337.hs": Hugs session for: /lusr/share/hugs/lib/prelude.hs 337.hs

Comments Any string following -- is considered a comment Alternatively, we can begin and end a comment using {- and -} The latter format is particularly useful for multiline comments

Examples of Function Definitions Here are three simple function definitions inc x = x + 1 imply p q = not p q digit c = ( 0 <= c) && (c <= 9 ) A function need not have any arguments offset = (ord a ) - (ord A )

Using Functions Main> inc 5 6 Main> imply True False False Main> digit 6 True Main> digit a False Main> digit(chr(inc(ord 8 ))) True Main> digit(chr(inc(ord 9 ))) False

Conditional Equations It is often convenient to define a function by cases using a conditional equation Such a definition consists of a nonempty sequence of clauses of the form G = E where G is a boolean predicate called the guard and E is an expression For a given set of arguments, the function evaluates to the expression associated with the first clause for which the guard evaluates to True If no guard evaluates to True, a run-time error occurs Here is a simple example of a function that computes the absolute value of its argument absolute x x >= 0 = x x < 0 = -x

Conditional Equations: Otherwise The last clause of a conditional equation can have the special guard otherwise If such an otherwise clause is present, the associated expression determines the value of the function whenever the guards of all other clauses evaluate to False Here is an alternative definition of the absolute value function absolute x x >= 0 = x otherwise = -x