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

Size: px
Start display at page:

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

Transcription

1 CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually write a Scheme interpreter in Project 4! Scheme is a dialect of the Lisp programming language, a language dating back to 958. The popularity of Scheme within the programming language community stems from its simplicity in fact, previous versions of CS 6A were taught in the Scheme language. 2 Primitives Scheme has a set of atomic primitive expressions. Atomic means that these expressions cannot be divided up. scm> scm> scm> #t True scm> #f False Unlike in Python, the only primitive in Scheme that is a false value is #f and its equivalents, false and False. This means that 0 is not false. The define special form defines variables and procedures by binding a value to a variable, just like the assignment statement in Python. When a variable is defined, the define special form returns a symbol of its name. A procedure is what we call a function in Scheme! The syntax to define a variable and procedure are: (define <variable name> <value>) (define (<function name> <parameters>) <function body>)

2 2 Scheme 3 Call Expressions Call expressions apply a procedure to some arguments. (<operator> <operand> <operand2>...) Call expressions in Scheme work exactly like they do in Python. To evaluate them:. Evaluate the operator to get a procedure. 2. Evaluate each of the operands from left to right. 3. Apply the value of the operator to the evaluated operands. For example, consider the call expression (+ 2). First, we evaluate the symbol + to get the built-in addition procedure. Then we evaluate the two operands and 2 to get their corresponding atomic values. Finally, we apply the addition procedure to the values and 2 to get the return value 3. Operators may be symbols, such as + and *, or more complex expressions, as long as they evaluate to procedure values. scm> (- ) ; - 0 scm> (/ 8 4 2) ; 8 / 4 / 2 scm> (* (+ 2) (+ 2)) ; ( + 2) * ( + 2) 9 Some important built-in functions you ll want to know are: +, -, *, / =, >, >=, <, <= quotient, modulo even?, odd? Questions 3. What would Scheme display? scm> (define a (+ 2)) a scm> (define b (+ (* a a) (* 4 4))) b scm> b

3 Scheme 3 25 scm> (+ (modulo 0 3) (quotient 0 3)) 5 scm> (> (+ 2 3) (* 2 3)) #f scm> (even? (quotient (* 4 6) 2)) #t scm> (= (+ (- (* 5 4) 3) 2) ) #f 4 Special Forms Special form expressions contain a special form as the operator. Special form expressions do not follow the same rules of evaluation as call expressions. Each special form has its own rules of evaluation that s what makes them special! The special forms we ll see in today s discussion include if, and, or, lambda, and define. 4. If Expression The if special form allows for control flow in our programs. An if expression looks like this: (if <predicate> <if-true> [if-false]) <predicate> and <if-true> are required expressions ad [if-false] is optional. The rules for evaluation are as follows:. Evaluate <predicate>. 2. If <predicate> evaluates to a truth-y value, evaluate <if-true> and return its value. Otherwise, evaluate [if-false] if provided and return its value. This is a special form because not all operands will be evaluated! Only one of the second and third operands is evaluated, depending on the value of the first operand. Notice that the behavior of an if special form is similar to that of an if statement in Python - the major difference is that an if special form evaluates to a value, whereas if statements, being statements, do not evaluate to anythin.

4 4 Scheme Remember that only #f is a false-y value in Scheme; everything else is truth-y. scm> (if (< 4 5) 2) scm> (if #f (/ 0) 42) 42 scm> (if (= (+ 2) 3) 'equal) equal

