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

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

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

Transcription

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

2 PL Features: Reflection Reflection in a computer program refers to the ability of the program to deal with its own code at runtime: examining the code, looking at its properties, and modifying it. An important property of a PL is whether, and how well, it supports reflection. The Lisp-family PLs are distinguished by their excellent support for reflection. 3 Apr 2017 CS F331 / CSCE A331 Spring

3 PL Categories: Lisp-Family PLs [1/2] In 1958, MIT professor John McCarthy published a mathematical formalism for describing computation. This formalism was written in various was; the most common notation was the Symbolic Expression, or S-expression. An S-expression is either an atom (basically a word), a pair (two S-expressions separated by a dot and enclosed in parentheses), or nil (an empty pair of parentheses). (THIS. (IS. (AN. ((S. (EXPRESSION. ())). ())))) A shorter form uses a parenthesized list of space-separated items. Something like (A. (B. (C. ()))) is written as (A B C). (THIS IS AN (S EXPRESSION)) 3 Apr 2017 CS F331 / CSCE A331 Spring

4 PL Categories: Lisp-Family PLs [2/2] An implementation of the evaluation procedure of this formalism, initially written by Dartmouth student Steve Russell, became the programming language Lisp (LISt Processor). S-expressions are the syntax for both code and data in Lisp. The C/C++/Java expression (a + 2) * -b would be written as follows in a typical Lisp-family PL: (* (+ a 2) (- b)) Lisp source code is a direct representation of its own AST! 3 Apr 2017 CS F331 / CSCE A331 Spring

5 Introduction to Scheme Characteristics [1/4] Scheme is a Lisp-family PL with a minimalist design philosophy. Scheme code consists of parenthesized lists, which may contain atoms or other lists. List items are separated by space; blanks and newlines between list items are treated the same. (define (hello-world) (begin (display "Hello, world!") (newline) ) ) When a list is evaluated, the first item should be a procedure (think function ); the remaining items are its arguments. 3 Apr 2017 CS F331 / CSCE A331 Spring

6 Introduction to Scheme Characteristics [2/4] The type system of Scheme is similar to that of Lua. Typing is dynamic. Typing is implicit. Type annotations are generally not used. Type checking is structural. Duck typing is used. There is a high level of type safety: operations on invalid types are not allowed, and implicit type conversions are rare. There is a fixed set of types. Lua s fixed set of types includes only 8 types, while Scheme has 36. We look at some of these next. 3 Apr 2017 CS F331 / CSCE A331 Spring

7 Introduction to Scheme Characteristics [3/4] Two heavily used types are pair and null, which are mostly used to construct lists. Values of all other types are atoms. Here are a few of these: Booleans. Values are #t (true) and #f (false). Strings. Enclosed in double quotes: "This is a string." Characters. For example, here is the 'a' character: #\a Symbols. A symbol is an identifier: abc x a-long-symbol Number types. There are seven of these, including arbitrarily large integers (like Haskell s Integer), floating-point numbers, exact rational numbers, and complex numbers. Procedure types. A procedure is what we would call a first-class function. A procedure may be bound to a name (a symbol), or it may be unnamed. There are actually six procedure types, but we will not need to distinguish between these. 3 Apr 2017 CS F331 / CSCE A331 Spring

8 Introduction to Scheme Characteristics [4/4] Scheme has no special syntax for flow of control. Instead, flow-ofcontrol constructs are procedures. Here is some Lua code and more or less equivalent Scheme code. if x == 3 then -- Lua io.write("three") else io.write("other") end (if (= x 3) ; Scheme "if" is a procedure (display "three") (display "other") ) 3 Apr 2017 CS F331 / CSCE A331 Spring

9 Scheme: Expressions & Procedures General Syntax We have seen what Scheme code looks like: parenthesized lists of lists, with items separated by space. Many special characters are legal in symbols. In addition: Strings are surrounded by double quotes. A leading single quote suppresses evaluation. Scheme has three kinds of comments. A semicolon (;) begins a single-line comment. Multiline comments: # # Comment out a single expression: #;EXPR #;(This code is commented (out)) (display "But this is not.") 3 Apr 2017 CS F331 / CSCE A331 Spring

10 Scheme: Expressions & Procedures Expressions [1/4] Like Haskell, Scheme code consists largely of expressions. An expression can be: An atom. A list whose first item evaluates to a procedure. To evaluate a list, Scheme begins by evaluating its first item. This should result in a procedure. Normally, the remaining arguments are then evaluated. The results of these evaluations are passed to the procedure as its arguments. For some special procedures, the remaining arguments are not evaluated. 3 Apr 2017 CS F331 / CSCE A331 Spring

