Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.

Similar presentations


Presentation on theme: "Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one."— Presentation transcript:

1 Introduction to Scheme

2 Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one of most popular dialects of Lisp. ( Another popular one: common lisp)

3 Dr.Scheme Download Dr. Scheme from http://download.plt-scheme.org/drscheme/ http://download.plt-scheme.org/drscheme/ Install Dr. Scheme in your computer Set language: “advanced student”

4 Primitive Elements Atoms –A string of characters beginning with a letter, digit or special character other than a single left or right parenthesis. Lists –(atoms +/or lists) –Null Define a atom and a list in Dr. Scheme

5 Evaluation Read->evaluate->print Imperative language –f(x) –g(x, y, z) Lisp –(f x) –(g x y z) In a list, first element is expected to be a function which uses remaining elements as arguments

6 Manipulating lists car: get the first element of list cdr: get the rest of list caar: get the first item of the first list in a list cadr: get the second item of a non-empty list cadar: get the second item of the first list of a list caddr: get the third item of a non-empty list cons: construct a list by two lists or a list and an atom. The second argument must be a list.

7 How they come? caar == car(car list) cadr == car(cdr list) cadar == car(cdr(car list)) caddr == car(cdr(cdr list))

8 A problem to solve A farmer is taking a fox, goose and bag of corn to market and must cross a river. The river has a boat that can hold the farmer and one item, so he must make multiple crossings while leaving some items unattended. If the fox gets a chance, it will eat the goose; likewise the goose will eat the corn. What’s a pair farmer to do?

9 Valid States Representation: (left-bank list, right-bank list) Initial state: ((fox goose corn boat) ()) Tasks - define functions: (left-bank state) : (fox goose corn boat) (right-bank state): null Another states: –((fox corn) (goose boat)) –((goose boat) (fox corn)) Final state: (()(fox goose corn boat))

10 Defining functions (define function_name (lambda (args) expressions ) Function call: (function_name args)

11 Exercise Current state := ((state on left bank)(state on right bank)) Define initial state initState to be ((fox goose corn boat)()) Write a function leftBank that returns state on left bank based on current state Write a function rightBank that returns state on right bank based on current state

12 Conditional expressions In scheme: (cond (cond1 val1) (cond2 val2) … (condn valn) (else default-val) ) In imperative language: if(cond1) return val1; else if(cond2) return val2; …. else if(condn) return valn; else return default-val;

13 Useful build-in functions (eq? val1 val2) -- compare two values –Return true if the two values are the same (> val1 val2) -- compare numbers for greater-than (< val1 val2) -- compare numbers for less- than (= val1 val2) -- compare numbers for equality (null? val) – if the value is empty list

14 Exercise Write a function called otherBank that returns RIGHT if bank==LEFT returns LEFT if bank==RIGHT and otherwise returns HUH

15 Recursive list processing If we want to process elements of a list L with function f –First, process first element of L : f( car L) –Next, recursively process rest of L: (cdr L) Exercise: –Write a function isThere to test if x is on list L


Download ppt "Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one."

Similar presentations


Ads by Google