# CS 314 Principles of Programming Languages

Size: px
Start display at page:

Transcription

1 CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018

2 Review: Defining Scheme Functions (define <fcn-name> ( lambda ( <fcn-params> ) <expression> ) ) Example: Given function pair? (true for non-empty lists, false o/w) and function not (boolean negation): <fcn-params> (define atom? ( lambda ( object ) ) ) ( not (pair? object) ) <expression> 2

3 Scheme: Functions as First Class Values (Higher-Order) Higher-order functions - Take a function as argument - Or return a function as a result 3

4 Scheme: Functions as First Class Values (Higher-Order) Functions as arguments: (define f (lambda (g x) (g x) ) ) (f number? 0) (number? 0) #t (f length '(1 2)) (length '(1 2)) 2 (f (lambda (x) (* 2 3)) 3) ((lambda (x) (* 2 3)) 3) (* 2 3) 6 4

5 Review: Higher-Order Functions Functions as returned value: (define plusn ( lambda ( n ) ( lambda (x) (+ n x) ) ) ) Return a function (plusn 5) evaluates to a function that adds 5 to its argument: ((plusn 5) 6) 5

6 Review: Higher-Order Functions In general, any n-ary function (lambda (x_1 x_2 x_n) e) can be rewritten as a nest of n unary functions: (lambda (x_1) (lambda (x_2) ( (lambda(x_n) e) ))) 6

7 Review: Higher-Order Functions In general, any n-ary function (lambda (x_1 x_2 x_n) e) can be rewritten as a nest of n unary functions: (lambda (x_1) (lambda (x_2) ( (lambda(x_n) e) ))) This translation process is called currying. It means that having functions with multiple parameters do not add anything to the expressiveness of the language: ( (lambda (x_1 x_2... x_n) e) v_1 v_2... v_n) ( (((lambda (x_1) (lambda (x_2)... (lambda (x_n) e )...)) v_1) v_2)... v_n) 7

