CSE 341 Section Handout #6 Cheat Sheet

Similar documents
Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

Scheme Quick Reference

Scheme Quick Reference

CSC 533: Programming Languages. Spring 2015

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

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

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

Functional Programming Languages (FPL)

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

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

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

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

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

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

Essentials of Programming Languages Language

Essentials of Programming Languages Language

Programming Languages

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

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

User-defined Functions. Conditional Expressions in Scheme

Chapter 15 Functional Programming Languages

SCHEME AND CALCULATOR 5b

Documentation for LISP in BASIC

Scheme as implemented by Racket

Common LISP Tutorial 1 (Basic)

Scheme: Strings Scheme: I/O

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

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

Module 8: Local and functional abstraction

Modern Programming Languages. Lecture LISP Programming Language An Introduction

How to Design Programs Languages

CSc 520 Principles of Programming Languages

Lisp. Versions of LISP

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

Organization of Programming Languages CS3200/5200N. Lecture 11

CS115 - Module 9 - filter, map, and friends

The Typed Racket Guide

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

An Introduction to Scheme

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen


MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Section 1.2: What is a Function? y = 4x

Functional Programming. Pure Functional Languages

A Brief Introduction to Scheme (II)

Chapter 15. Functional Programming Languages

Typed Racket: Racket with Static Types

Functional programming with Common Lisp

Sample Final Exam Questions

CS61A Midterm 2 Review (v1.1)

Administrivia. Simple data types

CPS 506 Comparative Programming Languages. Programming Language Paradigm

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

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

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

Symbolic Computation

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

More BSTs & AVL Trees bstdelete

Introduction to Scheme

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming - 2. Higher Order Functions

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

Functional Programming. Big Picture. Design of Programming Languages

Scheme: Expressions & Procedures

Common Lisp. Blake McBride

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

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

Repetition Through Recursion

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Functional Programming. Pure Functional Programming

Functional Languages. Hwansoo Han

CSc 520 Principles of Programming Languages

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

CS 314 Principles of Programming Languages

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

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 in Scheme: The Metacircular Evaluator Eval and Apply

Introduction to Functional Programming

Module 10: General trees

Functional Programming. Pure Functional Languages

LECTURE 16. Functional Programming

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Discussion 4. Data Abstraction and Sequences

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

Advanced features of the Calculate signal tool

An introduction to Scheme

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

Lecture 10: Boolean Expressions

Scheme Basics > (butfirst '(help!)) ()

Section 1.8. Simplifying Expressions

CS 314 Principles of Programming Languages

Box-and-arrow Diagrams

Programming Project #4: A Logo Interpreter Summer 2005

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Excel Functions & Tables

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

Lecture Notes on Lisp A Brief Introduction

How to Design Programs Languages

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

Transcription:

Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5 "hello" (list 3 82.9) 73) Constructs function call: (f arg1 arg2 arg3... argn) variable binding: (define sym expr) function binding: (define (f p1 p2... pn) expr) function binding (define (f p1 p2... pn) with helpers: (define...) (define...) expr) let binding: (let ((sym1 e1) (sym2 e2)... (symn en)) expr) let* binding: (let* ((sym1 e1) (sym2 e2)... (symn en)) expr) if expression: (if test e1 e2) cond expression: (cond (test1 e1) (test2 e2)... (testn en)) (cond (test1 e1) (test2 e2)... (else en)) Useful procedures arithmetic: +, -, *, /, modulo, quotient, remainder mathematical: abs, sin, cos, max, min, expt, sqrt, floor, ceiling, truncate, round relational: =, <, >, <=, >= equality: eq?, eqv?, equal? logical: and, or, not type predicates: number? integer? real? symbol? boolean? string? list? higher-order: map, filter, foldl, foldr, sort, andmap, ormap List procedures length length of a list car first element of a list cdr rest of the list cons takes a value and a list and joins them; ML's :: append joins >= 2 lists together; ML's @ list forms a list from a sequence of values member whether a value is in a list remove removes one occurrence of a value from a list null? is something an empty list? pair? is something a nonempty list?

