A Brief Introduction to Scheme (I)

Similar documents
A Brief Introduction to Scheme (II)

CSC 533: Programming Languages. Spring 2015

Functional Programming. Pure Functional Languages

Module 10: Imperative Programming, Modularization, and The Future

Functional Programming. Pure Functional Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

CS 314 Principles of Programming Languages

Chapter 15 Functional Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Chapter 15. Functional Programming Languages

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

Chapter 1. Fundamentals of Higher Order Programming

FP Foundations, Scheme

Functional Programming

Functional Programming Languages (FPL)

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

Functional Programming Lecture 1: Introduction

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

Functional Programming. Pure Functional Programming

Functional Programming. Big Picture. Design of Programming Languages

An introduction to Scheme

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

Scheme Quick Reference

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

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Scheme Quick Reference

CSCC24 Functional Programming Scheme Part 2

Concepts of Programming Languages

Functional programming with Common Lisp

Documentation for LISP in BASIC

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 Languages. Hwansoo Han

CS 360 Programming Languages Interpreters

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

Functional abstraction

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

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

CS 314 Principles of Programming Languages

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

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

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

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Class Structure. Prerequisites

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

CS115 - Module 9 - filter, map, and friends

LECTURE 16. Functional Programming

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Module 8: Local and functional abstraction

Ruby: Introduction, Basics

Scheme: Expressions & Procedures

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

Chapter 15. Functional Programming Languages

An Introduction to Scheme

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

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

Introduction to Scheme

Functional Programming

Imperative, OO and Functional Languages A C program is

CS 314 Principles of Programming Languages. Lecture 16

Chapter 11 :: Functional Languages

SD314 Outils pour le Big Data

Introduction to Functional Programming

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

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Reasoning About Programs Panagiotis Manolios

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

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

CS 314 Principles of Programming Languages

Functional Programming - 2. Higher Order Functions

CSc 520 Principles of Programming Languages

Box-and-arrow Diagrams

Principles of Programming Languages

Functional Programming in Scala. Raj Sunderraman

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

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

MIT Scheme Reference Manual

Functional Programming Languages (FPL)

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

Func%onal Programming in Scheme and Lisp

Introduction to Functional Programming in Haskell 1 / 56

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?

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

CIS192: Python Programming

User-defined Functions. Conditional Expressions in Scheme

Ruby: Introduction, Basics

CS111: PROGRAMMING LANGUAGE II

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

Principles of Programming Languages

Func%onal Programming in Scheme and Lisp

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

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Functional Programming and Haskell

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

CS 11 Haskell track: lecture 1

Programming Languages

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

The syntax and semantics of Beginning Student

Transcription:

A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada

Scheme Scheme I p.1/44

Scheme: Feature Set A small dialect of Lisp Support symbolic list processing Garbage collected Functional (although not purely functional) Better than Lisp... first-class procedures tail-call optimization continuations user-defined syntactic extensions the first Lisp dialect to support lexical scoping best for building sophisticated interpreters Scheme I p.2/44

Scheme: What s the Big Deal? Why learn about functional programming with a Lisp dialect... Python has lambda expressions, etc see also Ruby XSLT is functional its ancester DSSSL is a Scheme dialect Scheme and Lisp are popular extension languages Emacs: a scriptable UNIX editor Sawfish: an scriptable X11-based window manager Guile: the GNU extension language Scheme I p.3/44

Scheme: What s the Big Deal? Why learn about functional programming with a Lisp dialect... Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. Eric Raymond How To Become a Hacker Scheme I p.4/44

Scheme: What s the Big Deal? Why learn about functional programming with a Lisp dialect... MIT teaches Scheme to their 1st-year CS students Waterloo is following suit SFU is beginning to teach Python to their 1st-year CS students...... because it helps them to learn functional programming concepts such as lambda expressions and higher-order functions. Scheme I p.5/44

Scheme: Standardization ANSI/IEEE standard The Revised 5 Report on the Algorithmic Language Scheme [R 5 RS] Scheme I p.6/44

Interacting with Scheme Scheme I p.7/44

The Scheme REPL Scheme prompt: > Scheme I p.8/44

The Scheme REPL Scheme prompt: > (* 2 (cos 0) (+ 4 6)) Scheme I p.8/44

The Scheme REPL Scheme prompt: > (* 2 (cos 0) (+ 4 6)) 20 Scheme I p.8/44