11 Scheme: Expressions & Procedures Expressions [2/4] For example, + is a symbol. It evaluates to a procedure that takes zero or more numeric parameters and returns their sum. Informally, we say that + is a procedure. > (+ 2 7) 9 > ( ) 29 Some procedures can take a varying number of parameters. Symbols -, *, and / are similar. > (* (+ 3 5) (- 7 3) ; Like (3 + 5) * (7 3) 32 > (* 2 (/ 15 3)) 10 3 Apr 2017 CS F331 / CSCE A331 Spring

12 Scheme: Expressions & Procedures Expressions [3/4] Scheme does not distinguish between operators and other symbols. Nothing is infix. Scheme uses +, -, *, and / for the basic arithmetic operations. But the division operator might not do what you expect. > (/ 4 2) 2 > (/ 4 6) 2/3 > (/ 2/3 4) 1/6 > (+ 1/5 0.7) 0.9 An exact rational number A real (floating-point) number, which, as usual with floating-point, is inexact. Implicit type conversions: integer rational real complex. 3 Apr 2017 CS F331 / CSCE A331 Spring

13 Scheme: Expressions & Procedures Expressions [4/4] The numeric comparison operators: = < <= > >= There is no inequality operator! Logical operations: and or not > (= 1 2) #f > (not (= 1 2)) #t > (and (> 4 1) (<= 5 2)) #f There are several different kinds of equality in Scheme. Use the above comparison operators only with numbers. 3 Apr 2017 CS F331 / CSCE A331 Spring

14 Scheme: Expressions & Procedures Lists Two heavily used procedures are car and cdr. Each takes a pair. car returns the first item of the pair. cdr returns the second. Thus, for a nonempty list, car returns the first item, while cdr returns a list of the remaining items. > (car '( )) 5 > (cdr '( )) (4 2 7) A single quote suppresses evaluation. We do not want to treat 5 as a procedure, passing 4, 2, 7 as its arguments. We want the list. cons constructs a pair. We can use it to construct a list from an item and a list, like : in Haskell. > (cons 5 '(4 2 7)) ( ) 3 Apr 2017 CS F331 / CSCE A331 Spring

15 Scheme: Expressions & Procedures Predicates Recall: a predicate is a function returning a boolean. It answers a yes/no question about its argument(s). It is traditional for the name of a Scheme predicate to end in a question mark. Here are some type-checking predicates. Each takes a single parameter, which can be of any type. number? Returns true (#t) if its argument is a number, otherwise false (#f). null? Returns true if its argument is null (an empty list). pair? Returns true if its argument is a pair. Thus, if the argument is a list, then it returns true if the list is nonempty. If neither null? nor pair? returns true for a value, then the value is an atom. > (number? 3) #t > (number? +) #f 3 Apr 2017 CS F331 / CSCE A331 Spring

16 Scheme: Expressions & Procedures Binding Globally bind a symbol to a value with define. > (define abc (+ 5 3)) > abc 8 > (* abc (- abc 5)) 24 > (define xyz +) > (xyz 3 4) 7 3 Apr 2017 CS F331 / CSCE A331 Spring

17 Scheme: Expressions & Procedures Defining Procedures [1/2] We can also define new procedures with define. The first argument is a list that is essentially a picture of a call to our new procedure. The second argument is an expression giving the code for the procedure; this code is not evaluated until the procedure is called. Parameters are bound locally. > (define (sqr x) (* x x)) > (sqr 6) 36 > (define (not= a b) (not (= a b))) > (not= 1 3) #t > (not= (+ 1 2) 3) #f 3 Apr 2017 CS F331 / CSCE A331 Spring

18 Scheme: Expressions & Procedures Defining Procedures [2/2] if is a three-parameter procedure. (if COND THEN-EXPR ELSE-EXPR) The above evaluates COND. If this evaluates to anything other than #f, then it evaluates THEN-EXPR and returns the result; otherwise, it evaluates ELSE-EXPR and returns the result. > (if (= 3 3) "yes" "NO") "yes" A recursive call is done by using the word being defined inside its body. For code, see proc.scm. 3 Apr 2017 CS F331 / CSCE A331 Spring

19 Scheme: Data quote, eval, & More Code quote quote is a special procedure that takes one parameter, suppressing the normal parameter evaluation. It returns this parameter. > (quote (1 2 3)) (1 2 3) The leading-single-quote syntax is actually shorthand for quote. > '(1 2 3) ; Same as (quote (1 2 3)) (1 2 3) 3 Apr 2017 CS F331 / CSCE A331 Spring

20 Scheme: Data quote, eval, & More Code eval eval is a procedure that takes one parameter and evaluates it. eval does not suppress the normal evaluation of parameters, so, strictly speaking, evaluation happens twice: the parameter is evaluated, and then it evaluates the result. list is a procedure that takes any number of parameters, evaluating them as usual, and returns a list holding them. > (list "hello" ' )) ("hello" ) > (eval (cdr (list "hello" ' ))) 6 3 Apr 2017 CS F331 / CSCE A331 Spring

21 Scheme: Data quote, eval, & More Code More Code [1/3] Scheme has map, which is much like Haskell s map. > (define (sqr n) (* n n) > (map sqr '( )) ( ) Similarly: filter. > (define (big n) (> n 20) > (filter big '( )) ( ) Let s write our own versions of these. 3 Apr 2017 CS F331 / CSCE A331 Spring

22 Scheme: Data quote, eval, & More Code More Code [2/3] A useful function is cond, which does if elseif. (cond ) [CONDITION1 EXPR1] [CONDITION2 EXPR2] [CONDITION3 EXPR3] The return value is the value of the expression corresponding to the first true condition or nothing if no condition is true. The last condition may be else, which makes it handle all remaining cases. 3 Apr 2017 CS F331 / CSCE A331 Spring

23 Scheme: Data quote, eval, & More Code More Code [3/3] TO DO Write our own version of map; call it mymap. Similarly, myfilter. Done. See data.scm. Q. We need to be able to return an empty list. How? A. Use '(). Or null, which evaluates to an empty list. 3 Apr 2017 CS F331 / CSCE A331 Spring

24 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. 3 Apr 2017 CS F331 / CSCE A331 Spring

25 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 3 Apr 2017 CS F331 / CSCE A331 Spring

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

27 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 3 Apr 2017 CS F331 / CSCE A331 Spring

28 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 3 Apr 2017 CS F331 / CSCE A331 Spring

29 Scheme: Data Taking a Varying Number of Parameters [1/3] We have seen how to create new procedures using define. So far, all of our procedures have taken a fixed number of parameters. But Scheme allows for procedures like +, which can take any number of parameters. Suppose we wish to duplicate +, in the form of a function called sum. We will use +, but only as a 2-parameter function. Think about a call to sum: Procedure Call > (sum ) 10 PROC ARGS The above list (sum ) is the same as (sum. ( )). A procedure call is a pair. The car is the procedure; the cdr is a list of the procedure s arguments (illustrated to the right above). 3 Apr 2017 CS F331 / CSCE A331 Spring

30 Scheme: Data Taking a Varying Number of Parameters [2/3] 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) )... TO DO Write procedure sum. Done. See data.scm. A tricky issue is how to make a recursive call on (cdr args). We cover this on the next slide. 3 Apr 2017 CS F331 / CSCE A331 Spring