5 Scheme Boolean Operators Like Python, Scheme also has the boolean operators and, or, and not. and and or are special forms because they are short-circuiting operators. and takes in any amount of operands and evaluates these operands from left to right until one evaluates to a false-y value. It either returns that first false-y value or the value of the last expression if all operands evaluate to a truth-y value. or takes in any amount of operands and evaluates these operands from left to right until one evaluates to a truth-y value. It either returns that first truth-y value or the value of the last expression if all operands evaluate to a false-y value. not takes in a single operand, evaluates it, and returns its opposite truthiness value. Because its one operand is always evaluated, not is a regular procedure rather than a special form. scm> (and 25 32) 32 scm> (or #f (< 4 3) (= 2)) #f scm> (or (/ 0)) ; Short-circuits scm> (not (odd? 0)) ; Not a special form! #t Questions 4. What would Scheme display? scm> (if (or #t (/ 0)) (/ 0)) scm> (if (> 4 3) ( ) (+ 3 4 (* 3 2))) 0 scm> ((if (< 4 3) + -) 4 00) -96 scm> (if 0 2)

6 6 Scheme 4.3 Lambdas and Defining Functions All Scheme procedures are lambda procedures. One way to create a procedure is to use the lambda special form. (lambda (<param> <param2>...) <body>) This expression creates a lambda function with the given parameters and body, but does not evaluate the body. Just like in Python, the body expression is not evaluated until the lambda function is called and applied to some argument values. The fact that neither operand is evaluated is what makes lambda a special form. Another similarity to Python is that lambda expressions do not assign the returned function to any name. We can assign the value of an expression to a name with the define special form: (define <name> <expr>) For example, (define square (lambda (x) (* x x))) creates a lambda procedure that squares its argument and assigns that procedure to the name square. The second version of the define special form is a shorthand for this function definition: (define (<name> <param> <param2...>) <body>) This expression creates a function with the given parameters and body and binds it to the given name. scm> (define square (lambda (x) (* x x))) ; Bind the lambda function to the name square square scm> (define (square x) (* x x)) ; Equivalent to the line above square scm> square (lambda (x) (* x x)) scm> (square 4) 6 Questions 4. Write a function that returns the factorial of a number. (define (factorial x) (if (< x 2) (* x (factorial (- x ))))) 4.2 Write a function that returns the n th Fibonacci number. (define (fib n)

7 (if (or (= n 0) (= n )) n (+ (fib (- n )) (fib (- n 2))))) Scheme 7

8 8 Scheme 5 Pairs and Lists Scheme lists implement an abstract data type known as a linked list with a sequence of pairs. We define a list to be one of two things: the empty list, nil a pair whose second element is a list Linked lists are recursive data structures. The base case is the empty list. We can use the same procedures used to construct and select from pairs to construct and select from lists: (cons first rest) constructs a list with the given first element and rest of the list (car lst) gets the first item of the list (cdr lst) gets the rest of the list scm> nil () scm> (define lst (cons (cons 2 (cons 3 nil)))) lst scm> lst ( 2 3) scm> (car lst) scm> (cdr lst) (2 3) scm> (car (cdr lst)) 2 scm> (cdr (cdr (cdr lst))) () In general, the rule for displaying a pair is as follows: a dot separates the car and cdr fields of a pair. However, if the dot is immediately followed by an open parenthesis, then remove the dot and the parenthesis pair. Thus, (0. (. 2)) becomes (0. 2). Intuitively, a dot will be followed by an open parenthesis if the cdr of a pair is another pair. scm> (cons (cons 2 3)) ( 2. 3) scm> (cons (cons 2 (cons 3 nil))) ( 2 3) Two other common ways of creating lists is using the built-in list procedure or the quote special form: The list procedure takes in an arbitrary amount of arguments. Because it is a procedure, all operands are evaluated when list is called. A list is constructed with the values of these operands and is returned.

9 Scheme 9 The quote special form takes in a single operand. It returns this operand exactly as is, without evaluating it. Note that this special form can be used to return any value, not just a list. scm> (list 2 3) ( 2 3) scm> (quote ( 2 3)) ( 2 3) scm> '( 2 3) ; Equivalent to the previous quote expression ( 2 3) To check if a list is a well-formed list (i.e. it fits one of the descriptions in the definition above), use the predicate procedure list?. =, eq?, equal? = can only be used for comparing numbers. eq? behaves like == in Python for comparing two non-pairs (numbers, booleans, etc.). Otherwise, eq? behaves like is in Python. equal? compares pairs by determining if their cars are equal? and their cdrs are equal?(that is, they have the same contents). Otherwise, equal? behaves like eq?. scm> (define a '( 2 3)) a scm> (= a a) Error scm> (equal? a '( 2 3)) #t scm> (eq? a '( 2 3)) #f scm> (define b a) b scm> (eq? a b) #t

