How to Design Programs

Similar documents
CS115 - Module 10 - General Trees

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Module 8: Local and functional abstraction

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

ormap, andmap, and filter

Intro. Scheme Basics. scm> 5 5. scm>

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Lab 7: OCaml 12:00 PM, Oct 22, 2017

A brief tour of history

(add1 3) 4 (check-expect (add1 3) 4)

CS115 - Module 9 - filter, map, and friends

Typed Racket: Racket with Static Types

The Design Recipe Fall 2017

Functional Programming. Pure Functional Languages

Module 10: General trees

The Typed Racket Guide

CSU211 Exam 2 Fall 2007

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Graphs. Directed graphs. Readings: Section 28

Rewriting your function using map and foldr

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

(add1 3) 4 (check-expect (add1 3) 4)

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

The Design Recipe Fall 2018

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Functional Programming. Pure Functional Programming

Typed Scheme: Scheme with Static Types

Racket: Modules, Contracts, Languages

Trees. Readings: HtDP, sections 14, 15, 16.

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

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

Functional abstraction

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Case Study: Undefined Variables

Adding Machine Run 2

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Functional Programming. Pure Functional Languages

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

The Typed Racket Reference

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

User-defined Functions. Conditional Expressions in Scheme

CS115 - Module 8 - Binary trees

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Module 9: Binary trees

CS 1101 Exam 3 A-Term 2013

CS 314 Principles of Programming Languages

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

COP4020 Programming Assignment 1 - Spring 2011

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CS 314 Principles of Programming Languages. Lecture 16

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.

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

The type checker will complain that the two branches have different types, one is string and the other is int

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

YOUR NAME PLEASE: *** SOLUTIONS ***

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

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

Module 10: Imperative Programming, Modularization, and The Future

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

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

Loops in Scheme, II. (early slides assume map/filter)

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming

Freeing memory is a pain. Need to decide on a protocol (who frees what?) Pollutes interfaces Errors hard to track down

CS 314 Principles of Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

CS2500 Exam 2 Fall 2011

Discussion 4. Data Abstraction and Sequences

A Brief Introduction to Scheme (II)

Laboratory: Recursion Basics

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

Module 8: Binary trees

CS61A Notes Disc 11: Streams Streaming Along

Racket Style Guide Fall 2017

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

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

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

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

Lisp. Versions of LISP

Introduction to Functional Programming

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Shell CSCE 314 TAMU. Haskell Functions

CSE413 Midterm. Question Max Points Total 100

CS 360 Programming Languages Day 14 Closure Idioms

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Scheme as implemented by Racket

CS3 Midterm 2 Summer 2008

CS450 - Structure of Higher Level Languages

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Transcription:

How to Design Programs How to (in Racket): represent data variants trees and lists write functions that process the data See also http://www.htdp.org/ 1

Running Example: GUIs Pick a fruit: Apple Banana Coconut Ok Cancel Possible programs: Can click? Find a label Read screen 2

Representing GUIs Pick a fruit: Apple Banana Coconut Ok Cancel labels a label string buttons a label string enabled state lists a list of choice strings selected item (define-type GUI [label (text string?)] [button (text string?) (enabled? boolean?)] [choice (items (listof string?)) (selected integer?)]) 7