The Scheme REPL Scheme prompt: > (* 2 (cos 0) (+ 4 6)) 20 The read-evaluate-print loop (REPL): loop read an expression evaluate the expression print the result of evaluation end loop Evaluation: expression evaluate value Scheme I p.8/44

Scheme Syntax Let s examine the example in details: > (* 2 (cos 0) (+ 4 6)) 20 Scheme I p.9/44

Scheme Syntax Let s examine the example in details: > (* 2 (cos 0) (+ 4 6)) 20 constants: 0, 2, 4, 6 identifiers: cos, +, * structured forms: (...) Brackets are not for grouping! In C/C++, ((1)) is the same as 1 In Scheme, ((1)) is NOT 1 Brackets are for delimiting structured forms. procedure application: (proc arg 1... arg n ) + : name of the addition procedure case-insensitive: (cos 0) (COS 0) Scheme I p.9/44

Order of Evaluation Order of Evaluation: When there are multiple subexpressions, which subexpression should be evaluated first? Applicative Order: (proc arg 1... arg n ) 1. evaluate arg 1,..., arg n 2. evaluate proc 3. apply proc to arg 1,..., arg n Scheme I p.10/44

Numeric Procedures Procedure Meaning (+ x 1... x n ) The sum of x 1,..., x n (* x 1... x n ) The product of x 1,..., x n (- x y) Subtract y from x (- x) Minus x (/ x y) Divide x by y (max x 1... x n ) The maximum of x 1,..., x n (min x 1... x n ) The minimum of x 1,..., x n (sqrt x) The square root of x (abs x) The absolute value of x Scheme I p.11/44

Exercise Look up from [TSPL3] Chapter 6 the answers to the following questions: What procedure computes x n? What is the difference between the procedures remainder and modulo? Scheme I p.12/44

Scheme Programs Scheme I p.13/44

lambda Expressions An anonymous procedure: (lambda (x) (* x 2)) (lambda...) - a name-less procedure (x) - parameter list (* x 2) - procedure body Applying the procedure: > ((lambda (x) (* x 2)) 3) 6 The value of a procedure application is obtained by 1. substituting the actual arguments for the formal parameters in the procedure body 2. evaluating the procedure body Scheme I p.14/44

Top-Level Definitions > (define y 3) Scheme I p.15/44

Top-Level Definitions > (define y 3) > y 3 Scheme I p.15/44

Top-Level Definitions > (define y 3) > y 3 > (define double (lambda (x) (* x 2))) Scheme I p.15/44

Top-Level Definitions > (define y 3) > y 3 > (define double (lambda (x) (* x 2))) > (double 3) 6 Scheme I p.15/44

Top-Level Definitions > (define y 3) > y 3 > (define double (lambda (x) (* x 2))) > (double 3) 6 > (double (double 3)) 12 Scheme I p.15/44

Storing Programs in Files Demonstration Edit program Syntax-check program Run program Scheme I p.16/44

Functional Programming Scheme I p.17/44

Imperative Programming Mutable global state assignment statements modify variable bindings Command = state transformer state 1 command state 2 Program = sequence of commands Scheme I p.18/44

Functional Programming No side effect allowed No assignment statement! Every expression denotes a unique value: expression evaluate value Referential transparency variable bindings do not mutate evaluating an expression always yields the same value Scheme I p.19/44

The Functional Fragment of Scheme Scheme supports both the imperative and functional paradigm of programming In the first half of the course, we focus on the functional fragement of Scheme. i.e., programming with expressions that do not produce side effects Scheme I p.20/44

Conditional Expressions Scheme I p.21/44

Boolean Values Boolean values: #t, #f Anything that is not #f is considered true. A procedure that returns a boolean value is called a predicate. Scheme I p.22/44