Questions 1. For each of the following definitions of a factorial function, identify the parenthesis error: a. (define (fact n) (if (= n 0) (1) (* n (fact (- n 1))))) b. (define (fact n) (if = n 0 1 (* n (fact (- n 1))))) c. (define fact (n) (if (= n 0) 1 (* n (fact (- n 1))))) d. (define (fact n) (if (= n 0) 1 (* n fact (- n 1)))) e. (define (fact n) (if (= n 0) 1 (* n ((fact) (- n 1))))) 2. Use the R5RS Scheme standard documentation web site to figure out the following: a. How do you form a comment in Scheme? b. Is there a syntax for multi-line comments? c. How is the expression (/ a b c d) evaluated (i.e., left-to-right or right-to-left)? d. How would you compare to see if one string is less than another? e. How can you sort a list of integers? 3. Define a function called days-in-month that takes an integer representing a month as an argument and that returns the number of days in that month. You may assume that the month value passed is between 1 and 12 inclusive. You may also assume that the month is not part of a leap year. The following table shows the number of days in each month: Month 1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 Jun 7 Jul 8 Aug 9 Sep 10 Oct 11 Nov 12 Dec Days 31 28 31 30 31 30 31 31 30 31 30 31 For example, the call of (days-in-month 5) would return 31. 4. Define a function called pow that takes two integers as arguments and that returns the result of raising the first integer to the power of the second (i.e., (pow x y) should return x y ). You may assume that the power is not negative. For our purposes, we will assume that every integer to the 0 power is 1 (this isn't true of 0 to the 0, but that's okay). For example, (pow 2 10) should return 1024. 5. Define a function called sum-to that accepts an integer n and that computes the sum of the first n reciprocals. That is: n 1 i=1 i For example, (sum-to 3) should return (1 + 1 / 2 + 1 / 3 ) = 1 5 / 6. The function should return 0 if n is 0. You may assume that the function is not passed a negative value of n. Notice that unlike ML, Scheme can compute these values exactly as rational numbers rather than using the real type.

Problems (continued) 6. Define a procedure named sum that accepts a list of numbers as a parameter and returns the sum of all the numbers in the list. For example, the call of (sum (list 1 2-3 4 5)) should return 9. (What happens if you put some real numbers in the list? Fractions? Etc.) 7. Define a procedure named stutter that takes a list as an argument and that returns the list obtained by replacing every value in the list with two of that value. For example, the call of (stutter '(1 2 3)) should return (1 1 2 2 3 3). 8. Define a procedure named multiples that accepts two integer parameters n and k that returns a list of the first n multiples of k. For example, the call of (multiples 3 5) should return (5 10 15). 9. a. Write a procedure named positive-sum that that takes a list as an argument and that returns the sum of the positive numbers in the list. works on lists of integers only; for example, the call of (positive-sum '(1-5 2 3-6 4 7)) should return 17. Use your code from the previous sum problem as a basis to get you started. b. Modify your function so that it can handle lists where some of the elements are non-numbers (skip them). The list might contain inner lists; skip them entirely. (In other words, don't worry about any numbers that might appear inside of any inner lists). For example, the call of (positive-sum '(1 a b 3.4-5 "hello" (2-1 3) -8)) should return 4.4. 10. Define a procedure named flatten that takes a list as an argument and that returns the list obtained by eliminating internal list structures. For example, the call of: (flatten '(1 2 a (b c (d e (f)) g) () () 13)) should return (1 2 a b c d e f g 13).

Solutions 1. Recall that the correct definition is: (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) The errors are as follows: a. (define (fact n) (if (= n 0) (1) (* n (fact (- n 1))))) (1) is not a function b. (define (fact n) (if = n 0 1 (* n (fact (- n 1))))) the if has 5 arguments c. (define fact (n) (if (= n 0) 1 (* n (fact (- n 1))))) bad define with 3 arguments instead of 2 d. (define (fact n) (if (= n 0) 1 (* n fact (- n 1)))) the call on * includes fact as if it were a number e. (define (fact n) (if (= n 0) 1 (* n ((fact) (- n 1))))) (fact) is a bad call 2. This information can be found in the R5RS standard: a. For the question about comments, go to the index and look up "comment" to find that anything after a semi-colon is considered a comment. b. Scheme has only single-line comments. c. In evaluating, (/ a b c d), the standard says "associating to the left", which means it is evaluated as, (((a / b) / c) / d). d. Looking through the index for things that begin with "string", you'll find a function string<? which you can call by saying, (string<? "hello" "there"). e. You can sort a list of integers with an expression such as, (sort '(1 5 2 7 4 8 3) <). 3. (define (days-in-month m) (cond ((or (= m 9) (= m 4) (= m 6) (= m 11)) 30) ((= m 2) 28) (else 31))) 4. (define (pow x y) (if (= 0 y) 1 (* x (pow x (- y 1))))) 5. (define (sum-to n) (if (= 1 n) 1 (+ (/ 1 n) (sum-to (- n 1)))))

Solutions 6. (define (sum lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))))) 7. (define (stutter lst) (if (null? lst) () (cons (car lst) (cons (car lst) (stutter (cdr lst)))))) 8. (define (multiples n m) (define (explore i) (if (> i n) () (cons (* i m) (explore (+ i 1))))) (explore 1)) 9. ; a) (define (positive-sum lst) (cond ((null? lst) 0) ((>= (car lst) 0) (+ (car lst) (positive-sum (cdr lst))))) (else (positive-sum (cdr lst)))) ; b) (ignoring non-numbers) (define (positive-sum lst) (cond ((null? lst) 0) ((and (number? (car lst)) (>= (car lst) 0)) (+ (car lst) (positive-sum (cdr lst)))) (else (positive-sum (cdr lst))))) 10. (define (flatten lst) (cond ((null? lst) ()) ((list? (car lst)) (append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst))))))