Scheme: Strings Scheme: I/O

Size: px
Start display at page:

Download "Scheme: Strings Scheme: I/O"

Transcription

1 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 Alaska Fairbanks 2017 Glenn G. Chappell

2 Review Scheme: Data quote, eval, & More Code quote suppresses the normal parameter evaluation. The leading single quote is shorthand for quote. > (quote (1 2 3)) ; Same as '(1 2 3) (1 2 3) eval evaluates its parameter. list returns a list of its arguments. > (list "hello" ' )) ("hello" ) > (eval (cdr (list "hello" ' ))) 6 Return an empty list with '() or null. 5 Apr 2017 CS F331 / CSCE A331 Spring

3 Review Scheme: Data Data Format [1/5] The dot notation originally used in S-expressions is also valid in Scheme. > '(1. 2) (1. 2) A list is really shorthand for the equivalent dot notation, again, just as in the original S-expression syntax. > '(1. (2. (3. (4. ())))) ( ) Dot (.) is not a function. It is simply another way of typing S- expressions. If you want a normal function that puts things together the way dot does, use cons. 5 Apr 2017 CS F331 / CSCE A331 Spring

4 Review Scheme: Data Data Format [2/5] The main building block for constructing data structures in Scheme is the pair. This is a node with two pointers. (1. 2) 1 2 We get the item referenced by the left pointer using car; similarly use cdr for the right pointer. > (car '(1. 2)) 1 > (cdr '(1. 2)) 2 5 Apr 2017 CS F331 / CSCE A331 Spring

5 Review Scheme: Data Data Format [3/5] Lists are constructed from pairs and null. (1 2 3) = (1. (2. (3. ()))) NULL 5 Apr 2017 CS F331 / CSCE A331 Spring

6 Review Scheme: Data Data Format [4/5] The full story on the dot syntax is that the dot may optionally be added just before the last item of a list. (1 2 3) = (1. (2. (3. ()))) (1 2. 3) = (1. (2. 3)) NULL 5 Apr 2017 CS F331 / CSCE A331 Spring

7 Review Scheme: Data Data Format [5/5] We can create arbitrary binary trees with the restriction that only leaves have data. ((((). 8). 1). (5. 1)) NULL 8 5 Apr 2017 CS F331 / CSCE A331 Spring

8 Review Scheme: Data Taking a Varying Number of Parameters We wrote a procedure sum that, like +, takes an arbitrary number of parameters. A procedure call is a pair: (PROC. ARGS). And define will also take this form of a picture of a function call. (define (sum. args) )... How to make a recursive call on (cdr args)? (sum. (cdr args)) ; WRONG! (eval (cons sum (cdr args))) ; Okay (apply sum (cdr args)) (sum. (cdr args)) is just another way to write (sum cdr args), which is not what we want. ; Okay (and also clearer) 5 Apr 2017 CS F331 / CSCE A331 Spring

9 Review Scheme: Data Manipulating Trees Recall that a Scheme value is null, or a pair, or an atom. Any value for which both null? and pair? return #f is an atom. We can write procedures that deal with a structure, not as a list, but as a tree, traversing the tree and dealing with atoms in some way. A useful procedure is error, which, much like Haskell s error, takes a string, and crashes with a message if it executes. See data.scm. 5 Apr 2017 CS F331 / CSCE A331 Spring

10 Scheme: Strings Basics [1/2] String literals in Scheme are surrounded by double quotes. "This is a string." The usual backslash escapes are accepted. "A newline: \na double quote: \" A backslash: \\" Check whether a value is a string with string?. > (string? "42") #t > (string? 42) #f 5 Apr 2017 CS F331 / CSCE A331 Spring

11 Scheme: Strings Basics [2/2] Get the length of a string with string-length. > (string-length "Hello!") 6 Concatenate strings with string-append. > (string-append "abc" "def" "ghi" "jklmnop") "abcdefghijklmnop" Get a substring with (substring STRING START PAST_END). > (substring "Howdy thar!" 2 7) ; Zero-based indices "wdy t" Includes the characters at indices 2, 3, 4, 5, 6, but not 7. 5 Apr 2017 CS F331 / CSCE A331 Spring

12 Scheme: Strings Numeric Conversions Convert a number to a string using number->string. > (number->string 42) "42" Convert a string to a number using string->number. This returns the number, or #f if the conversion could not be done. So the result can be used in an if. > (string->number "42") 42 > (string->number "Hello!") #f (if COND THEN-EXPR ELSE-EXPR) Recall: When the above is evaluated, THEN-EXPR is chosen if COND evaluates to anything other than #f. 5 Apr 2017 CS F331 / CSCE A331 Spring

13 Scheme: Strings Characters [1/2] Character literals generally have the form #\CHAR. #\A ; The 'A' character Some characters have special forms. #\newline #\space Check whether a value is a character with char?. > (char? #\x) #t > (char? "x") #f 5 Apr 2017 CS F331 / CSCE A331 Spring

14 Scheme: Strings Characters [2/2] Convert a character to its numeric version (ASCII value/unicode codepoint) with char->integer. Reverse: integer->char. > (char->integer #\A) 65 > (integer->char 65) #\A Convert between strings and lists of characters with string->list and list->string. > (string->list "Howdy thar!") (#\H #\o #\w #\d #\y #\space #\t #\h #\a #\r #\!) > (list->string '(#\a #\b #\c)) "abc" 5 Apr 2017 CS F331 / CSCE A331 Spring

15 Scheme: Strings Comparisons [1/3] We have seen the Scheme numeric comparison operators: = < <= > >=. These can be used only with numbers. Many Scheme types have their own equality functions, along with the other comparisons, if appropriate. > (string=? "abc" "def") #f > (string=? "42" 42) ERROR > (string<? "abc" "def") #t Also: string<=? string>? string>=? char=? char<? char<=? char>? char>=? 5 Apr 2017 CS F331 / CSCE A331 Spring

16 Scheme: Strings Comparisons [2/3] There are many kinds of equality in Scheme: same object in memory, same primitive value, etc. It is easy to get lost among the various equality functions (eq?, eqv?, etc.). Fortunately, Scheme has equal?. This can be called on any two values. It returns #f if they are of different types. Otherwise, it does an appropriate type-specific equality check. For numbers, it calls =. For strings, it calls string=?. For characters, it calls char=?. For two nulls (empty lists), it returns #t. For pairs, it recursively calls equal? on the cars and the cdrs. So it is a reasonable equality check for lists. > (equal? '(1 (2 3) ((4))) '(1 (2 3) ((4)))) #t 5 Apr 2017 CS F331 / CSCE A331 Spring

17 Scheme: Strings Comparisons [3/3] Because equal? always returns #f for parameters of different types, it may not do what you expect when given numbers. > (= 0 0.0) #t > (equal? 0 0.0) #t Thus, I offer the following rule of thumb. Use = for numeric equality. Use equal? for most other kinds of equality. If you want the code to indicate what type you are comparing, and flag type errors for other types, then use a type-specific equality function (e.g., string=?, char=?). Use other equality functions only if you know what you are doing. 5 Apr 2017 CS F331 / CSCE A331 Spring

18 Scheme: I/O Console Output [1/2] Print a any value with display. String conversion is automatic. No trailing newline is printed. Print a newline with newline. Both of these return void, which does not print in the REPL. > (display "Howdy thar!") Howdy thar! > (newline) > (display #\A) A > (display '(42 #t (300))) (42 #t (300)) > (display +) #<procedure:+> 5 Apr 2017 CS F331 / CSCE A331 Spring

19 Scheme: I/O Console Output [2/2] To do multiple I/O calls in a single expression, use begin. This takes any number of parameters, evaluates them all, in order, and returns the value of the last one. > (begin (display "dog") (display "food") (display "love") ) dogfoodlove 5 Apr 2017 CS F331 / CSCE A331 Spring

20 Scheme: I/O Console Input [1/2] Read a line from the console with read-line. This takes no parameters. It returns the typed-in line with no trailing newline. How can we set a variable equal to the return value of read-line in a procedure? We want a local binding, not a global one. One way is to define inside the procedure. Another is to use let. (let ; Locally bind vars to values in the expression ) ( ) (VARIABLE1 VALUE1) (VARIABLEn VALUEn) ( EXPRESSION ) Scheme s let is much like Haskell s where, except that the definitions come before, instead of after. Punchline: Haskell has let also, but I prefer where. 5 Apr 2017 CS F331 / CSCE A331 Spring

21 Scheme: I/O Console Input [2/2] TO DO Write a procedure to prompt for input, read a line, and then print it, with some explanation ( Here is what you typed: ). Done. See io.scm. 5 Apr 2017 CS F331 / CSCE A331 Spring

22 Scheme: I/O Files Overview Scheme file I/O can be done with the usual open close sequence. On success, opening a file gives a value called a port, by which the open file can be accessed. This is passed to all other file-i/o functions, as usual. But Scheme includes wrappers around this functionality that are more convenient: call-with-output-file and call-with-input-file. 5 Apr 2017 CS F331 / CSCE A331 Spring

23 Scheme: I/O Files Output [1/2] Procedure call-with-output-file takes two parameters: The filename (string). A 1-parameter procedure that takes an output port. Notes On failure, call-with-output-file raises an exception, which will crash if no error handling is done. On success it calls the given procedure, passing the port. The given procedure is responsible for closing the file, by passing the port to close-output-port. A variation, call-with-output-file*, closes the file automatically when the given procedure terminates. 5 Apr 2017 CS F331 / CSCE A331 Spring

24 Scheme: I/O Files Output [2/2] Do file output by passing the port as a second argument to display or newline. Example use of call-with-output-file*: (define (do-output outport) (begin (display "Here is some file content." outport) (newline outport) ) ) (call-with-output-file* "myfile.txt" do-output) 5 Apr 2017 CS F331 / CSCE A331 Spring

25 Scheme: I/O Files Lambdas [1/2] We can do it all with a single procedure using a lambda. As in Haskell, this refers to an unnamed function (procedure). This: (define (f a b) (* a b)) is the same as this: (define f (lambda (a b) (* a b))) Scheme has first-class functions, so we can use a lambda without giving it a name. 5 Apr 2017 CS F331 / CSCE A331 Spring

26 Scheme: I/O Files Lambdas [2/2] Here is our file output as a single expression. (call-with-output-file* "myfile.txt" (lambda (outport) (begin (display "Here is some file content." outport) (newline outport) ) ) ) 5 Apr 2017 CS F331 / CSCE A331 Spring

27 Scheme: I/O Files Input [1/3] Similarly, we can use call-with-input-file to do file input. This takes a filename and a procedure that takes an input port. The given procedure is responsible for closing the file, by passing the port to close-input-port. Or we can use call-with-inputfile*, which closes automatically. Read a line from a file by passing the port to read-line. As before, this normally returns the line as a string with no trailing newline. However, on end of file, read-line returns an EOF object. Check for this by passing the return value to the predicate eof-object?. 5 Apr 2017 CS F331 / CSCE A331 Spring

28 Scheme: I/O Files Input [2/3] Suppose we are reading a file, line by line. How do we repeat? The let procedure has a useful extension: we can give it a name: (let NAME ( ) ) (VARIABLE1 VALUE1) (VARIABLEn VALUEn) ( EXPRESSION ) Within EXPRESSION, we can use NAME as a procedure taking n arguments. These become the values of VARIABLE1 through VARIABLEn in that invocation of the procedure. 5 Apr 2017 CS F331 / CSCE A331 Spring

29 Scheme: I/O Files Input [3/3] TO DO Write a procedure that reads a file, line by line, and prints each line. Extra credit: add line numbers. Done. See io.scm. 5 Apr 2017 CS F331 / CSCE A331 Spring

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

More information

Scheme: Expressions & Procedures

Scheme: Expressions & Procedures Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University

More information

A Brief Introduction to Scheme (II)

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

More information

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G. Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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,

More information

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

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

More information

User-defined Functions. Conditional Expressions in Scheme

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

More information

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

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

More information

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

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

More information

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

More information

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

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

More information

Functional Programming. Pure Functional Languages

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

More information

CS 314 Principles of Programming Languages

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

More information

CS 314 Principles of Programming Languages. Lecture 16

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:

More information

Project 2: Scheme Interpreter

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

More information

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

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,

More information

CSCI337 Organisation of Programming Languages LISP

CSCI337 Organisation of Programming Languages LISP Organisation of Programming Languages LISP Getting Started Starting Common Lisp $ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

More information

LECTURE 16. Functional Programming

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

More information

Functional Programming. Pure Functional Languages

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

More information

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

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

More information

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

More information

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

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

More information

SCHEME AND CALCULATOR 5b

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 information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

More information

CSc 520 Principles of Programming Languages

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

More information

CS 314 Principles of Programming Languages

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.

More information

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

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,

More information

FP Foundations, Scheme

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

More information

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

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

More information

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

More information

Documentation for LISP in BASIC

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

More information

Programming Languages

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

More information

An Introduction to Scheme

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

More information

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

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,

More information

CSE 341 Section Handout #6 Cheat Sheet

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

More information

CS61A Midterm 2 Review (v1.1)

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

More information

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

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

More information

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

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

More information

Functional Programming. Pure Functional Programming

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

More information

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Principles of Programming Languages 2017W, Functional Programming

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.

More information

Typed Racket: Racket with Static Types

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

More information

Introduction to Scheme

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

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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

More information

Scheme as implemented by Racket

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

More information

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

More information

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

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

More information

CS450 - Structure of Higher Level Languages

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.

More information

CS 314 Principles of Programming Languages

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 (

More information

Lisp. Versions of LISP

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

More information

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89 Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

More information

Scheme Quick Reference

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

More information

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

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

More information

YOUR NAME PLEASE: *** SOLUTIONS ***

YOUR NAME PLEASE: *** SOLUTIONS *** 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.

More information

Functional Programming

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

More information

Turtles All The Way Down

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

More information

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

Scheme Basics > (butfirst '(help!)) () Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst

More information

Typed Scheme: Scheme with Static Types

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

More information

Scheme Quick Reference

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

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

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

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) (+

More information

Sample Final Exam Questions

Sample Final Exam Questions 91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will

More information

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

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

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

More information

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

More information

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

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,

More information

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

More information

An Explicit Continuation Evaluator for Scheme

An Explicit Continuation Evaluator for Scheme Massachusetts Institute of Technology Course Notes 2 6.844, Spring 05: Computability Theory of and with Scheme February 17 Prof. Albert R. Meyer revised March 3, 2005, 1265 minutes An Explicit Continuation

More information

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

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

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

More information

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

More information

Introduction to Syntax Analysis Recursive-Descent Parsing

Introduction to Syntax Analysis Recursive-Descent Parsing Introduction to Syntax Analysis Recursive-Descent Parsing CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 10, 2017 Glenn G. Chappell Department of

More information

SOFTWARE ARCHITECTURE 6. LISP

SOFTWARE ARCHITECTURE 6. LISP 1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp and Scheme a Lists in Lisp and Scheme Lists are Lisp s fundamental data structures, but there are others Arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt

More information

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S. Answers to CS61 A Final of May 23, 1997 We repeat the questions; some have typos corrected with respect to the actual exam handed out. Question 1 (5 points): Let us represent a set S of unique expressions,

More information

Functional Programming Languages (FPL)

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

More information

Functional Programming - 2. Higher Order Functions

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

More information

Thoughts on Assignment 4 Haskell: Flow of Control

Thoughts on Assignment 4 Haskell: Flow of Control Thoughts on Assignment 4 Haskell: Flow of Control CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 27, 2017 Glenn G. Chappell Department of Computer

More information

Building a system for symbolic differentiation

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)

More information

Common LISP-Introduction

Common LISP-Introduction Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally

More information

Programming Systems in Artificial Intelligence Functional Programming

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

More information

How to Design Programs Languages

How to Design Programs Languages How to Design Programs Languages Version 4.1 August 12, 2008 The languages documented in this manual are provided by DrScheme to be used with the How to Design Programs book. 1 Contents 1 Beginning Student

More information

6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs 6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

More information

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

(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

More information

Introductory Scheme. Revision 1

Introductory Scheme. Revision 1 Introductory Scheme Revision 1 Joseph W. Lavinus and James D. Arthur (lavinus@cs.vt.edu and arthur@cs.vt.edu) Department of Computer Science Virginia Polytechnic Institute and State University Blacksburg,

More information

Discussion 12 The MCE (solutions)

Discussion 12 The MCE (solutions) Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of

More information

Building a system for symbolic differentiation

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)

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

More information

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

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

More information

Essentials of Programming Languages Language

Essentials of Programming Languages Language Essentials of Programming Languages Language Version 6.90.0.26 April 20, 2018 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly

More information

CS61A Notes Disc 11: Streams Streaming Along

CS61A Notes Disc 11: Streams Streaming Along CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever

More information

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

More information

INTRODUCTION TO SCHEME

INTRODUCTION TO SCHEME INTRODUCTION TO SCHEME PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/110 SCHEME: A FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be passed as

More information

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G. Writing a Lexer CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1

More information

Comp 311: Sample Midterm Examination

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

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information