8 Higher-order Functions: map function (define map (lambda ( f l ) (if (null? l) '( ) list ( cons ( f (car l) ) ( map f (cdr l) ) ) ) ) ) Apply f to the first element of l Apply map to the rest of l map takes two arguments: a function f and a list l map builds a new list by applying the function to every element of the (old) list 8

9 More on Higher-Order Functions reduce Higher order function that takes a binary, associative operation and uses it to roll up a list (define reduce (lambda ( op l id) ( if (null? l) id (op (car l) (reduce op (cdr l) id)) ) ) ) Example: (reduce + '( ) 0) (+ 10 (reduce + '(20 30) 0)) (+ 10 (+ 20 (reduce + '(30) 0))) (+ 10 (+ 20 (+ 30 (reduce + '() 0)))) (+ 10 (+ 20 (+ 30 0))) 60 9

10 Higher-Order Functions Compose higher order functions to form compact powerful functions (define sum (lambda (f l) (reduce + ( map f l ) 0) ) ) (sum (lambda (x) (* 2 x)) '(1 2 3) ) (reduce (lambda (x y) (+ 1 y)) '(a b c) 0) 10

11 Lexical Scoping and let, let*, and letrec All are variable binding operations: LET = let, let*, letrec (LET ( (v1 e1) (v2 e2) (vn en) ) e ) 11

12 Lexical Scoping and let, let*, and letrec let: - binds variables to values (no specific order), and evaluate body e using bindings - new bindings are not effective during evaluation of any ei let*: - binds variables to values in textual order of write-up - new binding ei-1 is effective for next ei. letrec: - bindings of variables to values in no specific order - independent evaluations of all ei to values have to be possible - new bindings effective for all ei - mainly used for recursive function definitions 12

13 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 13

14 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 14

15 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 15

16 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 16

17 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 17

18 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 18

19 let and let* example ( let ( (a 5) (b 6) ) ( + a b ) ) ;; 11 (let ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; ERROR: a: undefined (let* ( (a 5) (b (+ a 6)) ) ( + a b ) ) ;; 16 Note: let and let* do not add anything to the expressiveness of the language, i.e., they are only a convenient shorthand. For instance, (let ((x1 e1) (x2 e2)) e) can be rewritten as ((lambda (x y) e) v1 v2) 19

20 letrec examples Typically used for local definitions for recursive functions (letrec ( (a 5) (b ( lambda() (+ a 6) ) ) ) ( + a (b) ) ) ;; 16 (letrec ( (b (lambda () (+ a 6) ) ) (a 5) ) ( + a (b) ) ) ;; 16 (letrec ( (even? (lambda (x) ( or (= x 0) (odd? (- x 1)) ) ) ) (odd? (lambda (x) ( and (not (= x 0)) (even? (- x 1)) ))) ) ( list (even? 3) (even? 20) (odd? 21) ) ) ;; (#f #t #t) 20

21 letrec examples Typically used for local definitions for recursive functions (letrec ( (a 5) (b ( lambda () (+ a 6) ) ) ) ( + a (b) ) ) ;; 16 (letrec ( (b (lambda () (+ a 6) ) ) (a 5) ) ( + a (b) ) ) ;; 16 (letrec ( (even? (lambda (x) ( or (= x 0) (odd? (- x 1)) ) ) ) (odd? (lambda (x) ( and (not (= x 0)) (even? (- x 1)) ))) ) ( list (even? 3) (even? 20) (odd? 21) ) ) ;; (#f #t #t) 21

22 letrec examples Typically used for local definitions for recursive functions (letrec ( (even? (lambda (x) ( or (= x 0) (odd? (- x 1)) ) ) ) (odd? (lambda (x) ( and (not (= x 0)) (even? (- x 1)) ))) ) ( list (even? 3) (even? 20) (odd? 21) ) ) ;; (#f #t #t) 22

23 letrec examples Typically used for local definitions for recursive functions (letrec ( (even? (lambda (x) ( or (= x 0) (odd? (- x 1)) ) ) ) (odd? (lambda (x) ( and (not (= x 0)) (even? (- x 1)) ))) ) ( list (even? 3) (even? 20) (odd? 21) ) ) ;; (#f #t #t) 23

24 Next Lecture Things to do: Read Scott, Chapter (4th Edition) or Chapter (3rd Edition) 24

### 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

### 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:

### 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.

### 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

### 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

### 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

### 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

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

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

### 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

### 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

### Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?

### 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

### 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,

### Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Racket Next 2+ weeks will use the Racket language (not ML) and the DrRacket programming environment (not emacs) Installation / basic usage instructions on course website CSE34: Programming Languages Lecture

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

Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy

### 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

### 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 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,

### 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

### 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

### 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

### 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

### 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

### 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).

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

### 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

### 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

### 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

### 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

### Functional Programming

Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario When the Function is the Thing In O-O programming, you typically know where an action is needed,

### 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

### 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

### An Introduction to Scheme

An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

### 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

### 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

### 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

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

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### Functional abstraction

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### 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

### 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

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.

### 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

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

CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

### 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

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

Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

### CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

### More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

More Scheme CS 331 Quiz 1. What is (car ((2) 3 4))? (2) 2. What is (cdr ((2) (3) (4)))? ((3)(4)) 3. What is (cons 2 (2 3 4))? (2 2 3 4) 4. What is the length of the list (()()()())? 4 5. Which element

### 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 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

### 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,

### CS 360 Programming Languages Day 14 Closure Idioms

CS 360 Programming Languages Day 14 Closure Idioms Why lexical scope rocks Last time: currying Today: implementing callbacks and object-oriented programming. Review: mutable state Racket's variables are

### 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,

### CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

### 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

### 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

### Functional Programming. Contents

Functional Programming Gunnar Gotshalks 2003 September 27 Contents Introduction... 2 Example for Inner Product... 2 Basis Set for Functionals... 3 Example Function Definitions... 3 Lisp Builtin Functionals...

### Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus CS 550 Programming Languages Jeremy Johnson 1 Lambda Calculus The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics

### Evaluating Scheme Expressions

Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure

### low and not larger than high. 18 points

CSE 330 Test 1 (1 point) Spring 015 Multiple Choice. Write your answer to the LEFT of each problem. 3 points each 1. Lisp was invented at: A. IBM B. MIT C. Netscape D. Stanford. In C++, what operator is

### Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

Lecture 8: Recursing Lists CS15: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Defining Recursive Procedures 1. Be optimistic! Assume you can solve

### 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

### Overview. CS301 Session 3. Problem set 1. Thinking recursively

Overview CS301 Session 3 Review of problem set 1 S-expressions Recursive list processing Applicative programming A look forward: local variables 1 2 Problem set 1 Don't use begin unless you need a side

### 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.

### Putting the fun in functional programming

CM20167 Topic 4: Map, Lambda, Filter Guy McCusker 1W2.1 Outline 1 Introduction to higher-order functions 2 Map 3 Lambda 4 Filter Guy McCusker (1W2.1 CM20167 Topic 4 2 / 42 Putting the fun in functional

### Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS This document contains solutions to the problems on the final exam review problems posted earlier except for

### CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan 1 Reading SICP, page 11-19, Section 1.1.6 Little Schemer, Chapter 2 2 The Idea of Syntax Abstraction Problem. Often programming tasks are repetitive,

### CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

### Datatype declarations

Datatype declarations datatype suit = HEARTS DIAMONDS CLUBS SPADES datatype a list = nil (* copy me NOT! *) op :: of a * a list datatype a heap = EHEAP HEAP of a * a heap * a heap type suit val HEARTS

### CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages Functional Programming with Lists 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as a linked data structure

### 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

### 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

### 6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### Turtles All The Way Down

Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of

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

CS 314 Principles of Programming Languages Lecture 21 Zheng Zhang Department of Computer Science Rutgers University Wednesday 23 rd November, 2016 Zheng Zhang 1 CS@Rutgers University Class Information

### 1.3. Conditional expressions To express case distinctions like

Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

### Midterm Examination (Sample Solutions), Cmput 325

Midterm Examination (Sample Solutions, Cmput 325 [15 marks] Consider the Lisp definitions below (defun test (L S (if (null L S (let ((New (add (cdar L S (test (cdr L New (defun add (A S (if (null S (cons

### CSC324 Functional Programming Efficiency Issues, Parameter Lists

CSC324 Functional Programming Efficiency Issues, Parameter Lists Afsaneh Fazly 1 January 28, 2013 1 Thanks to A.Tafliovich, P.Ragde, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton 1 Example: efficiency

### Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

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

Constructing Lists CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona The most important

### 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

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

### Programming Systems in Artificial Intelligence Functional Programming

Click to add Text Programming Systems in Artificial Intelligence Functional Programming Siegfried Nijssen 8/03/16 Discover thediscover world at the Leiden world University at Leiden University Overview

### 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

### 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

### 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

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

Programming Languages Function-Closure Idioms Adapted from Dan Grossman's PL class, U. of Washington More idioms We know the rule for lexical scope and function closures Now what is it good for A partial

### 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

### What is tail position?

What is tail position? Tail position is defined inductively: The body of a function is in tail position When(if e1 e2 e3) is in tail position, so are e2 ande3 When(let (...) e) is in tail position, so

### Vectors in Scheme. Data Structures in Scheme

Data Structures in Scheme Vectors in Scheme In Scheme, lists and S-expressions are basic. Arrays can be simulated using lists, but access to elements deep in the list can be slow (since a list is a linked

### Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 13: Names, Scopes and Bindings Zheng (Eddy) Zhang Rutgers University February 28, 2018 Review: Names, Scopes and Binding What s in a name? Each name means

### A Brief Introduction to Scheme (I)

A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Scheme Scheme I p.1/44 Scheme: Feature Set A

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

Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns Previous lecture Basics of Scheme Expressions and associated values (or syntax and

### Functional Programming in Scheme

Cristian Giumale / Lecture Notes 1 Functional Programming in Scheme Functional programming languages - or applicative languages - follow closely the Lambda calculus model. Their strengths and weaknesses

### 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.

### Shell CSCE 314 TAMU. Haskell Functions

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

### 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

### CSE 341 Section 5. Winter 2018

CSE 341 Section 5 Winter 2018 Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records