CSc 520 Principles of Programming Languages

Similar documents
CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

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

Functional Programming. Pure Functional Languages

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

CSc 520 Principles of Programming Languages

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

Functional Programming. Pure Functional Programming

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

Scheme: Expressions & Procedures

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

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

CSc 520 Principles of Programming Languages

Functional Programming

SCHEME AND CALCULATOR 5b

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

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming. Big Picture. Design of Programming Languages

FP Foundations, Scheme

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

CSE 341 Section Handout #6 Cheat Sheet

Project 2: Scheme Interpreter

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

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

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Chapter 15. Functional Programming Languages

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

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Chapter 15 Functional Programming Languages

Lisp. Versions of LISP

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Documentation for LISP in BASIC

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Scheme Quick Reference

User-defined Functions. Conditional Expressions in Scheme

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

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

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

6.001: Structure and Interpretation of Computer Programs

Functional programming in LISP

Programming Languages

Functional Programming Languages (FPL)

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Scheme Quick Reference

Imperative, OO and Functional Languages A C program is

A Small Interpreted Language

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

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

Lambda Calculus. Gunnar Gotshalks LC-1

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

MetacircularScheme! (a variant of lisp)

Repetition Through Recursion

LECTURE 17. Expressions and Assignment

Principles of Programming Languages 2017W, Functional Programming

Introduction to Functional Programming

Example Scheme Function: equal

Lecture Notes on Lisp A Brief Introduction

Principles of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS 314 Principles of Programming Languages. Lecture 16

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

Functional programming with Common Lisp

Administrivia. Simple data types

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

ALISP interpreter in Awk

LISP Programming. (23 (this is easy) hello 821)

Typed Racket: Racket with Static Types

CS 61A Discussion 8: Scheme. March 23, 2017

LECTURE 16. Functional Programming

CS 314 Principles of Programming Languages

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Introduction to Scheme

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

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

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

COP4020 Programming Assignment 1 - Spring 2011

Java Bytecode (binary file)

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

Introduction to lambda calculus Part 3

Class Structure. Prerequisites

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Functional Programming

CS 360 Programming Languages Interpreters

Lambda Calculus LC-1

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

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

Introduction to Functional Programming and basic Lisp

Common LISP-Introduction

CSc 520. Principles of Programming Languages 11: Haskell Basics

CS1 Lecture 3 Jan. 18, 2019

Functional Languages. Hwansoo Han

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Transcription:

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]

Background Scheme is based on LISP which was developed by John McCarthy in the mid 50s. LISP stands for LISt Processing, not Lots of Irritating Silly Parentheses. Functions and data share the same representation: S-Expressions. A basic LISP implementation needs six functions cons, car, cdr, equal, atom, cond. Scheme was developed by Sussman and Steele in 1975. [2]

S-Expressions An S-Expression is a balanced list of parentheses. More formally, an S-expression is 1. a literal (i.e., number, boolean, symbol, character, string, or empty list). 2. a list of s-expressions. Literals are sometimes called atoms. [3]

