MetacircularScheme! (a variant of lisp)

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

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

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

CS 360 Programming Languages Interpreters

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

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

SCHEME AND CALCULATOR 5b

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

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

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

Functional Programming. Pure Functional Languages

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

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. Pure Functional Programming

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

CSCC24 Functional Programming Scheme Part 2

Administrivia. Simple data types

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

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

Documentation for LISP in BASIC

CS 314 Principles of Programming Languages

LECTURE 16. Functional Programming

CSc 520 Principles of Programming Languages

FP Foundations, Scheme

Programming Languages

Project 2: Scheme Interpreter

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

CS61A Midterm 2 Review (v1.1)

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

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

CSc 520 Principles of Programming Languages

Functional Languages. Hwansoo Han

Discussion 12 The MCE (solutions)

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

An Introduction to Scheme

Chapter 1. Fundamentals of Higher Order Programming

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

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

An introduction to Scheme

CS1102: Macros and Recursion

Warm-up and Memoization

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

4.2 Variations on a Scheme -- Lazy Evaluation

Introduction to Scheme

Organization of Programming Languages CS3200/5200N. Lecture 11

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

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

Principles of Programming Languages

Lecture Notes on Lisp A Brief Introduction


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

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

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

Imperative, OO and Functional Languages A C program is

CS 314 Principles of Programming Languages. Lecture 16

Concepts of programming languages

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

Functional Programming

Scheme Quick Reference

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

CS115 - Module 9 - filter, map, and friends

CSC 533: Programming Languages. Spring 2015

Repetition Through Recursion

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

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

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

CSE 341 Section Handout #6 Cheat Sheet

Scheme Quick Reference

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

Lisp. Versions of LISP

Turtles All The Way Down

Modern Programming Languages. Lecture LISP Programming Language An Introduction

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

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

CS115 - Module 10 - General Trees

Lambda Calculus. Gunnar Gotshalks LC-1

Chapter 15. Functional Programming Languages

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

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

CSCI337 Organisation of Programming Languages LISP

61A Lecture 2. Wednesday, September 4, 2013

Func%onal Programming in Scheme and Lisp

Lecture #24: Programming Languages and Programs

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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

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

Lexical vs. Dynamic Scope

Func%onal Programming in Scheme and Lisp

CPS 506 Comparative Programming Languages. Programming Language Paradigm

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

Transcription:

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)? => 2

Nested Expressions (function expr1 expr2 ) The arguments can themselves be expressions(which must be evaluated first), so this is a more correct description. (+ 1 (sqrt4))? => 3 (* (+ 2 3) (+ 5 (-2 2)))? => 25 The function might also be the result of an expression, but you don t need to worry about that now.

Backtrack to Primitives A primitive is something that evaluates to itself. Numbers, Strings and Booleans are primitives, and you ll see more later. You know you can stop evaluating a nested expression when all you have are primitives. (primitive? 1) => true (primitive? foo ) => true (primitive? (+ 1 2)) => true (primitive? (< 1 2)) => true

List Related Expressions Listis a function that outputs its arguments as a list. (list 1 2 3) => (1 2 3) (list(+ 01) 2 (* 31)) => (1 2 3) Notice how the output of list looks just like actual code! (except that we don t yet know how to put functions in lists) (cons0 (list 1 2 3)) => (0 1 2 3)

Special Forms Not everything in scheme is a function applied to arguments. For example: if only evaluates either the consequenceor alternative (depending on if the condition is true). (if condition consequence alternative) (if (= 1 2) 3 4)? => 4 (if (= x 0) divide by zero error (/ 2 x)) Assume x equals 1? => 2 Assume x equals 0? => divide by zero error

Back to Lists (first(list 1 2 3))? => 1 (rest (list 1 2 3))? => (2 3) (null? (list 1 2 3)) => false (null? (list)) => true Is list a special form? No because all of its arguments are evaluated.

Symbols A symbolis an abstract thing that only represents itself. When you evaluate an expression, you have a dictionary of symbols like + which correspond to functions which actually mean something (like adding). Sometimes symbols have an entry in a dictionary, and sometimes they don t.

Basic Quoting Quotingan expression returns that expression, unevaluated. So if the expression is just a function, quoting returns a symbol that looks like that function. (quote expression) (quote +)? => + (quote sqrt)? => sqrt (quote blurppp)? => blurppp

More Quoting If you quote a number, it simply returns that number. (quote 2) => 2 If you quote a list, it simply returns that list, with all the list elements as symbols. (quote (a bc))? => (a bc) (quote (+ 1 2)) => (+ 1 2)

Syntactic Sugar Generally, the philosophy of scheme is to minimize syntax, but occasionally an operation is so common that it gets a shortcut notation (this is known as syntactic sugar). (quote foo) gets abbreviated to foo (quote expression) = expression (a (quote b) c (+ 1 2)) => (a b c (+ 1 2) )

Defining Functions (define(function-name name arg1 arg2 ) expression) (define (square x) (* x x)) (square 3) => 9 Is define a special form? Yes it doesn't actually evaluate anything. The function name and arguments are all just names, the expression is only evaluated when the function is called.

