Scheme Datatypes. expressions. pairs. atoms. lists. symbols. booleans

Similar documents
Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

Functional Programming

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

Functional Programming. Big Picture. Design of Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 314 Principles of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lambda Calculus. Gunnar Gotshalks LC-1

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

Lambda Calculus LC-1

FP Foundations, Scheme

Functional Java Lambda Expressions

LECTURE 16. Functional Programming

Functional Programming

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

A Brief Introduction to Scheme (I)

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

Chapter 15. Functional Programming Languages

Recursive Functions of Symbolic Expressions and Their Application, Part I

Chapter 11 :: Functional Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional Programming. Pure Functional Programming

Functional Languages. Hwansoo Han

Heap storage. Dynamic allocation and stacks are generally incompatible.

Lecture #24: Programming Languages and Programs

Functional programming in LISP

CSc 520 Principles of Programming Languages

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

Week 4: Functional Programming

CS 360 Programming Languages Interpreters

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

Introduction to lambda calculus Part 3

Class Structure. Prerequisites

Chapter 1. Fundamentals of Higher Order Programming

Binghamton University. CS-140 Fall Functional Java

Control in Sequential Languages

Scheme: Expressions & Procedures

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Imperative languages

Functional Programming Lecture 1: Introduction

Functional Programming

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

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

Functional Programming Languages (FPL)

Functional programming with Common Lisp

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

Programming Language Pragmatics

Concepts of programming languages

Principles of Programming Languages 2017W, Functional Programming

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

CSCE 314 Programming Languages

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

Introduction to Functional Programming

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

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

CSCC24 Functional Programming Scheme Part 2

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Nano-Lisp The Tutorial Handbook

Introduction. chapter Functions

LISP: LISt Processing

LECTURE 17. Expressions and Assignment

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

COMMOM OBJECT ORIENTED LISP SYSTEMS

The Design and Implementation of a Modern Lisp. Dialect

Programming Languages

15. Functional Programming

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

CS 11 Haskell track: lecture 1

Discussion 12 The MCE (solutions)

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Lambda Calculus. Variables and Functions. cs3723 1

Symbolic Computation

Section 10: LISP to Scheme. Evolution of Software Languages

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

SOFTWARE ARCHITECTURE 6. LISP

Lexical Considerations

Lambda Calculus see notes on Lambda Calculus

COP4020 Programming Assignment 1 - Spring 2011

Part II Composition of Functions

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

MIT Scheme Reference Manual

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

61A Lecture 28. Friday, November 4

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Programming Systems in Artificial Intelligence Functional Programming

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

15 Unification and Embedded Languages in Lisp

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

1 Lexical Considerations

Lexical Considerations

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

Example Scheme Function: equal

Transcription:

Lisp Lisp was invented in 1958 at MIT by John McCarthy. Lisp stands for list processing. Its intended use was research in artificial intelligence. It is based on the λ calculus which was invented by Alonzo Church in the 1930's. Many features of modern programming languages such as garbage collection, first class functions and compiling to virtual machine bytecode first appeared in Lisp.

Scheme Scheme is a dialect of Lisp invented at MIT in 1975 by Guy Steele and Gerald Sussman. It corrected some design flaws in Lisp Single name space for functions and data Lexically (t dynamically) scoped variables Optimization of tail calls No programming language matches Scheme in ratio of expressive power to implementation size.

Scheme Datatypes expressions atoms pairs lists symbols booleans numbers functions

Expressions In Scheme all values are expressions. Expressions are either pairs or n-pairs. Pairs are the building blocks of lists. Non-pairs are also called atoms. Atomic types include symbols, numbers, booleans and functions. Lists can be nested and can contain expressions of different types.

Programs Both programs and data are expressions. Programs are represented as nested lists. For example, (/ (+ (- b) (sqrt (- (* b b) (* 4 (* a c))))) (* 2 a)). Function calls in Scheme are written in prefix tation. Operators appear before operands in the list representing the function call, e.g., (+ 1 2).

Programs Expressions representing programs are evaluated by recursively evaluating subexpressions. All atomic types except symbols evaluate to themselves. Recursive evaluation bottoms out on these selfevaluating types.

eval 0.0 return x x number? (f a b)? error return apply f to eval(a) and eval(b)

Evaluating an Expression (/ (+ 7 (- 3 2)) 2) (/ (+ 7 1) 2) (/ 8 2) 4

Referential Transparency When a function is called second and subsequent times on given values, the result is always the same. Referential transparency simplifies analysis of program behavior. When a programming language is referentially transparent, code can be transformed algebraically without breaking it. These transformations can result in significant gains in efficiency and parallelism.

No Referential Transparency Consider the following C expression ((x++) * y) + (x * (y++)) Now let x = 1 and y = 2. If the left subexpression is evaluated first ((x++) * y) + (x * (y++)) 2 + (x * (y++)) 2 + 4 6 However, if the right is evaluated first ((x++) * y) + (x * (y++)) ((x++) * y) + 2 3 + 2 5 The program's behavior depends on the order of evaluation of its subexpressions!

First Class Datatypes A datatype is first class if functions can take values of that type as arguments and return values of that type as results. If a datatype is first class then new values of that type can be created at run time.

First Class Functions In Scheme, functions are first class. Functions which take functions as arguments and return functions as results are called higher-order functions. Higher-order functions can be used to abstractly represent sets of similar functions. The use of higher-order functions as a method of abstraction leads to increased program modularity.

Symbols Symbols are the only atomic type that is t self-evaluating. Symbols serve as names for other values. They are different from variables in imperative programming because the values they represent never change. Functional programming languages are sometimes called single-assignment languages because once defined, a symbol's value never changes.

Symbols with Function Values In the expression (+ 1 2), the plus sign is t the function that adds two numbers, it is a symbol. The symbol's value is the function that adds two numbers.

eval 1.0 return value(x) return x error defined? x number? function? symbol? (f a b)? error return x return apply eval(f) to eval(a) and eval(b)

Program Data Equivalence If programs and data are both expressions, how does the evaluator kw which is which? How does it kw what expressions represent programs and should be evaluated and what expressions represent data and should be left alone? Expressions which are data are quoted. For example '(+ 1 2) (+ 1 2)

eval 2.0 return value(x) x return x error defined? quote? number? function? symbol? (f a b)? error return x return x return apply eval(f) to eval(a) and eval(b)

True and False Boolean true and false are typed #t and #f. However, #t is sort of useless, because any value that is t #f is considered true in Scheme.

Special-Forms A special-form is an exception to the rmal rules of evaluation. Normally, all of a function's arguments are evaluated before the function is applied to them. Expressions can be conditionally evaluated in Scheme using the if special-form.

if special-form The if special-form evaluates its first argument. If the first argument is t #f, it evaluates and returns the value of its second argument. Otherwise, it evaluates and returns the value of its third argument. For example, (if (= 1 1) 0 1) 0

eval 3.0 return value(x) return eval(b) otherwise x return x error defined? eval(a) #f return eval(c) quote? boolean? number? function? symbol? (if a b c)? (f a b)? error return x return x return x return apply eval(f) to eval(a) and eval(b)

If ever the time should come, when vain and aspiring men shall possess the highest seats in government, our country will stand in need of its experienced patriots to prevent its ruin. Samuel Adams MASSACHUSETTS