31 Scheme: Data Taking a Varying Number of Parameters [3/3] In writing function sum, we need to make a recursive call on (cdr args). How do we do this? NOT like this (dot is not a function!): (sum. (cdr args)) ; WRONG! (sum. (cdr args)) is just another way to write (sum cdr args), which is not what we want. The following will work, but it is a bit unwieldy: (eval (cons sum (cdr args))) Scheme provides apply for this situation. Here is how we do it: (apply sum (cdr args)) 3 Apr 2017 CS F331 / CSCE A331 Spring

32 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. TO DO Write a procedure atomsum that is given a tree t and returns the sum of all the atoms in t. Write a procedure atommap that is given a function f and a tree t and returns a tree that is t with each atom replaced by the value of f at that atom. Done. See data.scm. A useful procedure is error, which, much like Haskell s error, takes a string, and crashes with a message if it executes. 3 Apr 2017 CS F331 / CSCE A331 Spring

Scheme: Strings Scheme: I/O

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

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

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

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

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

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

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

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

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

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

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

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp. Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

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

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

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

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

More information

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires 1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

Structure and Interpretation of Computer Programs

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

More information

ALISP interpreter in Awk

ALISP interpreter in Awk ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.

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

A LISP Interpreter in ML

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

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Pointers (continued), arrays and strings

Pointers (continued), arrays and strings Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

More information

Structure and Interpretation of Computer Programs

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

More information

Working with recursion

Working with recursion Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2). We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in

More information

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

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

More information

CSc 520. Principles of Programming Languages 25: Types Introduction

CSc 520. Principles of Programming Languages 25: Types Introduction CSc 520 Principles of Programming Languages 25: Types Introduction Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April

More information

Lecture Notes on Lisp A Brief Introduction

Lecture Notes on Lisp A Brief Introduction Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

SMURF Language Reference Manual Serial MUsic Represented as Functions SMURF Language Reference Manual Serial MUsic Represented as Functions Richard Townsend, Lianne Lairmore, Lindsay Neubauer, Van Bui, Kuangya Zhai {rt2515, lel2143, lan2135, vb2363, kz2219}@columbia.edu

More information

Test Requirement Catalog. Generic Clues, Developer Version

Test Requirement Catalog. Generic Clues, Developer Version Version 4..0 PART 1: DATA TYPES Test Requirement Catalog SIMPLE DATA TYPES... 4 BOOLEAN... 4 CASES... 4 COUNTS... 5 INTERVALS... 5 [LOW, HIGH] an interval that contains both LOW and HIGH... 6 [LOW, HIGH)

More information

Warm-up and Memoization

Warm-up and Memoization CSE341 Spring 05 Due Wednesday, May 11 Assignment 4 Part I Warm-up and Memoization Warm-up As a warm-up, write the following Scheme functions: 1. Write the function foldl of the form (foldl func initial

More information

6.001 Notes: Section 31.1

6.001 Notes: Section 31.1 6.001 Notes: Section 31.1 Slide 31.1.1 In previous lectures we have seen a number of important themes, which relate to designing code for complex systems. One was the idea of proof by induction, meaning

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 Exercise 1. Consider the if-then-else construct of Pascal, as in the following example: IF 1 = 2 THEN PRINT X ELSE PRINT Y (a) Assume

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Project 5 - The Meta-Circular Evaluator

Project 5 - The Meta-Circular Evaluator MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 2005 Project 5 - The Meta-Circular

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

Problem Set 4: Streams and Lazy Evaluation

Problem Set 4: Streams and Lazy Evaluation Due Friday, March 24 Computer Science (1)21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Problem Set 4: Streams and Lazy Evaluation Reading Assignment: Chapter 3, Section 3.5.

More information

Recursive Functions of Symbolic Expressions and Their Application, Part I

Recursive Functions of Symbolic Expressions and Their Application, Part I Recursive Functions of Symbolic Expressions and Their Application, Part I JOHN MCCARTHY Review: Amit Kirschenbaum Seminar in Programming Languages Recursive Functions of Symbolic Expressions and Their

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used) The design recipe Readings: HtDP, sections 1-5 (ordering of topics is different in lectures, different examples will be used) Survival and Style Guides CS 135 Winter 2018 02: The design recipe 1 Programs

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016 CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016 In this course you will start by building a compiler for a language called Xi. This is an imperative,

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

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

More information

MetacircularScheme! (a variant of lisp)

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

More information

Common Lisp. Blake McBride

Common Lisp. Blake McBride Contents Common Lisp Blake McBride (blake@mcbride.name) 1 Data Types 2 2 Numeric Hierarchy 3 3 Comments 3 4 List Operations 4 5 Evaluation and Quotes 5 6 String Operations 5 7 Predicates 6 8 Math Predicates

More information

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics CONVENTIONAL EXECUTABLE SEMANTICS Grigore Rosu CS422 Programming Language Semantics Conventional Semantic Approaches A language designer should understand the existing design approaches, techniques and

More information

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College CS5 Programming Languages Handout # 9 Prof. Lyn Turbak March, 00 Wellesley College Postfix: A Simple Stack Language Several exercises and examples in this course will involve the Postfix mini-language.

More information

Welcome to CS 115 (Winter 2018)

Welcome to CS 115 (Winter 2018) Welcome to CS 115 (Winter 2018) Web page (the main information source): http://www.student.cs.uwaterloo.ca/ cs115/ Course Personnel: Contact information and office hours for all staff: instructors, ISAs

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

Lecture #2 Kenneth W. Flynn RPI CS

Lecture #2 Kenneth W. Flynn RPI CS Outline Programming in Lisp Lecture #2 Kenneth W. Flynn RPI CS Items from last time Recursion, briefly How to run Lisp I/O, Variables and other miscellany Lists Arrays Other data structures Jin Li lij3@rpi.edu

More information

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

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

More information

A Brief Introduction to Scheme (I)

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

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer April 20, 2015 OOPP / C++ Lecture 3... 1/23 New Things in C++ Object vs. Pointer to Object Optional Parameters Enumerations Using an enum