S-Expressions Examples Legal 66 () (4 5) ((5)) (()()) ((4 5) (6 (7))) Illegal ( (5)) ()() (4 (5) )( [4]

S-Expressions as Trees An S-expression can be seen as a linear representation of tree-structure: 2 2 (6) (3 4) 6 3 4 (2 (3 4) (5 (6) 7)) 2 3 4 5 7 6 [5]

S-Expressions as Function Calls A special case of an S-expression is when the first element of a list is a function name. Such an expression can be evaluated. > (+ 4 5) 9 > (add-five-to-my-argument 20) 25 > (draw-a-circle 20 45) #t [6]

S-Expressions as Functions As we will see, function definitions are also S-expressions: ( d e f i n e ( farenheit 2 celsius f ) ( ( f 3 2 ) 5 / 9 ) ) So, Scheme really only has one syntactic structure, the S-expression, and that is used as a data-structure (to represent lists, trees, etc), as function definitions, and as function calls. [7]

Function Application In general, a function application is written like this: (operator arg 1 arg 2... arg n ) The evaluation proceeds as follows: 1. Evaluate operator. The result should be a function F. 2. Evaluate arg 1, arg 2,... arg n to get val 1, val 2,... val n 3. Apply F to val 1, val 2,... val n. [8]

Function Application Examples > (+ 4 5) 9 > (+ (+ 5 6) 3) 14 > 7 7 > (4 5 6) eval: 4 is not a function > #t #t [9]

Atoms Numbers Scheme has Fractions (5/9) Integers (5435) Complex numbers (5+2i) Inexact reals (#i3.14159265) > (+ 5 4) 9 > (+ (* 5 4) 3) 23 > (+ 5/9 4/6) 1.2 > 5/9 0.5 [10]

Atoms Numbers... > (+ 5/9 8/18) 1 > 5+2i 5+2i > (+ 5+2i 3-i) 8+1i > (* 236542164521634 3746573426573425643) 886222587860913289285513763860662 > pi #i3.141592653589793 > e #i2.718281828459045 > (* 2 pi) #i6.283185307179586 [11]

Atoms Numbers... Scheme tries to do arithmetic exactly, as much as possible. Any computations that depend on an inexact value becomes inexact. Scheme has many builtin mathematical functions: > (sqrt 16) 4 > (sqrt 2) #i1.4142135623730951 > (sin 45) #i0.8509035245341184 > (sin (/ pi 2)) #i1.0 [12]

Atoms Strings A string is enclosed in double quotes. > (display "hello") hello > "hello" "hello" > (string-length "hello") 5 > (string-append "hello" " " "world!") "hello world!" [13]

Atoms Booleans true is written #t. false is written #f. > #t true > #f false > (display #t) #t > (not #t) false [14]

Identifiers Unlike languages like C and Java, Scheme allows identifiers to contain special characters, such as! $ % & * + -. / : < = >? @ ˆ. Identifiers should not begin with a character that can begin a number. This is a consequence of Scheme s simple syntax. You couldn t do this in Java because then there would be many ways to interpret the expression X-5+Y. Legal h-e-l-l-o give-me! WTF? Illegal 3some -stance [15]

Defining Variables define binds an expression to a global name: (define name expression) (define PI 3.14) > PI 3.14 (define High-School-PI (/ 22 7)) > High-School-PI 3.142857 [16]

Defining Functions define binds an expression to a global name: (define (name arg 1 arg 2...) expression) arg 1 arg 2... are formal function parameters. (define (f) hello) > (f) hello (define (square x) (* x x)) > (square 3) 9 [17]

Defining Helper Functions A Scheme program consists of a large number of functions. A function typically is defined by calling other functions, so called helper or auxiliary functions. (define (square x) (* x x)) (define (cube x) (* x (square x))) > (cube 3) 27 [18]

Preventing Evaluation Sometimes you don t want an expression to be evaluated. For example, you may want to think of (+ 4 5) as a list of three elements +, 4, and 5, rather than as the computed value 9. (quote (+ 4 5)) prevents (+ 4 5) from being evaluated. You can also write (+ 4 5). > (display (+ 4 5)) 9 > (display (quote (+ 4 5))) (+ 4 5) > (display (+ 4 5)) (+ 4 5) [19]

Dr Scheme Download DrScheme from here: http://www.drscheme.org. It has already been installed for you in lectura and the Windows machines in the lab. Start DrScheme under unix (on lectura) by saying > drscheme On Windows and MacOS it may be enough to click on the DrScheme logo to start it up. [20]

Dr Scheme Save definitions Definitions window Select language level Add teachpacks Language level Interaction window [21]

Dr Scheme Using TeachPacks [22]

Dr Scheme Using the Stepper [23]

References Free interpreter: http://www.drscheme.org. Manual: http://www.swiss.ai.mit.edu/projects/scheme/documentation/scheme.html Tutorials: http://ai.uwaterloo.ca/ dale/cs486/s99/scheme-tutorial.html http://cs.wwc.edu/%7ecs_dept/ku/pr/scheme.html http://www.cis.upenn.edu/%7eungar/cis520/scheme-tutorial.html http://dmoz.org/computers/programming/languages/lisp/scheme [24]

References... Language reference manual: http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs.ps. Some of this material is taken from http://www.ecf.utoronto.ca/ gower/csc326f/slides, c Diana Inkpen 2002, Suzanne Stevenson 2001. [25]

Scheme so Far A function is defined by (define (name arguments) expression) A variable is defined by (define name expression) Strings are inclosed in double quotes, like "this". Common operations on strings are (string-length string) (string-append list-of-strings) Numbers can be exact integers, inexact reals, fractions, and complex. Integers can get arbitrarily large. Booleans are written #t and #f. [26]

Scheme so Far... An inexact number is written: #i3.14159265. Common operations on numbers are (+ arg1 arg2), (- arg1 arg2) (add1 arg), (sub1 arg) (min arg1 arg2), (max arg1 arg2) A function application is written: > (function-name arguments) Quoting is used to prevent evaluation or (quote argument) argument [27]