Scheme: Expressions & Procedures

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

Functional Programming. Pure Functional Languages

Scheme: Strings Scheme: I/O

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

Functional Programming

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

COP4020 Programming Assignment 1 - Spring 2011

Functional Programming Lecture 1: Introduction

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

Functional programming in LISP

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

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

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

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

Functional Programming. Big Picture. Design of Programming Languages

CSc 520 Principles of Programming Languages

CSC 533: Programming Languages. Spring 2015

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

Thoughts on Assignment 4 Haskell: Flow of Control

The Typed Racket Guide

Introduction to Scheme

Introduction to Functional Programming

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

Typed Racket: Racket with Static Types

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

Documentation for LISP in BASIC

Functional Programming. Pure Functional Programming

User-defined Functions. Conditional Expressions in Scheme

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Scheme Quick Reference

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

Writing an Interpreter Thoughts on Assignment 6

LECTURE 16. Functional Programming

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Lisp. Versions of LISP

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

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

Functional Programming Languages (FPL)

Scheme Quick Reference

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

Welcome to CS 135 (Winter 2018)

Chapter 15 Functional Programming Languages

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

FP Foundations, Scheme

6.001: Structure and Interpretation of Computer Programs

Principles of Programming Languages 2017W, Functional Programming

An Introduction to Scheme

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Concepts of programming languages

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

A Brief Introduction to Scheme (II)

Project 2: Scheme Interpreter

Welcome to CS 135 (Fall 2018) Themes of the course. Lectures. cs135/

The SPL Programming Language Reference Manual

JME Language Reference Manual

A Small Interpreted Language

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

The PCAT Programming Language Reference Manual

Class Structure. Prerequisites

SOFTWARE ARCHITECTURE 6. LISP

CS 314 Principles of Programming Languages

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

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

Programming Languages

Honu. Version November 6, 2010

Reasoning About Programs Panagiotis Manolios

15 Unification and Embedded Languages in Lisp

CS 314 Principles of Programming Languages. Lecture 16

Introduction to lambda calculus Part 3

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CSCI337 Organisation of Programming Languages LISP

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

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

CSE 341 Section Handout #6 Cheat Sheet

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

Lecture #24: Programming Languages and Programs

Evaluating Scheme Expressions

Introduction to Syntax Analysis Recursive-Descent Parsing

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

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

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

1 Lexical Considerations

Full file at

Typescript on LLVM Language Reference Manual

CSE 341, Autumn 2015, Ruby Introduction Summary

CS 3360 Design and Implementation of Programming Languages. Exam 1

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


Functional programming with Common Lisp

Example Scheme Function: equal

CS 360 Programming Languages Interpreters

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

Chapter 15. Functional Programming Languages

Symbolic Computation

Programming Languages

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

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Transcription:

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 of Alaska Fairbanks ggchappell@alaska.edu 2017 Glenn G. Chappell

Review 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 2

PL Categories: Lisp-Family PLs Background [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)) 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 3