10 0 Scheme Questions 5. Write a function which takes two lists and concatenates them. Notice that simply calling (cons a b) would not work because it will create a deep list. (define (concat a b) (if (null? a) b (cons (car a) (concat (cdr a) b)))) Video walkthrough scm> (concat '( 2 3) '(2 3 4)) ( ) 5.2 Write a function that takes an element x and a non-negative integer n, and returns a list with x repeated n times. (define (replicate x n) (if (= n 0) nil (cons x (replicate x (- n ))))) Video walkthrough scm> (replicate 5 3) (5 5 5)

11 Scheme 5.3 A run-length encoding is a method of compressing a sequence of letters. The list (a a a b a a a a) can be compressed to ((a 3) (b ) (a 4)), where the compressed version of the sequence keeps track of how many letters appear consecutively. Write a function that takes a compressed sequence and expands it into the original sequence. Hint: You may want to use concat and replicate. (define (uncompress s) (if (null? s) s (concat (replicate (car (car s)) (car (cdr (car s)))) (uncompress (cdr s))))) Video walkthrough scm> (uncompress '((a ) (b 2) (c 3))) (a b b c c c) 5.4 Write a function that takes a procedure and applies it to every element in a given list. (define (map fn lst) (if (null? lst) nil (cons (fn (car lst)) (map fn (cdr lst))))) scm> (map (lambda (x) (* x x)) '( 2 3)) ( 4 9) 5.5 Write a function that takes a procedure and applies to every element in a given nested list. The result should be a nested list with the same structure as the input list, but with each element replaced by the result of applying the procedure to that element. Use the built-in list? procedure to detect whether a value is a list. (define (deep-map fn lst) (cond ((null? lst) lst) ((list? (car lst)) (cons (deep-map fn (car lst)) (deep-map fn (cdr lst)))) (else (cons (fn (car lst)) (deep-map fn (cdr lst)))) ) ) scm> (deep-map (lambda (x) (* x x)) '( 2 3)) ( 4 9)

12 2 Scheme scm> (deep-map (lambda (x) (* x x)) '( ((4) 5) 9)) ( ((6) 25) 8)

13 Scheme 3 6 Extra Questions 6. Fill in the following to complete an abstract tree data type: (define (make-tree label branches) (cons label branches)) (define (label tree) (define (branches tree) (define (label tree) (car tree)) (define (branches tree) (cdr tree)) 6.2 Using the abstract data type above, write a function that sums up the entries of a tree, assuming that the entries are all numbers. Hint: you may want to use the map function you defined above, and also write a helper function for summing up the entries of a list. (define (tree-sum tree) (+ (label tree) (sum (map tree-sum (branches tree))))) (define (sum lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))))) 6.3 Using the abstract data type above, write a function that creates a new tree where the entries are the product of the entries along the path to the root in the original tree. Hint: you may want to write a helper function that keeps track of the current product (define (path-product-tree t) (define (path-product t product) (let ((prod (* product (label t)))) (make-tree prod (map (lambda (t) (path-product t prod)) (branches tree))))) (path-product t ))

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

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

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

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

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

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### 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 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

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

SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

### SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

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

CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

### CS 61A Discussion 8: Scheme. March 23, 2017

CS 61A Discussion 8: Scheme March 23, 2017 Announcements Ants is due today. Finish it! Also turn it in! HW 6 is due tomorrow. Finish it! Also turn it in! HW party today from 6:30-8:30p in 247 Cory. Midterm

### Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

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

Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

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

Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

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

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

### Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

### Lisp. Versions of LISP

Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major

### Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

### FP Foundations, Scheme

FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

### Functional Programming

Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

### Documentation for LISP in BASIC

Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

### CSE 341 Section Handout #6 Cheat Sheet

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

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

### Macros & Streams Spring 2018 Discussion 9: April 11, Macros

CS 61A Macros & Streams Spring 2018 Discussion 9: April 11, 2018 1 Macros So far, we ve mostly explored similarities between the Python and Scheme languages. For example, the Scheme list data structure

### TAIL RECURSION, SCOPE, AND PROJECT 4 11

TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration

### INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

INTERPRETERS 8 COMPUTER SCIENCE 61A November 3, 2016 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that understand other programs. In

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### Programming Languages

Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

### ;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the

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

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

### Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

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

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

### 61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

61A LECTURE 18 SCHEME Steven Tang and Eric Tzeng July 24, 2013 What s happening today? We re learning a new language! After you know one language (Python), learning your second (Scheme) is much faster

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

### CSC 533: Programming Languages. Spring 2015

CSC 533: Programming Languages Spring 2015 Functional programming LISP & Scheme S-expressions: atoms, lists functional expressions, evaluation, define primitive functions: arithmetic, predicate, symbolic,

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

### Structure and Interpretation of Computer Programs

CS 61A Fall 2016 Structure and Interpretation of Computer Programs Final INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except

### CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

### ITERATORS AND STREAMS 9

ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

YOUR NAME PLEASE: *** SOLUTIONS *** Computer Science 201b SAMPLE Exam 1 SOLUTIONS February 15, 2015 Closed book and closed notes. No electronic devices. Show ALL work you want graded on the test itself.

### Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

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

Deferred operations Continuations 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (define the-cons (cons 1 #f)) (set-cdr! the-cons the-cons) (define (run-in-circles l) (+

### Programming Languages

Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

### CS61A Midterm 2 Review (v1.1)

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

### Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

### Chapter 1. Fundamentals of Higher Order Programming

Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

### Streams and Evalutation Strategies

Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance

### Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

### Structure and Interpretation of Computer Programs

CS 6A Fall 206 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

### INTERPRETERS AND TAIL CALLS 9

INTERPRETERS AND TAIL CALLS 9 COMPUTER SCIENCE 61A April 9, 2015 We are beginning to dive into the realm of interpreting computer programs that is, writing programs that understand other programs. In order

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### Scheme as implemented by Racket

Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

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

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### Introduction to Functional Programming

Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,

### Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.

### Functional Programming Languages (FPL)

Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you

### CS450 - Structure of Higher Level Languages

Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### Repetition Through Recursion

Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

### CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

### Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

CS17 Integrated Introduction to Computer Science Klein Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018 Contents 1 Factorial 3 2 Fibonacci 4 3 Odds Only 5 4 Increment All 6 5 Frequency 6 6 Sublist 7 6.1

### Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Homework 1 Due 14 February Handout 3 CSCI 334: Spring, 2012 Reading 1. (Required) Mitchell, Chapter 3. 2. (As Needed) The Lisp Tutorial from the Links web page, as needed for the programming questions.

### Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

### April 2 to April 4, 2018

MORE SCHEME COMPUTER SCIENCE MENTORS 61A April 2 to April 4, 2018 1 Scheme 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. (a) (cons (cons 1 nil) (cons 2 (cons (cons 3

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

HIERARCHICAL DATA 5 COMPUTER SCIENCE 61AS Concepts and Definitions 1. What is a Tree? How is it different from a Deep List? When would you use one over the other? A Tree is an Abstract Data Type composed

### MetacircularScheme! (a variant of lisp)

MetacircularScheme! (a variant of lisp) Lisp = Beauty Passed on through ages Basic Expressions (function arg1 arg2 ) To evaluate an expression, apply the functionto the arguments. (+ 1 2)? => 3 (sqrt4)?

### A LISP Interpreter in ML

UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,

### Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Homework 1 Due 14 February Handout 3 CSCI 334: Spring, 2017 Notes This homework has three types of problems: Self Check: You are strongly encouraged to think about and work through these questions, but

### Recursion & Iteration

Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion

### CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

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

UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully.

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

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

A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner