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

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

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

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

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

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

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

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

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 3: Scheme Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

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

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

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

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

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

Functional programming in LISP

Functional programming in LISP Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of

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

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

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

Writing an Interpreter Thoughts on Assignment 6

Writing an Interpreter Thoughts on Assignment 6 Writing an Interpreter Thoughts on Assignment 6 CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, March 27, 2017 Glenn G. Chappell Department of Computer Science

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

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

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

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

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

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

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

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

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

A Small Interpreted Language

A Small Interpreted Language A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

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

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

15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp 15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

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

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

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

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

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

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

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

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

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

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

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

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

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create

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

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

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

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

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

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

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

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

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

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

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions PL Categories: Functional PLs Introduction to Haskell Haskell: Functions CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, February 22, 2017 Glenn G. Chappell

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

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

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

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

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

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

Chapter 15 Functional Programming Languages

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

More information

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia

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

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

ECE473 Lecture 2: Types and Conventions

ECE473 Lecture 2: Types and Conventions ECE473 Lecture 2: Types and Conventions Jeffrey Mark Siskind School of Electrical and Computer Engineering Spring 2019 Siskind (Purdue ECE) ECE473 Lecture 2: Types and Conventions Spring 2019 1 / 28 Booleans

More information

Repetition Through Recursion

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

More information

C311 Lab #3 Representation Independence: Representation Independent Interpreters

C311 Lab #3 Representation Independence: Representation Independent Interpreters C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction

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

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

4/19/2018. Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

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

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

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

Functional programming with Common Lisp

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

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

Introduction to lambda calculus Part 3

Introduction to lambda calculus Part 3 Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that

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

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

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

CSC 533: Programming Languages. Spring 2015

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

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

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

Lecture #24: Programming Languages and Programs

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

More information

ECE570 Lecture 2: Types and Conventions

ECE570 Lecture 2: Types and Conventions ECE570 Lecture 2: Types and Conventions Jeffrey Mark Siskind School of Electrical and Computer Engineering Fall 2017 Siskind (Purdue ECE) ECE570 Lecture 2: Types and Conventions Fall 2017 1 / 28 Booleans

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

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona 1/26 CSc 372 Comparative Programming Languages 36 : Scheme Conditional Expressions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/26 Comparison

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

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 7 Wouter Swierstra 1 Last time Relating evaluation and types How to handle variable binding in embedded languages? 2 DSLs: approaches A stand-alone DSL typically

More information

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 3 September 5, 2018 Value-Oriented Programming (continued) Lists and Recursion CIS 120 Announcements Homework 1: OCaml Finger Exercises Due: Tuesday

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