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

Similar documents
CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

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

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

CSE 341 Section Handout #6 Cheat Sheet

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

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

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

CSc 520. Principles of Programming Languages 25: Types Introduction

CSc 372 Comparative Programming Languages

CSc 520 Principles of Programming Languages

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

Scheme: Expressions & Procedures

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

Scheme Quick Reference

CSc 553. Principles of Compilation. 16 : OO Languages Polymorphism. Department of Computer Science University of Arizona

Module 3: New types of data

Organization of Programming Languages CS3200/5200N. Lecture 11

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

CSC 533: Programming Languages. Spring 2015

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

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Scheme Quick Reference

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

A Brief Introduction to Scheme (I)

CSc 520. Principles of Programming Languages 7: Scheme List Processing

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Java Bytecode (binary file)

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

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Data Dependence Analysis

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Common LISP Tutorial 1 (Basic)

Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Lec 3. Compilers, Debugging, Hello World, and Variables

Programming in C++ PART 2

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 231 Data Structures and Algorithms, Fall 2016

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

CS115 - Module 3 - Booleans, Conditionals, and Symbols

Programming refresher and intro to C programming

IB Sets and Venn Diagram Questions- Package #1

Functional Programming

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

Department of Computer Science COMP The Programming Competency Test

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CSc 520 Principles of Programming Languages

Functional Programming Languages (FPL)

CSc 520. Principles of Programming Languages 11: Haskell Basics

1 Lexical Considerations

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

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

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

CS302: Self Check Quiz 2

Chapter 15 Functional Programming Languages

CSc 520 Principles of Programming Languages

ECE570 Lecture 2: Types and Conventions

ECE473 Lecture 2: Types and Conventions

The Arithmetic Operators

An Introduction to Scheme

The Interpreter + Calling Functions. Scheme was designed by people. The Interpreter + Calling Functions. Clickers. Parentheses Matter We asked scheme

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Arrays. Eng. Mohammed Abdualal

The current topic: Python. Announcements. Python. Python

How to Design Programs Languages

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CONTROL AND ENVIRONMENTS 1

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

Dept. of CSE, IIT KGP

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

COP4020 Programming Assignment 1 - Spring 2011

CSc 372 Comparative Programming Languages

Simple Java Reference

User-defined Functions. Conditional Expressions in Scheme

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Inheritance. CSc 372. Comparative Programming Languages. 28 : Ruby Classes. Department of Computer Science University of Arizona.

CSc 372 Comparative Programming Languages

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Expression Values Operators. (string-append a b c ) a, b, c string-append. (substring abcd 0 2) abcd, 0, 2 substring

Introduction to C An overview of the programming language C, syntax, data types and input/output

C ONTROL AND H IGHER O RDER F UNCTIONS

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Transcription:

1/26 CSc 372 Comparative Programming Languages 36 : Scheme Conditional Expressions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/26 Comparison Functions Boolean functions (by convention) end with a?. We can discriminate between different kinds of numbers: > (complex? 3+4i) #t > (complex? 3) #t > (real? 3) #t > (real? -2.5+0.0i) #t > (rational? 6/10)

3/26 Comparison Functions... #t > (rational? 6/3) #t > (integer? 3+0i) #t > (integer? 3.0) #t > (integer? 8/4) #t

4/26 Tests on Numbers Several of the comparison functions can take multiple arguments. (< 4 5 6 7 9 234) returns true since the numbers are monotonically increasing. > (< 4 5) true > (< 4 5 6 7 9 234) true > (> 5 2 1 3) false > (= 1 1 1 1 1) true > (<= 1 2 2 2 3) true

5/26 Tests on Numbers... > (>= 5 5) true > (zero? 5) false > (positive? 5) true > (negative? 5) false > (odd? 5) true > (even? 5) false

6/26 Conditionals If If the test-expression evaluates to #f (False) return the valuen of the then-expression, otherwise return the value of the else-expression: (if test-expression then-expression else-expression ) Up to language level Advanced Student if-expressions must have two parts. Set the language level to Standard (R5RS) to get the standard Scheme behavior, where the else-expression is optional.

7/26 Conditionals If... > (define x 5) > (if (= x 5) 2 4) 2 > (if (< x 3) (display "hello") (display "bye")) bye > (display (if (< x 3) "hello" "bye")) bye

