Top Banner
1 Scheme A “small” variant of Lisp
25

Scheme

Jan 23, 2016

Download

Documents

frisco

Scheme. A “small” variant of Lisp. Basics. Why Lisp? Learning lisp in the study of AI is like learning French if you are going to France (Charniak & McDermot) Interpreted language Prefix operator notation, with parentheses Functional style. Overview. Dr Scheme Atoms & Lists - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scheme

1

Scheme

A “small” variant of Lisp

Page 2: Scheme

2

Basics

• Why Lisp?– Learning lisp in the study of AI is like learning

French if you are going to France (Charniak & McDermot)

• Interpreted language

• Prefix operator notation, with parentheses

• Functional style

Page 3: Scheme

3

Overview

• Dr Scheme• Atoms & Lists• Functions: Math & predicates• Constants: Quoting• More functions: List construction• Defining variables• Flow of control• Defining functions

Page 4: Scheme

4

Dr. Scheme

• See web site for downloads• Has different language levels:

– “Language”• “Choose Language”

– “Full Scheme”

• Two windows– Top: program editing (“definitions”) window

• Define functions, variables, comments

– Bottom: interactive evaluation window: interpreted• Evaluate functions, test code

Page 5: Scheme

5

Atoms & Lists

• Characters are: A…Z, a…z, 0…9, ! % $ * + - / = ?• Atom

– String of one or more characters with no spaces between; Examples: 123, 1xyz, fred

• #\ used for character constants; e.g., #\newline• #t, #f (true, false)• Upper/lower case only distinguished in constants

Page 6: Scheme

6

Math

• Form of function application is prefixSyntax: operator value value

• Expressions are parenthesized, e.g.,(+ 1 2)

• Math operators: + - / *; other examples:– Other examples: abs, sqrt, modulo, remainder

• Nested expressions often used:(abs (* (- 2 3) (- 0 1)))

Page 7: Scheme

7

“Interactions” window

> (+ 3 4)

7

>

> (+ (- 6 3) 10)

WARNING: Interactions window is out of sync with the definitions window. Click Execute.

13 Notation: (+ (- 6 3) 10) 13

Page 8: Scheme

8

Predicates: Functions returning boolean values

• Often named ending with “?”equal? – test for equality (works with lists)

boolean? – test for boolean type object

integer? – test for integer type object

(equal? 10 3) #f

(equal? 4 4) #t

(boolean? #f) #t

Page 9: Scheme

9

Relational operators and functions

(> op1 op2) – test if op1 > op2

(< op1 op2) – test if op1 < op2

(= op1 op2) – numeric equality test

(>= op1 op2)

(<= op1 op2)

(not op1) – negate the boolean operand

(and op1 op2) – boolean conjunction

(or op1 op2) – boolean disjunction

Page 10: Scheme

10

Defining & using variables

(define x 1)– defines variable x to have value 1

> (define a 4)

> a

4

a 4

(+ a 6) 10

Page 11: Scheme

11

Lists

• Lists() is the empty listLists are fully parenthesized and contain lists and/or sequences

of atoms

• Often need to quote lists to pass as parameters to functions• E.g., test if two lists “(+ 1 2)” and “(+ 2 1)” are equal

(equal? (+ 1 2) (+ 2 1)) #t• Quote the lists so that they are not evaluated

(equal? ‘(+ 1 2) ‘(+ 2 1)) #f

• Quotes just need single quote in front of list

Page 12: Scheme

12

Functions for lists

(member op1 op2) – if op1 is a list element of op2, return sublist of op2 starting with op1; else return #f

(member ‘1 ‘(+ 1 2)) (1 2)

(member ‘- ‘(+ 1 2)) #f

(member ‘1 ‘((1 2) (2 3))) #f

Page 13: Scheme

13

List construction functions-1

(list op1 op2 …) - form a list from a sequence of items

(append op1 op2 …) – put elements of lists in a new list

(car op) - return the head element of a list op(cdr op) - return the tail of a list op, as a list(cons op1 op2) - make new list with op1 on front of op2

(list ‘a ‘b ‘c) (a b c)(append '(a) '(b)) (a b)(car ‘(a b)) a(cdr ‘(a b)) (b)(cons ‘a ‘(b c)) (a b c)

Page 14: Scheme

14

Exercise-1

(list ‘(a) ‘b))

(append ‘(a b c) ‘(1 2 3)) (car (list ‘a ‘b ‘c)) (cdr ‘((a b) c)) (cdr ‘(a)) (cons ‘a ‘((b))) (cons ‘(a) ‘())

Page 15: Scheme

15

List construction functions-2

• More list functions & predicatescaar: (car (car x))cadr: (car (cdr x))caddr: (car (cdr (cdr x)))

etc (up to 4 operators in a sequence)(null? ‘()) #t(null? ‘(a b c)) #f(null? (cdr ‘(a))) #t

Page 16: Scheme

16

Defining list variables

(define y ‘(a b c))

y (a b c)(define init-state ‘((blank 5 4) (6 1 8) (7 3 2)))

(car init-state) (blank 5 4)

Page 17: Scheme

17

Define-struct: Dr. Scheme

Syntax: (define-struct name (field1 field2 …))

Make new structs: make-name

Get elements: name-field2, name-field2…

(define-struct row (left middle right))

(define row1 (make-row ‘blank 1 8))

(row-left row1) blank

(row-middle row1) 1

(row-right row1) 8

Page 18: Scheme

18

Conditional evaluation

Syntax: (cond[bool-condition1 expr1][bool-condition2 expr2]…[else default-expr] ; optional

)(define init-state ‘((blank 5 4) (6 1 8) (7 3 2)))

(cond[(null? init-state) init-state][else (car init-state)]

) (blank 5 4)

Page 19: Scheme

19

Functions-1

• Use “lambda expressions”• Syntax: (lambda (op1 op2 …) (expression))

(lambda (init-state) (cond

[(null? init-state) init-state][else (car init-state)]

))#<procedure>>

Page 20: Scheme

20

Functions-2: Supplying parameters

(list ‘(a)) ; typical function usage( list ; the function ‘(a) ; the parameter)( (lambda (init-state)

(cond [(null? init-state) init-state] [else (car init-state)]

) ) ; the function

'(a) ; the parameter)

Page 21: Scheme

21

Functions-3

• Usually want to name our functions– These have been “anonymous” functions

(define ourFun (lambda (arg)

(cond [(null? arg) arg] [else (car arg)]) ; end cond

) ; end lambda) ; end define(ourFun '(a)) a

Page 22: Scheme

22

Exercise-2

• Write a function to return –1, 0, +1 depending on two parameters a and ba < b return –1

a > b return +1

a = b return 0

Page 23: Scheme

23

Recursion: Control structure in functional programming

• Count the number of elements in a list(define list-length (lambda (myList) (cond [(null? myList) 0] [else

(+ 1 (list-length (cdr myList)) )]

) ; end cond ) ; end lambda) ; end list-length

Page 24: Scheme

24

Recursive list reverse

(define list-reverse (lambda (myList) (cond [(null? myList) myList] [else (append

(list-reverse (cdr myList))(list (car myList))

)] ; end else

) ; end cond ) ; end lambda) ; end list-reverse

Page 25: Scheme

25

Non-negative integer multiply through repeated addition

(define int-mult (lambda (x y) (cond [(equal? y 0) 0] [(equal? x 0) 0] [(equal? y 1) x] [else

(+ x(int-mult x (- y 1))

)] ; end else

) ; end cond ) ; end lambda ) ; end int-mult