PL Categories: Lisp-Family PLs Background [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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 4

PL Categories: Lisp-Family PLs Typical Characteristics [1/2] Lisp-family PLs typically have the following characteristics. Simple syntax based on the S-expression. A program is a list of lists. The first item of a list is a function; the rest are its arguments. For example, the C/C++/Java expression (a + 2) * -b would be written as follows in a typical Lisp-family PL: (* (+ a 2) (- b)) Where have we seen this before? Tweak the notation a bit: replace parentheses with braces, separate list items by commas, and place atoms in double quotes. Result: {"*", {"+", "a", "2"}, {"-", "b"}} Where have we seen this before? This was our first stab at an AST representation in Lua. Thus, Lisp source code is a direct representation of its own AST! Source-code syntax and storage format is that of the PLs primary data structures. Types can be checked, and code can be modified & executed at runtime. Support for reflection is excellent. Typical programming styles involve macros: transformations applied to code at runtime. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 5

PL Categories: Lisp-Family PLs Typical Characteristics [2/2] Typical characteristics of Lisp-family PLs (cont d): Typing is dynamic, implicit, and structural (duck typing). There is very good support for functional programming: first-class functions, higher-order functions, etc. But mutable data is allowed. The PL is extensible. Execution can be either interactive (REPL) or via previously compiled code. Accomplished Lisp programmers tend to be insufferably fond of Lisp. ( We can already do that in Lisp. <smirk> ) But perhaps they re onto something. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 6

Introduction to Scheme History [1/2] As with many PLs, the early history of Lisp was one of everincreasing complexity. As a result, the Common Lisp standard is huge, including exceptions, an object system, etc. Perhaps as a reaction to this, a Lisp-family PL called Scheme was created at the MIT AI Lab around 1970, by Guy Steele and Gerald Sussman. In contrast to Common Lisp, Scheme follows a minimalist design philosophy, with a small, simple core and versatile tools for extending the PL. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 7

Introduction to Scheme History [2/2] Scheme follows somewhat different conventions from traditional Lisp for the evaluation of functions. Thus, while some say Scheme is a dialect of Lisp, others emphatically deny this. But Scheme clearly belongs in the Lisp family of PLs. Scheme has been standardized in a series of standards documents. The most recent (R7RS) was released in 2013. A version of Scheme called PLT Scheme (named for the Rice University Programming Languages Team) was first released in 1994. This was renamed as Racket in 2010. Distributed with Racket is a simple IDE called DrRacket, which runs on all major platforms. This is the Scheme implementation we will be using. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 8

Introduction to Scheme Characteristics Introduction 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 9

Introduction to Scheme Characteristics Type System [1/2] 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 10

Introduction to Scheme Characteristics Type System [2/2] 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 11

Introduction to Scheme Characteristics Flow of Control 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") ) 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 12

Introduction to Scheme Characteristics Miscellaneous As with Lua, Scheme local variables are lexically scoped. Scheme globals have dynamic scope. Scheme has very good support for functional programming. It is not a pure functional PL; it does allow for mutable data. Like all Lisp-family PLs, the syntax and storage format of code is the same as that of the language s primary data structure. Thus, it is natural to manipulate code at runtime. Such code can be executed as part of the same runtime. Thus, Scheme has excellent support for reflection. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 13

Introduction to Scheme Build & Execution The standard filename suffix for Scheme source files is.scm. Scheme allows for interactive execution or compiled executables. We will not be doing the latter. We will execute Scheme using an IDE called DrRacket. The upper part of the DrRacket window is a source-code editor, with the usual open-save interface. The first line of the code in this window should always be as follows: #lang scheme The lower part of the Window is a REPL. Type in Scheme code to execute. Pressing the Run button executes all code in the upper window. Thereafter, symbols defined in that code may be used in the REPL. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 14

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.") 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 15

Scheme: Expressions & Procedures Expressions Evaluation [1/2] 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 16

Scheme: Expressions & Procedures Expressions Evaluation [2/2] 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 > (+ 5 3 4 8 1 2 6) 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 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 17

Scheme: Expressions & Procedures Expressions Operators [1/2] 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 18

Scheme: Expressions & Procedures Expressions Operators [2/2] 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. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 19

Scheme: Expressions & Procedures Lists [1/2] 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 4 2 7)) 5 > (cdr '(5 4 2 7)) (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)) (5 4 2 7) 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 20

Scheme: Expressions & Procedures Lists [2/2] It is common to use combinations of car & cdr. For example, (car (cdr x)) returns the second item of list x. > (car (cdr '(5 4 2 7))) ; Second item 4 > (car (cdr (cdr '(5 4 2 7)) ; Third item 2 All such combinations, up to 5 car/cdr applications, are implemented as predefined functions. > (cadr '(5 4 2 7)) ; Second item 4 > (caddr '(5 4 2 7)) ; Third item 2 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 21

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 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 22

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

Scheme: Expressions & Procedures Defining Procedures [1/3] 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 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 24

Scheme: Expressions & Procedures Defining Procedures [2/3] 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" 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 25

Scheme: Expressions & Procedures Defining Procedures [3/3] A recursive call is done by using the word being defined inside its body. And now we have the tools to write all kinds of things See proc.scm. 31 Mar 2017 CS F331 / CSCE A331 Spring 2017 26