150 likes | 296 Vues
This guide introduces Scheme, a popular dialect of Lisp, focusing on its core concepts, including recursion, list manipulation, and conditional expressions. You'll learn to download and install DrScheme, set it for advanced student mode, and write functions to manage lists and evaluate expressions. The guide also includes exercises to deepen your understanding, like solving the classic farmer, fox, goose, and corn problem using valid state representations and function definitions. Ideal for beginners and intermediate learners interested in functional programming.
E N D
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. Scheme in your computer • Set language: “advanced student”
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 • (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 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 == car(cdr(car list)) • caddr == car(cdr(cdr list))
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 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 args)
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
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; Conditional expressions
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
Exercise • Write a function called otherBank 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 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