More information

FRAC: Language Reference Manual

FRAC: Language Reference Manual FRAC: Language Reference Manual Justin Chiang jc4127 Kunal Kamath kak2211 Calvin Li ctl2124 Anne Zhang az2350 1. Introduction FRAC is a domain-specific programming language that enables the programmer

More information

THE CALCULATOR SYNTAX

THE CALCULATOR SYNTAX TH CALCULATOR After looking at the basics of the denotational method with the binary numerals we will take chmidt s example of a calculator to bring us one step closer to looking at the semantics of a

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

CMSC 331 Final Exam Section 0201 December 18, 2000

CMSC 331 Final Exam Section 0201 December 18, 2000 CMSC 331 Final Exam Section 0201 December 18, 2000 Name: nswers Student ID#: You will have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points

More information

Problems and Solutions to the January 1994 Core Examination

Problems and Solutions to the January 1994 Core Examination Problems and Solutions to the January 1994 Core Examination Programming Languages PL 1. Consider the following program written in an Algol-like language: begin integer i,j; integer array A[0..10]; Procedure

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM EXAMINATION - CS130 - Spring 2005 MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

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

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

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

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

The Design and Implementation of a Modern Lisp. Dialect

The Design and Implementation of a Modern Lisp. Dialect The Design and Implementation of a Modern Lisp Dialect Sam Davis Nicholas Alexander January 26, 2006 Abstract Lisp, invented in 1958 by John McCarthy, revolutionized how programs could be written and expressed.

More information

Programming with Math and Logic

Programming with Math and Logic .. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

More information

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees The Pros of cons: Programming with CS251 Programming Languages Fall 2017, Lyn Turbak Department of Computer Science Wellesley College booleans: #t, #f numbers: integers: 42, 0, -273 ra3onals: 2/3, -251/17

More information

A Short Summary of Javali

A Short Summary of Javali A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts

More information

Decaf Language Reference

Decaf Language Reference Decaf Language Reference Mike Lam, James Madison University Fall 2016 1 Introduction Decaf is an imperative language similar to Java or C, but is greatly simplified compared to those languages. It will

More information

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROgramming in LOGic PROLOG Recursion, Lists & Predicates PROgramming in LOGic PROLOG Recursion, Lists & Predicates CSC9Y4 1 Recursion Recursion in Prolog means placing in the body of a rule a call to the predicate which occurs in the head of the rule. Here is

More information

Racket Style Guide Fall 2017

Racket Style Guide Fall 2017 CS17 Integrated Introduction to Computer Science Hughes Racket Style Guide Fall 2017 Contents 1 Introduction 1 2 Naming 1 3 Formatting 1 4 Equality 3 5 Conditionals 4 5.1 Prefer Cond to If......................................

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Graphs. Directed graphs. Readings: Section 28

Graphs. Directed graphs. Readings: Section 28 Graphs Readings: Section 28 CS 135 Winter 2018 12: Graphs 1 Directed graphs A directed graph consists of a collection of vertices (also called nodes) together with a collection of edges. An edge is an

More information

Twister: Language Reference Manual

Twister: Language Reference Manual Twister: Language Reference Manual Manager: Anand Sundaram (as5209) Language Guru: Arushi Gupta (ag3309) System Architect: Annalise Mariottini (aim2120) Tester: Chuan Tian (ct2698) February 23, 2017 Contents

More information

Data Types The ML Type System

Data Types The ML Type System 7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Functional Programming in Haskell Part I : Basics

Functional Programming in Haskell Part I : Basics Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian

More information

Lecture Notes for CS 150 Fall 2009; Version 0.5

Lecture Notes for CS 150 Fall 2009; Version 0.5 for CS 150 Fall 2009; Version 0.5 Draft! Do not distribute without prior permission. Copyright 2001-2009 by Mark Holliday Comments, corrections, and other feedback appreciated holliday@email.wcu.edu Chapter

More information

Data types for mcrl2

Data types for mcrl2 Data types for mcrl2 Aad Mathijssen April 5, 2018 We provide a syntax for the standard data types of the mcrl2 language. This syntax is intended to be a practical mix between standard mathematical notation

More information

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

More information

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work? STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a cons-stream a special form? Practice with Streams 1. Define a procedure (ones) that, when run

More information

Project 5 - The Meta-Circular Evaluator

Project 5 - The Meta-Circular Evaluator MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 2005 Project 5 - The Meta-Circular

More information

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1 Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind

More information