Introduction to Scheme Lisp and Scheme Lisp List

  • Slides: 15
Download presentation
Introduction to Scheme

Introduction to Scheme

Lisp and Scheme • Lisp: List processor language – Full recursion – Conditional expression

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)

Dr. Scheme • Download Dr. Scheme from http: //download. plt-scheme. org/drscheme/ • Install Dr.

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

Primitive Elements • Atoms – A string of characters beginning with a letter, digit

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

Evaluation • Read->evaluate->print • Imperative language – f(x) – g(x, y, z) • Lisp

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

Manipulating lists • • car: get the first element of list cdr: get the

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.

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

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

A problem to solve A farmer is taking a fox, goose and bag of

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?

Valid States • Representation: (left-bank list, right-bank list) • Initial state: ((fox goose corn

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))

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

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

Exercise • Current state : = ((state on left bank)(state on right bank)) •

Exercise • Current state : = ((state on left bank)(state on right bank)) • Define initial state init. State to be ((fox goose corn boat)()) • Write a function left. Bank that returns state on left bank based on current state • Write a function right. Bank that returns state on right bank based on current state

Conditional expressions • In scheme: (cond 1 val 1) (cond 2 val 2) …

Conditional expressions • In scheme: (cond 1 val 1) (cond 2 val 2) … (condn valn) (else default-val) ) • In imperative language: if(cond 1) return val 1; else if(cond 2) return val 2; …. else if(condn) return valn; else return default-val;

Useful build-in functions • (eq? val 1 val 2) -- compare two values –

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

Exercise • Write a function called other. Bank that returns RIGHT if bank==LEFT returns

Exercise • Write a function called other. Bank that returns RIGHT if bank==LEFT returns LEFT if bank==RIGHT and otherwise returns HUH

Recursive list processing • If we want to process elements of a list L

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 is. There to test if x is on list L