ECE570 Lecture 2: Types and Conventions

Similar documents
ECE473 Lecture 2: Types and Conventions

Scheme Quick Reference

Scheme Quick Reference

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Typed Racket: Racket with Static Types

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

The Typed Racket Guide

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

An introduction to Scheme

Programming Languages

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 1. Fundamentals of Higher Order Programming

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

CSE 341 Section Handout #6 Cheat Sheet

Introduction to Scheme

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Functional Programming Languages (FPL)

CSC 533: Programming Languages. Spring 2015

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming. Pure Functional Languages

How to Design Programs Languages

6.001 Notes: Section 8.1

Chapter 15 Functional Programming Languages

The SPL Programming Language Reference Manual

Types, Expressions, and States

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Essentials of Programming Languages Language

Essentials of Programming Languages Language

ITEC2620 Introduction to Data Structures

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CS 115 Lecture. Boolean logic Taken from notes by Dr. Neil Moore

SCHEME AND CALCULATOR 5b

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Extensible Pattern Matching

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Scheme: Strings Scheme: I/O

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

User-defined Functions. Conditional Expressions in Scheme

A Brief Introduction to Scheme (II)

CS 314 Principles of Programming Languages

Lecture #24: Programming Languages and Programs

Problem One: A Quick Algebra Review

Administrivia. Simple data types

Advanced R. V!aylor & Francis Group. Hadley Wickham. ~ CRC Press

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Scheme: Expressions & Procedures

CSc 520 Principles of Programming Languages

The Warhol Language Reference Manual

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme as implemented by Racket

Lecture Notes on Lisp A Brief Introduction

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

An Introduction to Scheme

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Documentation for LISP in BASIC

The PCAT Programming Language Reference Manual

Module 8: Local and functional abstraction


Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Reasoning About Programs Panagiotis Manolios

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Homework 1. Due Date: Wednesday 11/26/07 - at the beginning of the lecture

6.001 Notes: Section 15.1

A quick introduction to SML

Repetition Through Recursion

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

ITEC2620 Introduction to Data Structures

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Class Structure. Prerequisites

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

FP Foundations, Scheme

CSc 520 Principles of Programming Languages

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

7.1 Optional Parameters

CS 11 Haskell track: lecture 1

JME Language Reference Manual

Functional programming with Common Lisp

A Brief Introduction to Scheme (I)

Transcription:

ECE570 Lecture 2: Types and Conventions Jeffrey Mark Siskind School of Electrical and Computer Engineering Fall 2017 Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 1 / 28