Read Screen ; read-screen : GUI -> list-of-string (define (read-screen g) (type-case GUI g [label (t) (list t)] [button (t e?) (list t)] [choice (i s) i])) (test (read-screen (label "Hi")) '("Hi")) (test (read-screen (button "Ok" true)) '("Ok")) (test (read-screen (choice '("Apple" "Banana") 0)) '("Apple" "Banana")) 8

Assemblings GUIs Pick a fruit: Apple Banana Coconut Ok Cancel label buttons lists vertical stacking two sub-guis horizontal stacking two sub-guis (define-type GUI [label (text string?)] [button (text string?) (enabled? boolean?)] [choice (items (listof string?)) (selected integer?)] [vertical (top GUI?) (bottom GUI?)] [horizontal (left GUI?) (right GUI?)]) 12

Assemblings GUIs Pick a fruit: Apple Banana Coconut Ok Cancel label buttons lists vertical stacking two sub-guis horizontal stacking two sub-guis (define gui1 (vertical (horizontal (label "Pick a fruit:") (choice '("Apple" "Banana" "Coconut") 0)) (horizontal (button "Ok" false) (button "Cancel" true)))) 13

Read Screen ; read-screen : GUI -> list-of-string (define (read-screen g) (type-case GUI g [label (t) (list t)] [button (t e?) (list t)] [choice (i s) i] [vertical (t b) (append (read-screen t) (read-screen b))] [horizontal (l r) (append (read-screen l) (read-screen r))])) ;... earlier test cases... (test (read-screen gui1) '("Pick a fruit:" "Apple" "Banana" "Coconut" "Ok" "Cancel")) 14

Function and Data Shapes Match (define-type GUI [label (text string?)] [button (text string?) (enabled? boolean?)] [choice (items (listof string?)) (selected integer?)] [vertical (top GUI?) (bottom GUI?)] [horizontal (left GUI?) (right GUI?)]) (define (read-screen g) (type-case GUI g [label (t) (list t)] [button (t e?) (list t)] [choice (i s) i] [vertical (t b) (append (read-screen t) (read-screen b))] [horizontal (l r) (append (read-screen l) (read-screen r))])) 16

Design Steps Determine the representation define-type Write examples test Create a template for the implementation type-case plus natural recursion, check shape! Finish implementation case-by-case usually the interesting part, but good test cases make it less interesting (i.e., easier!) Run tests 17

Enable Button The name argument is along for the ride : ; enable-button : GUI string -> GUI (define (enable-button g name) (type-case GUI g [label (t) g] [button (t e?) (cond [(equal? t name) (button t true)] [else g])] [choice (i s) g] [vertical (t b) (vertical (enable-button t name) (enable-button b name))] [horizontal (l r) (horizontal (enable-button l name) (enable-button r name))]))... (test (enable-button gui1 "Ok") (vertical (horizontal (label "Pick a fruit:") (choice '("Apple" "Banana" "Coconut") 0)) (horizontal (button "Ok" true) (button "Cancel" true)))) 18

Show Depth (test (show-depth Hello 1 Hello Ok Cancel ) 2 Ok 2 Cancel ) 19

Show Depth Template: (define (show-depth g) (type-case GUI g [label (t)...] [button (t e?)...] [choice (i s)...] [vertical (t b)... (show-depth t)... (show-depth b)...] [horizontal (l r)... (show-depth l)... (show-depth r)...])) 20

Show Depth Template: (define (show-depth g) (type-case GUI g [label (t)...] [button (t e?)...] [choice (i s)...] [vertical (t b)... (show-depth t)... (show-depth b)...] [horizontal (l r)... (show-depth l)... (show-depth r)...])) (show-depth Ok ) 0 Ok 21

Show Depth Template: (define (show-depth g) (type-case GUI g [label (t)...] [button (t e?)...] [choice (i s)...] [vertical (t b)... (show-depth t)... (show-depth b)...] [horizontal (l r)... (show-depth l)... (show-depth r)...])) (show-depth Ok Cancel )... 0 Ok... 0 Cancel... 22

Show Depth Template: (define (show-depth g) (type-case GUI g [label (t)...] [button (t e?)...] [choice (i s)...] [vertical (t b)... (show-depth t)... (show-depth b)...] [horizontal (l r)... (show-depth l)... (show-depth r)...])) recursion results don t have the right labels... 23

Show Depth The n argument is an accumulator: ; show-depth-at : GUI num -> GUI (define (show-depth-at g n) (type-case GUI g [label (t) (label (prefix n t))] [button (t e?) (button (prefix n t) e?)] [choice (i s) g] [vertical (t b) (vertical (show-depth-at t (+ n 1)) (show-depth-at b (+ n 1)))] [horizontal (l r) (horizontal (show-depth-at l (+ n 1)) (show-depth-at r (+ n 1)))])) ; show-depth : GUI -> GUI (define (show-depth g) (show-depth-at g 0)) 24

Programming With Lists Sometimes you can use map, ormap, for/list, etc. ; has-label? : list-of-string string -> bool (define (has-label? l s) (ormap (lambda (e) (string=? e s)) l)) (test (has-label? empty "Banana") false) (test (has-label? '("Apple" "Banana") "Banana") true) 25

Programming With Lists Sometimes you can use map, ormap, for/list, etc. ; has-label? : list-of-string string -> bool (define (has-label? l s) (ormap (lambda (e) (string=? e s)) l)) (test (has-label? empty "Banana") false) (test (has-label? '("Apple" "Banana") "Banana") true) Otherwise, the general design process works for programs on lists using the following data definition: ; A list-of-string is either ; - empty ; - (cons string list-of-string) 26

Programming With Lists ; A list-of-string is either ; - empty ; - (cons string list-of-string) ; has-label? : list-of-string string -> bool (define (has-label? l s) (cond [(empty? l)...] [(cons? l)... (first l)... (has-label? (rest l) s)...])) 28

Programming With Lists ; A list-of-string is either ; - empty ; - (cons string list-of-string) ; has-label? : list-of-string string -> bool (define (has-label? l s) (cond [(empty? l) false] [(cons? l) (or (string=? (first l) s) (has-label? (rest l) s))])) 29

Programming With Lists For lists (and other built-in data), there are also loops. Read more in the documentation, but here s how you can write has-label? more succinctly using lists: (define (has-label? l desired-s) (for/or ([actual-s (in-list l)]) (equal? desired-s actual-s))) 30