1 / 25

Scheme

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

jconnelly
Download Presentation

Scheme

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scheme A “small” variant of Lisp

  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

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

  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

  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

  6. Math • Form of function application is prefix Syntax: operatorvaluevalue • 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)))

  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

  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

  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 op1op2) – boolean conjunction (or op1op2) – boolean disjunction

  10. Defining & using variables (define x 1) • defines variable x to have value 1 > (define a 4) > a 4 a  4 (+ a 6)  10

  11. Lists • Lists () is the empty list Lists 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

  12. Functions for lists (member op1op2) – 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

  13. List construction functions-1 (list op1op2 …) - form a list from a sequence of items (append op1op2 …) – 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 op1op2) - 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)

  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) ‘()) 

  15. List construction functions-2 • More list functions & predicates caar: (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

  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)

  17. Define-struct: Dr. Scheme Syntax: (define-struct name (field1field2 …)) 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

  18. Conditional evaluation Syntax: (cond [bool-condition1expr1] [bool-condition2expr2] … [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)

  19. Functions-1 • Use “lambda expressions” • Syntax: (lambda (op1op2 …) (expression)) (lambda (init-state) (cond [(null? init-state) init-state] [else (car init-state)] ) ) #<procedure> >

  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 )

  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

  22. Exercise-2 • Write a function to return –1, 0, +1 depending on two parameters a and b a < b return –1 a > b return +1 a = b return 0

  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

  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

  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

More Related