Basic types and definitions. Chapter 3 of Thompson

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

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

CS 320: Concepts of Programming Languages

Built-in Types of Data

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

A tour of the Haskell Prelude

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Introduction to C Language

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

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Basics of ST. Each must end with a semi-colon (";") Basic statement. Q:=IN; Q:=sin(angle); Q := (IN1 + (IN2 / IN 3)) * IN4;

1001ICT Introduction To Programming Lecture Notes

Arithmetic and Logic Blocks

What did we talk about last time? Examples switch statements

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

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

Using the um-fpu with the Javelin Stamp

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

How to Design Programs Languages

Lecture Notes on Haskell Programming

Scheme Quick Reference

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

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

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

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

Scheme Quick Reference

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

Fundamentals: Expressions and Assignment

Haskell through HUGS THE BASICS

Lecture 2 FORTRAN Basics. Lubna Ahmed

Script started on Thu 25 Aug :00:40 PM CDT

Excel Tool: Calculations with Data Sets

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

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

Introduction to GNU-Octave

Mentor Graphics Predefined Packages

Downloaded from Chapter 2. Functions

F28PL1 Programming Languages. Lecture 11: Standard ML 1

9. Elementary Algebraic and Transcendental Scalar Functions

JUN / 04 VERSION 7.0

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

CS-201 Introduction to Programming with Java

Using Game Maker 8: GML Scripting

Bits, Words, and Integers

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

Methods: A Deeper Look

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

MATHEMATICAL / NUMERICAL FUNCTIONS

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

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

C Functions. 5.2 Program Modules in C

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

Graphing Calculator Scientific Calculator Version 2.0

Introduction to Programming, Aug-Dec 2008

10 Using the PCFL Editor In this chapter

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

Topic 1: Introduction

Such JavaScript Very Wow

Functions and Recursion

Documentation for LISP in BASIC

Computing Fundamentals

Chapter 3 - Functions

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

Single row numeric functions

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS 115 Lecture 4. More Python; testing software. Neil Moore

Computer Programming in MATLAB

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Welcome. Please Sign-In

A General Introduction to Matlab

1 The number systems in Haskell 98

Data Types and Basic Calculation

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

A very brief Matlab introduction

Industrial Automation course

UNIT- 3 Introduction to C++

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Advanced features of the Calculate signal tool

CS 440: Programming Languages and Translators, Spring 2019 Mon

Variables. location where in memory is the information stored type what sort of information is stored in that memory

CT 229 Java Syntax Continued

Quick Reference Guide

Macro B Reference Guide

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

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

MYSQL NUMERIC FUNCTIONS

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

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

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

Chapter 3 Mathematical Functions, Strings, and Objects

Outline. Data and Operations. Data Types. Integral Types

L-System Fractal Generator: Language Reference Manual

Transcription:

Basic types and definitions Chapter 3 of Thompson

Booleans [named after logician George Boole] Boolean values True and False are the result of tests are two numbers equal is one smaller than the other The Haskell type is called Bool. The language predefines the following Boolean operators && logical and logical or (inclusive) not logical negation == equality /= inequality

Truth tables t 1 t 2 t 1 && t 2 t 1 t 2 not t 1 True True True True False False True False True True True False False True False False False False

Boolean function definition: exclusive or exor :: Bool -> Bool -> Bool exor x y = (x y) && not (x && y) t 1 t 2 exor t 1 t 2 True True False False True True True False True False False False

Literals and definitions Values that are given literally: True False Can be used in function definitions: mynot :: Bool -> Bool mynot True = False mynot False = True exorʹ True x = not x exorʹ False x = x These definitions use pattern matching on the argument values

Property testing with QuickCheck not x == mynot x exor x y == exor x y exor x y == (x /= y)

Integer and Int > maxbound :: Int 9223372036854775807 > minbound :: Int -9223372036854775808 > maxbound :: Integer <interactive>:19:1: No instance for (Bounded Integer) arising from a use of maxbound In the expression: maxbound :: Integer In an equation for it : it = maxbound :: Integer

Negative literals: caution > negate -34 <interactive>:22:1: No instance for (Show (a0 -> a0)) arising from a use of print In a stmt of an interactive GHCi command: print it > negate (-34) 34

Operators + sum * product ^ raise to power - difference div whole number division mod remainder abs remove sign negate change sign