Lets Implement Scheme! We now know enough to start writing our own scheme interpreter (also known as an evaluator) using scheme. An interpreter takes in a quoted string (so everything in it is just a valueless symbol), and interprets it as scheme code (uses a dictionary to figure out how to evaluate an expression). We ll develop in stages, rewriting what we had before to add more capability to our interpreter.

Start Primitive (define (eval expr) (if (primitive? expr) expr Not a primitive )) (eval 5) => 5 (eval foo ) => foo (eval (1 2)) => Not a primitive

Basic Expressions (define (eval expr) (if (primitive? expr) expr (eval-func(first expr) (rest expr)))) (define (eval-func function args) (if (equal? function '+) (+ (first args) (second args)) Function not defined )) (eval-func (+ 2 1)) => 3 (eval(-2 1)) => 1 ;notice that there is no quote, so it is evaluated by scheme instead of your interpreter (eval-func (-2 1)) => Function not defined

More Syntactic Sugar We want more than one function, but nesting if s is ugly, so lets use some more of scheme s syntactic sugar: cond. (cond(condition1condition1 consequence1) (condition2 consequence2) (else default-consequence)) (cond((< x 0) 0) ((> x 1) 2) (else 1)) Assume x = -1? => 0 Assume x =.5? => 1

More Basic Expressions (define (eval-func function args) (cond(equal? function '+) (+ (first args) (second args)) (equal? function /) (/ (first args) (second args)) (equal? function <) (< (first args) (second args)) ((equal? function list) args) (else Function not defined ))) (eval (+ 3 2)) => 5 (eval (list 1 2 3)) => (1 2 3) (eval (+ 3 (+ 2 1))) =>!!Error You cannot add numbers and lists!! So we need to add nested expressions.

Recursion Recursion is the idea of functions calling themselves. It allows one to make loops. (define (mystery n) (if (= n 1) 1 {Base Case} (* n (mystery(-n 1))))) {Recursive Case} (mystery 1) => 1 (mystery 2) => 2 (mystery 3) => 6 (mystery 4) => 24 What does mystery do? => factorial

List Recursion (define(mystery lst) (if(null? lst) lst (cons(square (first lst)) (mystery(rest lst))))) {Base Case} {Recursive Case} (mystery (list)) (mystery (list)) => () (mystery (list 1)) => (1) (mystery (list 1 2)) => (1 4) (mystery (list 1 2 3)) => (1 4 9) What does mystery do? => square the elements of a list

Higher Order Functions (define (twice func arg) (func(func arg))) (square 2) => 4 (twice square 2) => 16 (twice list 1) => ((1))

Map (define (map func lst) (if (null? lst) lst (cons (func(first lst)) (mapfunc(rest lst))))) (map square (list 1)) => 1 (map square (list 1 2 3)) => (1 4 9) (map list (list 1 2 3)) => ((1) (2) (3))

Nested Expressions (define (eval expr) (if (primitive? expr) expr (eval-func(first expr) (map eval(rest expr))))) (eval 3) (eval 3) => 3 (eval (+ 3 2)) => 5 (eval (+ 3 (+ 2 1))) => 6 (eval (list (+ 1 (+ 1 1)) (list 1))) => (3 (1))

Special Forms: Quote (define (eval expr) (cond((primitive? expr) expr) ((equal? quote (first expr)) (second expr)) (else (eval-func(first expr) (map eval(rest expr))))) (eval (quote 1)) => 1 (eval (quote blurrrp)) => blurrrp (eval (quote (+ 1 2))) => (+1 2)

Special Forms: if (define (eval expr) (cond((primitive? expr) expr) ((equal? quote (first expr)) (second expr)) ((equal? if (first expr)) (eval-if (second expr) (third expr) (fourth expr))) (else (eval-func(first expr) (map eval(rest expr))))) (define (eval-if condition consequence alternative) (if (eval condition) (eval consequence) (eval alternative))) (eval-if (< 1 2) (+ 1 1) (/ 1 0)) => 2 (eval (if (< 2 1) 1 2)) => 2

Define Adding define is a bit more complicated you need to keep a dictionary of symbols to values which changes over time (as you define more things). This dictionary is called an environment, and is also passed into eval. Whenever you define something, evaladds the definition to the environment given to it. (eval expr env)

Apply The other half of define is Apply (define (eval-func function args env) (cond((primitive-function? function) (eval-primitive-func function env) ((function-defined? function) (apply func args env)) (else Function not defined )))) You would then have to write the functions primitivefunction?, function-defined? and apply.

Details There are a details which I was forced to gloss over due to time constraints, but essentially everything written here is valid code (some trivial helper methods, like primitive? and first? do need to be defined). If you are interested in learning more about scheme, see http://mitpress.mit.edu/sicp/full-text/book/book.htmland http://icampus.mit.edu/xtutor/content/?6001publichints To specifically read more about evaluators, see: http://mitpress.mit.edu/sicp/full-text/sicp/book/node75.html