Booleans SCHEME has procedure to operate on Boolean values: (and #t #f) = #f (or #t #f) = #t (not #t) = #f Note: and and or are not really procedures. More on that later. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 2 / 28

Truth #f is the only SCHEME value treated as false. (In some implementations the empty list () is also treated as false.) All other SCHEME values are treated as true. Builtin predicates like =, <, >, <=, >=, etc. always yield #t or #f, never another true or false value. (if #t 1 2) = 1 (if #f 1 2) = 2 (if 27 1 2) = 1 Gotcha: In some implementations: (if () 1 2) = 1 In other implementations: (if () 1 2) = 2 Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 3 / 28

AND and OR are Syntax (and (> x 0) (< y 5)) is syntax. (or (> x 0) (< y 5)) is syntax. (not (> x 0)) is a procedure call. and and or do short-circuit evaluation. (and (> x 0) (< (sqrt x) 42)) left-to-right and stops at first false subexpression. or stops at first true subexpression. and and or take zero or more arguments. (and) =? (or) =? Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 4 / 28

AND Can Return Something Other Than #T or #F and returns the value of the first false subexpression if there is one. (and (< 3 2) 5) = #f and returns the value of the last subexpression if all subexpressions are true. (and (> 3 2) 5) = 5 (and 5 (> 3 2)) = #t Gotcha: and is implementation dependent when given the empty list () as an argument. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 5 / 28

OR Can Return Something Other Than #T or #F or returns the value of the first true subexpression if there is one. (or 5 (> 3 2)) = 5 (or (> 3 2) 5) = #t or returns the value of the last subexpression if all subexpressions are false. (or (< 7 6) (< 3 2)) = #f Gotcha: or is implementation dependent when given the empty list () as an argument. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 6 / 28

The Numeric Tower I SCHEME has arbitrary precision integers (bignums), (factorial 30) = 265252859812191058636308480000000 rationals, (/ 1 2) = 1/2 reals, and (sqrt 2) = 1.4142135623730951 complex numbers. (+ 1 (sqrt -4)) = 1+2i The numerator and denominator of a rational can be bignums. The real and imaginary components of a complex number can be rational or real. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 7 / 28

The Numeric Tower II Numeric type is a property of the number, not its representation. (integer? 2/1) = #t (integer? 1.0) = #t (integer? 1+0i) = #t (rational? 1.0) = #t (rational? (sqrt 2.0)) = #t (rational? (sqrt 2)) = #t (rational? 1+0i) = #t Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 8 / 28

The Numeric Tower III All integers are rational, all rationals are real, all reals are complex, and all complex numbers are numbers. (rational? 1) = #t (real? 1) = #t (real? 1/2) = #t (complex? 1) = #t (complex? 1/2) = #t (complex? (sqrt 2)) = #t Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 9 / 28

The Numeric Tower IV SCHEME supports exponential notation: (expt 10.0-20) = 1e-20 Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 10 / 28

The Numeric Tower V SCHEME numbers are exact or inexact. (exact? 1) = #t (inexact? 1.0) = #t (inexact? 1e5) = #t (exact? 1/3) = #t (exact? 1+1/2i) = #t (inexact? 1.0+1/2i) = #t Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 11 / 28

The Numeric Tower VI Exactness is orthogonal to numeric type. Implementations are not required to support all numeric types and all combinations of numeric type and exactness. Implementations can silently coerce exact numbers to inexact numbers but must preserve inexactness. Inexactness is lost when explicitly leaving the number domain or by using the inexact->exact type coercion. The precise rules are complicated. See the manual for details. We will only use exact integers and inexact rationals in this course. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 12 / 28

Types 1 is a number. (Actually, it is a numeric constant that evaluates to a number.) + is a procedure that takes numeric arguments and results a numeric result. (Actually, it is a variable that is bound to such a procedure.) < is a procedure that returns a Boolean result. #t and #f are Boolean constants. (Non-Boolean values are often treated as true. There are exceptions, however. The precise rules are complicated. See the manual for details.) Number and Boolean are types. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 13 / 28

Conventions I Convention: Procedures that return Boolean values have names that end in?. Examples: number?, exact?, integer?, boolean? exceptions: =, <, <=, >, >= Conventions are not enforced but it is a good idea to follow them. It helps other people understand your code. As a newcomer, it is a good idea to to follow the established conventions of a community because it helps you become a member of that community. It also allows you to leverage the existing wisdom and experience of that community. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 14 / 28

Conventions II Holds for global variables, Convention: Variables that are bound to Boolean values have names that end in?. Examples: (define *lifted?* #f) local variables, Examples: (let ((entry? #f))...) procedure parameters, and Examples: (define (tracking-pointer twice? press? tracker)...) structure slots. Examples: (define-structure pbm raw? bitmap) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 15 / 28

Conventions III Because: Convention: Variables that are bound to procedures that return Boolean values have names that end in?. (define (bound? x) (null? (rest (domain-variable-domain x)))) is really shorthand for: (define bound? (lambda (x) (null? (rest (domain-variable-domain x))))) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 16 / 28

Conventions IV Definition: A type predicate is a one argument procedure that returns a Boolean value indicating whether its argument is a member of a specified type. Convention: A type predicate for type is named type?. Examples: number?, boolean? Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 17 / 28

Conventions V Definition: A type coercion is a one-argument procedure that maps arguments of type 1 to results of type 2. Convention: A type coercion that maps type 1 to type 2 is named type 1 ->type 2. Examples: exact->inexact, integer->char, list->string Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 18 / 28

Case Insensitivity (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) (DEFINE (FACTORIAL N) (IF (ZERO? N) 1 (* N (FACTORIAL (- N 1))))) (Define (Factorial N) (If (Zero? N) 1 (* N (Factorial (- N 1))))) zero?, ZERO?, Zero?, and even zero? are all equivalent and refer to the same procedure. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 19 / 28

Conventions VI Convention: Variable (and procedure) names are written in lower case, not upper case or capitalized. Examples: not: number? NUMBER?, Number? Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 20 / 28

Conventions VII Convention: Use - to separate multiple words in variable (and procedure) names, not _, capitalization, or no separation. Examples: not: invert-matrix invert_matrix, invertmatrix, invertmatrix Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 21 / 28

Conventions VIII Convention: Use full English words in variable (and procedure) names. Examples: not: invert-matrix inv-mat, invmat Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 22 / 28

Conventions IX Convention: Limit line length to 79 characters so there is no wraparound. Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 23 / 28

Conventions X Example: Convention: Use a tool to properly indent your code automatically. (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) not: (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 24 / 28

Conventions XI Example: Convention: Do not put whitespace after ( or before ). (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) not: ( define (factorial n) ( if (zero? n) 1 (* n ( factorial (- n 1) )) ) ) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 25 / 28

Conventions XII Convention: Put a single space between elements of a list on the same line. Examples: (a b c), (() ()) not: (a b c), (()()), (() ()) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 26 / 28

Conventions XIII Convention: The alternate of an if expression is optional. But it is bad practice to leave it out. Use when or unless instead. bad: (if (bound? domain-variable) (place-queen i (binding domain-variable))) instead: (when (bound? domain-variable) (place-queen i (binding domain-variable))) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 27 / 28

Conventions XIV Convention: The else clause of a cond expression is optional. But it is bad practice to leave it out. Put in a dummy else clause. bad: (define (signum n) (cond ((negative? n) -1) ((zero? n) 0) ((positive? n) 1))) instead: (define (signum n) (cond ((negative? n) -1) ((zero? n) 0) ((positive? n) 1) (else (panic "This shouldn t happen")))) Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 28 / 28