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

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Programming Languages I LISP
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming
Additional Scheme examples
Functional Programming
Racket CSC270 Pepper major portions credited to
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
COP4020 Programming Languages
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
CS 36 – Chapter 11 Functional programming Features Practice
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lecture #9 מבוא מורחב.
CSE 3302 Programming Languages
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
To understand recursion, one must first understand recursion.
Lisp.
More Scheme CS 331.
Lists in Lisp and Scheme
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Introduction to Scheme

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

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;

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