Example: absolute-value abs(x) = { x if x < 0 x otherwise (define absolute-value (lambda (x) (if (negative? x) (- x) x))) Scheme I p.23/44

Conditional Form if (if test-expression true-expression false-expression) 1. Evaluate test-expression. 2. If test-expression evaluates to true then evaluate true-expression and return its value. 3. Otherwise, evaluate false-expression and return its value. Scheme I p.24/44

Numeric Predicates Predicate (zero? x) (positive? x) (negative? x) (even? x) (odd? x) Meaning x is zero x is positive x is negative x is even x is odd Scheme I p.25/44

Relational Predicates Predicate Meaning (= x y) x is equal to y (< x y) x is less than y (< x y) x is greater than y (<= x y) x is no greater than y (>= x y) x is no less than y Scheme I p.26/44

Type Predicates Predicate (number? x) (boolean? x) Meaning x is a number x is a Boolean value (i.e., #t, #f) Scheme I p.27/44

Example: sign sign(x) = (define sign (lambda (x) (cond ((positive? x) 1) ((zero? x) 0) (else -1)))) 1 if x > 0 0 if x = 0 1 otherwise Scheme I p.28/44

Conditional Form cond (cond (test 1 expr 1 ) (test 1 expr 2 )... (test n expr n ) (else default)) 1. Evaluate test 1, test 2,..., test n in the order they appear. 2. If some test i evaluates to true, then stop evaluating the rest of the test expressions, but instead evaluate expr i and return its value. 3. Otherwise, evaluate default and return its value. Scheme I p.29/44

More Conditional Forms Forms (or x 1... x n ) (and x 1... x n ) (not x) Meaning Logical or Logical and Logical negation These are not procedures. Example: (or x 1... x n ) Evaluate x 1,..., x n in the order they appear If any of the arguments evaluates to true, return true rightaway without evaluating the rest of the arguments. Otherwise, return #f. Scheme I p.30/44

Exercise Rewrite (not x) into an equivalent expression using only the if form. Rewrite (and x 1... x n ) into an equivalent expression using only the cond form. The definition of and in terms of cond is only an approximation. Check out [TSPL3] Sect. 5.3 to see why. Scheme I p.31/44

Equational Reasoning Scheme I p.32/44

Equational Reasoning We grow up knowing equational reasoning by heart: (1 + 2) (3 4) = 3 (3 4) = 3 1 = 3 Because of referential transparency, the behavior of functional programs can be understood using equational reasoning! Scheme I p.33/44

Example: double (double (double 3)) = (double ((lambda (x) (* x 2)) 3)) = (double (* 3 2)) = (double 6) = ((lambda (x) (* x 2)) 6) = (* 6 2) = 12 Scheme I p.34/44

Example: absolute-value (absolute-value -3) = ((lambda (x) (if (negative? x) (- x) x)) -3) = (if (negative? -3) (- -3) -3) = (if #t (- -3) -3) = (- -3) = 3 Scheme I p.35/44

Recursion Scheme I p.36/44

Recursion vs Iteration In imperative programming languages, repetition can be achieved by iterative constructs such as for or while. In functional programming languages, repetition is mainly achieved by recursion. Scheme I p.37/44

Example: triangular (1) triangular(n) = 1 + 2 +... + n { 1 if n = 1 = n + triangular(n 1) otherwise (define triangular (lambda (n) (if (= n 1) 1 (+ n (triangular (- n 1)))))) Scheme I p.38/44

Example: triangular (2) > (trace triangular) > (triangular 3) (triangular 3) (triangular 2) (triangular 1) 1 3 6 6 > (untrace triangular) Scheme I p.39/44

Example: triangular (3) (triangular 3) = ((lambda (n) (if (= n 1) 1 (+ n (triangular (- n 1))))) 3) = (if (= 3 1) 1 (+ 3 (triangular (- 3 1)))) = (if #f 1 (+ 3 (triangular (- 3 1)))) = (+ 3 (triangular (- 3 1))) = (+ 3 (triangular 2)) = (+ 3 (if (= 2 1) 1 (+ 2 (triangular (- 2 1))))) = (+ 3 (+ 2 (triangular (- 2 1)))) = (+ 3 (+ 2 (triangular 1))) = (+ 3 (+ 2 (if (= 1 1) 1 (+ 1 (triangular (- 1 1)))))) = (+ 3 (+ 2 1)) = (+ 3 3) = 6 Scheme I p.40/44

Example: power (1) x n = { 1 if n = 0 x x n 1 if n > 0 (define power (lambda (x n) (if (zero? n) 1 (* x (power x (- n 1)))))) Scheme I p.41/44

Example: power (2) > (power 2 4) (power 2 4) (power 2 3) (power 2 2) (power 2 1) (power 2 0) 1 2 4 8 16 16 Scheme I p.42/44

Exercise Use equational reasoning to trace the execution of (power 2 4). Scheme I p.43/44

Lecture Summary This lecture and the next cover [TSPL3] Chap. 1 2. In this lecture... REPL the numeric fragment of Scheme functional programming with Scheme equational reasoning lambda expression top-level definition Boolean values conditional forms simple examples of recursion Scheme I p.44/44