Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
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.
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
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 Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Clojure 2 Feb 7,
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Functional Languages  A function is an “association of a certain object from one set (the range) with each object from another set (the domain)  A function.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Fundamentals of Functional Programming Languages
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
Lisp and Scheme I.
Recursion, Higher-order-functions
Closures and Streams cs7100(Prasad) L11Clos
Lecture 13: Assignment and the Environment Model (EM)
Modern Programming Languages Lecture 18 Fakhar Lodhi
Chapter 15 Functional Programming 6/1/2019.
Presentation transcript:

λ => Scheme for Rubyists

Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson & Sussman

Scheme in Real Life GIMP (Script-Fu) Guile (official extension language of GNU) Lily Pond (sheet music engraving)

Scheme is a Lisp dialect LISP ==> “LIS t P rocessing language ”

Scheme is a Lisp dialect LISP ==> “LIS t P rocessing language ” LISP ==> L ots of I rritating S uperfluous P arentheses

Lisp Family of languages: Common Lisp Scheme Emacs Lisp Clojure Arc First incarnation by John McCarthy, 1958 older than COBOL (!)

Lisp vs. the World Prefix notation (+ x y) (< x y) (eq? x y)

Lisp vs. the World No operator precedence rules (mandatory parentheses eliminate the need) (sqrt (+ (/ (* 3 4) 2) 3))

Lisp vs. the World No operator precedence rules (mandatory parentheses eliminate the need) (sqrt (+ (/ (* 3 4) 2) 3)) ==> 3

Lisp vs. the World Functions are first-class objects, so we can: Assign them to variables Pass them as parameters to other functions Return them from other functions

Lisp vs. the World Example of 1st-class function use in Scheme (Scheme's map =~ Ruby's map or collect ) (define square (lambda (x) (* x x))) (map square (list ))

Lisp vs. the World Example of 1st-class function use in Scheme (Scheme's map =~ Ruby's map or collect ) (define square (lambda (x) (* x x))) (map square (list )) ==> ( )

Lisp vs. the World Linked lists are built in; based on pairs Scheme representation: (1 2 “a” 4 #\b) 1 2 “a” 4 #\b (empty list)

Scheme vs. Other Lisps Optimized tail recursion “Normal” looping constructs are not used Tail-recursive procedures are replaced by iterative version in compilation Why? Even for mutually recursive procedures

Scheme vs. Other Lisps Optimized tail recursion “Normal” looping constructs are not used Tail-recursive procedures are replaced by iterative version in compilation Why? So we don't blow the stack Even for mutually recursive procedures

Mutual Tail Recursion (define even? (lambda (x) (if (= x 0) #t (odd? (- x 1))))) (define odd? (lambda (x) (if (= x 0) #f (even? (- x 1)))))

Why use recursion? Some problems are more cleanly solved

Why use recursion? Some problems are more cleanly solved Especially in mathematics factorial(n) = { 1 if n==0 or n==1 n * factorial(n-1)

Why use recursion? (define factorial (lambda (n) (cond ((or (= n 1) (= n 0)) 1) (else (* n (factorial (- n 1))))))) Nearly identical to math definition Any problems?

Why use recursion? (define factorial (lambda (n) (cond ((or (= n 1) (= n 0)) 1) (else (* n (factorial (- n 1))))))) Nearly identical to math definition Any problems? not tail-recursive

Why use recursion? (define factorial (lambda (n) (define factorial2 (lambda (n acc) (cond ((or (= n 1) (= n 0)) acc) (else (factorial2 (- n 1) (* n acc)))))) (factorial2 n 1))) Now we're tail-recursive Is this better?

Why use recursion? (define factorial (lambda (n) (define factorial2 (lambda (n acc) (cond ((or (= n 1) (= n 0)) acc) (else (factorial2 (- n 1) (* n acc)))))) (factorial2 n 1))) Now we're tail-recursive Is this better? it depends

A Tale of Two Factorial Solutions (factorial 5) (* 5 (factorial 4)) (* 5 (* 4 (factorial 3))) (* 5 (* 4 (* 3 (factorial 2)))) (* 5 (* 4 (* 3 (* 2 (factorial 1))))) (* 5 (* 4 (* 3 (* 2 1)))) (* 5 (* 4 (* 3 2))) (* 5 (* 4 6)) (* 5 (24)) 120 (factorial 5) (factorial2 5 1) (factorial2 4 5) (factorial2 3 20) (factorial2 2 60) (factorial ) 120 Non-tail-recursiveTail-recursive

Scheme vs. Other Lisps Scheme has a minimal set of standard features Common Lisp has lots of built-in libraries One namespace :( Continuations Representation of the state of execution (think goto on steroids) Also available in Ruby ( callcc )

Prefix Notation (+ 2 7) => 9 (* 2/3 5/8) => 5/12 (/ 1.0 3) => (= 2 (+ 0 3)) => #f (not (= (> 2 (+ 6 1)) #t)) => #t

Defining Procedures (define add (lambda (x y) (+ x y))) (define truthy? (lambda (x) (not (= #f x)))) (define (truthy? x) (not (= #f x)))

Quoting (1 2 3) ;The object 1 is not applicable. (quote (1 2 3)) ==> (1 2 3) '(1 2 3) ==> (1 2 3) 'testing ==> testing

Applying Procedures (add 7 17) => 24 (truthy? '(1 2 4)) => #t (truthy? #f) => #f (truthy? 78) => #t

Parsing Lists (car '(1 2 3)) ==> 1 (cdr '(1 2 3)) ==> (2 3)

Building Lists (cons 1 2) ==> (1. 2) (cons 1 (cons 2 '())) ==> (1 2) (empty list)

Building Lists Will this work the way we expect? (cons '(1 2 3) '(4 5 6))

Building Lists Will this work the way we expect? (cons '(1 2 3) '(4 5 6)) ==> ((1 2 3) 4 5 6)

Building Lists What about this? (cons 1 (cons 2 (cons 3 '(4 5 6))))

Building Lists What about this? (cons 1 (cons 2 (cons 3 '(4 5 6)))) ==> ( ) Yes, but this way sucks

Building Lists The simpler solution: (append '(1 2 3) '(4 5 6)) ==> ( )

Functional Programming The result depends only on the input(s) No side effects Easy to run in parallel

Scheme's functional-ness Scheme encourages functional programming But it doesn't require it (not purely functional) (define x '(1 2 3)) x ==> (1 2 3) (set-car! x 'stupid-symbol) x ==> (stupid-symbol 2 3) (set-cdr! x '(#t #f)) x ==> (stupid-symbol #t #f)

Getting Started with Scheme Download an implementation MIT-Scheme: Set up your text editor Emacs (indentation help) Textmate Bundle

Anything else we might want?

Test Manager: xUnit for Scheme Author: Alexey Radul Home Page: Documentation:

Test Manager Example (load “test-manager/load.scm”) (define-each-test (assert-equal 2 (factorial 2)) (assert-equal 6 (factorial 3)) (assert-equal 24 (factorial 4))) (run-registered-tests)

λ => Happy Happy Joy Joy