Relational operators > greater than (and not equal to) >= greater than or equal to == equal to /= not equal to <= less than or equal to < less than (and not equal to)

Overloading Using the same symbol or name for different operations e.g., == applies to Int, Integer, and Bool > :print == == = (_t3::eq a1 => a1 -> a1 -> Bool)

Survey: Monty Hall problem A. I ve not heard of the Monty Hall Problem B. I ve heard of it, but don t know the solution C. I know the solution to the Monty Hall Problem

Let s make a deal!

Monty Hall problem To maximize your chances of winning the big prize, you should A. Stick with the door you originally chose B. Switch to the other unrevealed door C. Randomly choose whether to switch doors https://en.wikipedia.org/wiki/monty_hall_problem

Guards (conditions) A Boolean expression to allow alternatives in function definitions. For example: max :: Integer -> Integer -> Integer max x y x >= y = x otherwise = y

Guards (conditions) A Boolean expression to allow alternatives in function definitions. For example: max :: Integer -> Integer -> Integer max x y x >= y = x True = y

Guards (conditions) Evaluated in order until the first guard that evaluates to True. foo x1 x2 xk g1 = e1 g2 = e2... otherwise = e First g1, then g2, etc.

Conditional expressions if condition then m else n For example: maxʹ :: Integer -> Integer -> Integer maxʹ x y = if x >= y then x else y

Property testing with QuickCheck max x y == maxʹ x y x <= max x y && y <= max x y x == max x y y == max x y What about the following? x == max x y `exor` y == max x y

Char: characters Literal characters are written inside single quotes: 'a',, 'z', 'A',..., 'Z', etc. Escape characters: '\t' tab '\n' newline '\\' backslash (\) '\'' single quote (') '\"' double quote (")

Character representation > fromenum 'A' 65 > '\66' 'B' These are the ASCII encodings of 'A' and 'B'. > minbound :: Char '\NUL' > '\0' '\NUL' > maxbound :: Char '\1114111' [Try the command man ascii on Linux or your Mac]

Conversions offset :: Int offset = fromenum 'A' fromenum 'a' toupper :: Char -> Char toupper ch = toenum (fromenum ch + offset) isdigit :: Char -> Bool isdigit ch = ('0' <= ch) && (ch <= '9')

String > "This is a string!" "This is a string!" > putstr "" > putstr "\n" > putstr "\99a\116\n" cat > "cat" ++ "fish" "catfish"

Strings and values > show (2+3) "5" > show (True False) "True" > read "True" :: Bool True > read "3" :: Int 3

Float: Floating point numbers Fixed precision to match hardware floating point representations: Float is 32 bits Double is 64 bits Represent subsets of the real numbers. Example literals: 0.31426-23.12 231.61e7-3.142e03

Operators

Operator Type Meaning + - * Float -> Float -> Float Add, subtract, multiply Operations on reals / Float -> Float -> Float Fractional division ^ Float -> Integer -> Float Exponentiation x^n = x n for n N ** Float -> Float -> Float Exponentiation x**y = x y == /= < > <= >= Float -> Float -> Bool Relational operators abs Float -> Float Absolute value acos asin atan Float -> Float Inverse of cosine, sine, and tangent ceiling floor round Float -> Integer Convert fraction to integer cos sin tan Float -> Float Cosine, sine, and tangent exp Float -> Float Powers of e frominteger Integer -> Float Convert from an Integer fromintegral Int -> Float Convert from an Int (or any integral value) log Float -> Float Logarithm to base e logbase Float -> Float -> Float Logarithm to arbitrary base as first argument negate Float -> Float Change sign pi Float The constant pi signum Float -> Float 1.0, 0.0, or -1.0 according +ve, zero, or ve sqrt Float -> Float (Positive) square root

Beware of the following Non-numerical results > 1 / 0 Infinity No automatic conversion from Integer to Float > (floor 5.6) + 6.7... > fromintegral (floor 5.6) + 6.7 11.7

Syntax: definitions and layout Layout separates definitions: offside rule ends at the first piece of text that lies at the same (or lower) indentation level A sequence of definitions should appear at the same indentation level. Alternatively, use ; to end the current definition: answer = 42; facsix = 720 Names: start with lower case for variables, functions: foo, bar, baz start with upper case for types, constructors (e.g., True), modules, etc. camel case for multi-word names: foobarbaz