8/26 If it s not False (#f), it s True (#t) Any value that is not false, is interpreted as true. NOTE: In DrScheme this depends on which language level you set. Up to Advanced Student, the test-expression of an if must be either #t or #f. Set the language level to Standard (R5RS) to get the standard Scheme behavior: > (if 5 "hello" "bye") "hello" > (if #f "hello" "bye") "bye" > (if #f "hello") > (if #t "hello") "hello"

9/26 Boolean Operators and and or can take multiple arguments. and returns true if none of its arguments evaluate to False. or returns true if any of its arguments evaluates to True. > (and (< 3 5) (odd? 5) (inexact? (cos 32))) #t > (or (even? 5) (zero? (- 5 5))) #t > (not 5) #f > (not #t) #f

10/26 Boolean Operators... In general, any value that is not #f is considered true. and and or evaluate their arguments from left to right, and stop as soon as they know the final result. The last value evaluated is the one returned. > (and "hello") "hello" > (and "hello" "world") "world" > (or "hello" "world") "hello"

11/26 Defining Boolean Functions We can define our own boolean functions: (define (big-number? n) (> n 10000000) ) > (big-number? 5) #f > (big-number? 384783274832748327) #t >

12/26 Conditionals cond cond is a generalization of if: (cond (cond-expression 1 result-expression 1 ) (cond-expression 2 result-expression 2 )... (else else-expression)) Each cond-expression i is evaluated in turn, until one evaluates to not False. > (cond 4 ((< 2 3) 4) ((= 2 3) 5) (else 6))

13/26 Conditionals cond... To make this a bit more readable, we use square brackets around the cond-clauses: (cond [cond-expr 1 result-expr 1 ] [cond-expr 2 result-expr 2 ]... [else else-expression]) > (cond [#f 5] [#t 6]) 6 > (cond [(= 4 5) "hello"] [(> 4 5) "goodbye"] [(< 4 5) "see ya!"]) "see ya!"

14/26 Conditionals case case is like Java/C s switch statment: (case key [(expr 1 expr 2...) result-expr 1 ] [(expr 11 expr 11...) result-expr 2 ]... (else else-expr)) The key is evaluated once, and compared against each cond-expr in turn, and the corresponding result-expr is returned. > (case 5 [(2 3) "hello"] [(4 5) "bye"]) "bye"

15/26 Conditionals case... (define (classify n) (case n [(2 4 8 16 32) "small power of 2"] [(2 3 5 7 11) "small prime number"] [else "some other number"] ) ) > (classify 4) "small power of 2" > (classify 3) "small prime number" > (classify 2) "small power of 2" > (classify 32476) "some other number"

16/26 Sequencing To do more than one thing in sequence, use begin: (begin arg 1 arg 2...) > (begin (display "the meaning of life=") (display (* 6 7)) (newline) ) the meaning of life=42

17/26 Examples!n Write the factorial function!n: (define (! n) (cond [(zero? n) 1] [else (* n (! (- n 1)))] ) ) > (! 5) 120

Examples ( ) n r 18/26 Write the ( n r) function in Scheme: ( ) n n! = r r! (n r)! Use the factorial function from the last slide. (define (choose n r) (/ (! n) (* (! r) (! (- n r)))) ) > (choose 5 2) 10

19/26 Examples (sum m n) Write a function (sum m n) that returns the sum of the integers between m and n, inclusive. (define (sum m n) (cond [(= m n) m] [else (+ m (sum (+ 1 m) n))] ) ) > (sum 1 2) 3 > (sum 1 4) 10

20/26 Examples Ackermann s function Implement Ackermann s function: A(1,j) = 2j for j 1 A(i,1) = A(i 1,2) for i 2 A(i,j) = A(i 1,A(i,j 1)) for i,j 2 (define (A i j) (cond [(and (= i 1) (>= j 1)) (* 2 j)] [(and (>= i 2) (= j 1)) (A (- i 1) 2)] [(and (>= i 2) (>= j 2)) (A (- i 1) (A i (- j 1)))] ) )

21/26 Examples Ackermann s function... Ackermann s function grows very quickly: > (A 1 1) 2 > (A 3 2) 512 > (A 3 3) 1561585988519419914804999641169225 4958731641184786755447122887443528 0601470939536037485963338068553800 6371637297210170750776562389313989 2867298012168192

22/26 Scheme so Far Unlike languages like Java and C which are statically typed (we describe in the program text what type each variable is) Scheme is dynamically typed. We can test at runtime what particular type of number an atom is: (complex? arg), (real? arg) (rational? arg), (integer? arg) Tests on numbers: (< arg1, arg2), (> arg1, arg2) (= arg1, arg2), (<= arg1, arg2) (>= arg1, arg2), (zero? arg) (positive? arg), (negative? arg) (odd? arg), (even? arg)

23/26 Scheme so Far... Unlike many other languages like Java which are statement-oriented, Scheme is expression-oriented. That is, every construct (even if, cond, etc) return a value. The if-expression returns the value of the then-expr or the else-expr: (if test-expr then-expr else-expr) depending on the value of the test-expr.

24/26 Scheme so Far... The cond-expression evaluates its guards until one evaluates to non-false. The corresponding value is returned: (cond (guard 1 value 1 ) (guard 2 value 2 )... (else else-expr))

25/26 Scheme so Far... The case-expression evaluates key, finds the first matching expression, and returns the corresponding result: (case key [(expr 1 expr 2...) result-expr 1 ] [(expr 11 expr 11...) result-expr 2 ]... (else else-expr))

26/26 Scheme so Far... and and or take multiple arguments, evaluate their results left-to-right until the outcome can be determined (for or when the first non-false, for and when the first false is found), and returns the last value evaluated.