Introduction to Functional Programming


 Collin Horton
 2 years ago
 Views:
Transcription
1 Introduction to Functional Programming Xiao Jia Summer 2013
2 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell, JavaScript, Lua, Racket, Ruby, Usual filename extensions.scm.ss 2
3 Expressions and lists Arithmetic expressions Defining names Procedures Special forms: if, cond Applicative order versus normal order Example: square roots by Newton s method Lists: cons, car, cdr 3
4 Arithmetic expressions 486 è 486 ( ) è 486 ( ) è 666 (* 5 99) è 495 (/ 10 5) è 2 ( ) è 12.7 ( ) è 75 (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ ( 10 7) 6)) (* ) è
5 Defining names (define size 2) size è 2 (* 5 size) è 10 (define pi ) (define radius 10) (* pi (* radius radius)) è (define circumference (* 2 pi radius)) 5
6 Defining procedures (define (square x) (* x x)) (square 21) è 441 (square (+ 2 5)) è 49 (square (square 3)) è 81 (define (sumofsquares x y) (+ (square x) (square y)) (sumofsquares 3 4) è 25 (define (<name> <formal parameters>) <body>) 6
7 if (define (abs x) (if (< x 0) ( x) x)) (if <predicate> <consequent> <alternative>) 7
8 cond (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (define (abs x) ((< x 0) ( x)))) (cond (<p 1 > <e 1 >) (<p 2 > <e 2 >) (<p n > <e n >)) (cond ((< x 0) ( x)) (else x))) 8
9 Applicativeorder evaluation (sumofsquares (+ 5 1) (* 5 2)) (sumofsquares 6 10) (+ (square 6) (square 10)) (+ (* 6 6) (* 10 10)) ( ) 136 Evaluate the arguments and then apply 9
10 Normalorder evaluation (sumofsquares (+ 5 1) (* 5 2)) (+ (square (+ 5 1)) (square (* 5 2)) ) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) (+ (* 6 6) (* 10 10)) ( ) 136 Do not evaluate the operands until necessary 10
11 Square roots by Newton s method sqrt(x) is the y such that y 0 and y 2 = x Newton s method: given a guess y, average y and x/y to get a better guess Guess Quotient Average 1 (2/1) = 2 ((2 + 1)/2) = (2/1.5) = (( )/2) = (2/1.4167) = (( )/2) =
12 (define (sqrtiter guess x) (if (goodenough? guess x) guess (sqrtiter (improve guess x) x))) (define (goodenough? guess x) (< (abs ( (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt x) (sqrtiter 1.0 x)) 12
13 Lists (cons x y) è (x. y) (car (cons x y)) è x head (cdr (cons x y)) è y tail A sequence 1, 2, 3, 4 ó (cons 1 (cons 2 (cons 3 (cons 4 ())))) where () is pronounced as unit Shorter version: (list ) (cdr (list )) è (2 3 4) 13
14 14
15 (define (sum items) (+ (car items) (sum (cdr items)))) (sum (list 1 2 3)) (sum (1 2 3)) (+ (car (1 2 3)) (sum (cdr (1 2 3)))) (+ 1 (sum (2 3))) (+ 1 (+ (car (2 3)) (sum (cdr (2 3))))) (+ 1 (+ 2 (sum (3)))) (+ 1 (+ 2 (+ (car (3)) (sum (cdr (3)))))) (+ 1 (+ 2 (+ 3 (sum ())))) (+ 1 (+ 2 (+ 3 (+ (car ()) (sum (cdr ())))))) Error: Cannot apply car/cdr on the unit () 15
16 (define (sum items) (+ (car items) (sum (cdr items)))) (define (sum items) (if (null? items) 0 (+ (car items) (sum (cdr items))))) (sum (list 1 2 3)) è 6 16
17 Recursive style: (define (length items) (if (null? items) 0 (+ 1 (length (cdr items))))) Iterative style: (define (length items) (define (lengthiter a count) (if (null? a) count (lengthiter (cdr a) (+ 1 count)))) (lengthiter items 0)) 17
18 How to represent a tree? A binary tree An arbitrary tree Define some utility functions height leaves 18
19 Functions and streams Linear recursion Tree recursion Higherorder functions Anonymous functions Local names Infinite streams 19
20 Linear recursion (define (factorial n) recursive style è linear space (if (= n 1) 1 (* n (factorial ( n 1))))) 20
21 Linear recursion (define (factorial n) iterative style è constant space (factiter 1 1 n)) (define (factiter product counter maxcount) (if (> counter maxcount) product (factiter (* counter product) (+ counter 1) maxcount))) 21
22 Tree recursion (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib ( n 1)) (fib ( n 2)))))) How to compute it with a linear iteration? 22
23 Higherorder functions (define (square x) (* x x)) (define (map f a) (if (null? a) () Treat a function as a value (cons (f (car a)) (map f (cdr a))))) (map square (list 1 2 3)) è (1 4 9) 23
24 Anonymous functions (define (square x) (* x x)) and functions are values (define square (lambda (x) (* x x))) closures (map (lambda (x) (+ x 10)) (list 1 2 3)) è ( ) 24
25 Local names (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) ( 1 y))) (define (f x y) (let ((a (+ 1 (* x y)) ) (b ( 1 y) )) (+ (* x (square a)) (* y b) (* a b)))) 25
26 Infinite streams (define (numbersfrom n) (cons n (numbersfrom (+ n 1)))) (numbersfrom 10) (cons 10 (numbersfrom (+ 10 1))) (cons 10 (numbersfrom 11)) (cons 10 (cons 11 (numbersfrom (+ 11 1)))) 26
27 Infinite streams (define (numbersfrom n) (cons n (lambda () delay evaluation (numbersfrom (+ n 1))))) (numbersfrom 10) è (10. #<Closure>) We need to define a tail function for infinite streams 27
28 Infinite streams (define (tail stream) ((cdr stream))) closure value closure stream car (head) cdr (tail) closure application (numbersfrom 10) è (10. #<Closure>) (tail (numbersfrom 10)) è (11. #<Closure>) 28
29 Infinite streams (define (nth stream n) (if (= n 1) (car stream) (nth (tail stream) ( n 1)))) (nth (numbersfrom 10) 1) è 10 (nth (numbersfrom 10) 2) è 11 (nth (numbersfrom 10) 100) è
30 Infinite streams (define (makestream a) (if (null? a) () (cons (car a) (lambda () (makestream (cdr a)))))) (makestream (list 1 2 3)) è (1. #<Closure>) 30
31 Infinite streams (define (append x y) (if (null? x) y (cons (car x) (lambda () (append (tail x) y))))) (nth (append (makestream (list 1 2 3)) (numbersfrom 10)) 4) è 10 31
32 Infinite streams (define (interleave x y) (if (null? x) y (cons (car x) (lambda () (interleave y (tail x)))))) (nth (interleave (numbersfrom 1) (numbersfrom 10)) 5) è 3 32
33 Infinite streams (define (map f stream) (if (null? stream) () (cons (f (car stream)) (lambda () (map f (tail stream)))))) (map square (numbersfrom 10)) è (100. #<Closure>) 33
34 Infinite streams (define (filter p stream) (if (null? stream) () (if (p (car stream)) (cons (car stream) (lambda () (filter p (tail stream)))) (filter p (tail stream))))) (filter (lambda (x) (> x 100)) (numbersfrom 10)) è (101. #<Closure>) 34
35 How to represent an infinite tree? 35
36 Search strategies and infinite lists (define (depthfirst next x) (define (dfs y) (if (null? y) () (cons (car y) (lambda appends two finite lists next t is the list of the subtrees of t (dfs (list x))) (dfs (next (car y)) (cdr y))))))) 36
37 Search strategies and infinite lists (define (breadthfirst next x) (define (bfs y) (if (null? y) () (cons (car y) (lambda appends two finite lists next t is the list of the subtrees of t (bfs (list x))) (bfs (cdr y) (next (car y)))))))) 37
38 Search strategies and infinite lists Write versions of depthfirst and breadthfirst with an additional argument: a predicate to recognize solutions. Solve the Eight Queens problem. 38
39 Standard ML tutorial 39
An introduction to Scheme
An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize
More informationChapter 1. Fundamentals of Higher Order Programming
Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme
More informationSCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))
SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 +  4 00 scm> (if
More informationOrganization of Programming Languages CS3200/5200N. Lecture 11
Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages
More informationStreams and Evalutation Strategies
Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions  message passing ( define ( makeaccount balance
More informationCS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.
CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs Sexpressions cons, car, cdr Defining functions readevalprint loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its subexpressions (if any).
More informationStreams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson
Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation
More informationScheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax
Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is
More informationChapter 15 Functional Programming Languages
Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.
More informationFall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs Sexpressions cons, car, cdr Defining functions readevalprint loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationCS450  Structure of Higher Level Languages
Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.
More informationProcedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt
Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process
More information11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT
Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the
More informationStreams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004
Streams CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004 We ve already seen how evaluation order can change behavior when we program with state. Now we want to investigate how
More informationDeferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.
Deferred operations Continuations 6.037  Structure and Interpretation of Computer Programs Mike Phillips (define thecons (cons 1 #f)) (setcdr! thecons thecons) (define (runincircles l) (+
More informationScheme: Expressions & Procedures
Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University
More informationFunc%onal Programming in Scheme and Lisp
Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,
More informationCPS 506 Comparative Programming Languages. Programming Language Paradigm
CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming
More informationFunc%onal Programming in Scheme and Lisp
Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.
More informationLECTURE 16. Functional Programming
LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction
More informationFunctional Programming. Big Picture. Design of Programming Languages
Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 AddisonWesley. All rights reserved. 12 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationFunctional Programming Languages (FPL)
Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More information1.3. Conditional expressions To express case distinctions like
Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a
More informationhttps://lambda.mines.edu 1 Recursion is a beautiful way to express many algorithms, as it typically makes our algorithm easier to prove. 2 Calling a function requires space on the call stack for the variables,
More informationPrinciples of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003
Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in
More informationHIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?
HIERARCHICAL DATA 5 COMPUTER SCIENCE 61AS Concepts and Definitions 1. What is a Tree? How is it different from a Deep List? When would you use one over the other? A Tree is an Abstract Data Type composed
More informationPrinciples of Programming Languages
Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6  Defining a Programming Language Bottom Up Collaboration and Management  Elements of Programming Dana Fisman 1 What we accomplished
More informationFunctional Programming Languages (FPL)
Functional Programming Languages (FPL) 1. Definitions... 3 2. Applications... 3 3. Examples... 4 4. FPL Characteristics:... 5 5. Lambda calculus (LC)... 6 5.1. LC expressions forms... 6 5.2. Semantic of
More informationLecture #24: Programming Languages and Programs
Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from lowlevel notations, such as machine language or simple hardware
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationCSE341 Autumn 2017, Final Examination December 12, 2017
CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closedbook, closednote, etc. except for both sides of one 8.5x11in piece of paper. Please
More informationFunctional programming with Common Lisp
Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions
More informationInterpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4
CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that
More informationTail Recursion and Accumulators
Tail Recursion and Accumulators Recursion Should now be comfortable with recursion: No harder than using a loop (Maybe?) OAen much easier than a loop When processing a tree (e.g., evaluate an arithmefc
More informationIntroduction to Scheme
How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free
More informationCS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan
CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan 1 Reading Little Schemer Chapter 8 SICP 1, 3.2 2 Lecture Overview In this lecture, we will cover scheme procedures in a bit more detail
More informationFunctional Programming
Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario When the Function is the Thing In OO programming, you typically know where an action is needed,
More informationIntroduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples
Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the
More informationUserdefined Functions. Conditional Expressions in Scheme
Userdefined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment
More informationITERATORS AND STREAMS 9
ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only
More informationBoxandarrow Diagrams
Boxandarrow Diagrams 1. Draw boxandarrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))
More informationFunctional Programming Lecture 1: Introduction
Functional Programming Lecture 1: Introduction Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz Acknowledgements
More informationFunctional Programming
Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used
More informationDelayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses
CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.
More informationCSC 533: Programming Languages. Spring 2015
CSC 533: Programming Languages Spring 2015 Functional programming LISP & Scheme Sexpressions: atoms, lists functional expressions, evaluation, define primitive functions: arithmetic, predicate, symbolic,
More informationHeap storage. Dynamic allocation and stacks are generally incompatible.
Heap storage Dynamic allocation and stacks are generally incompatible. 1 Stack and heap location Pointer X points to stack storage in procedure Q's activation record that no longer is live (exists) when
More informationProblem Set 4: Streams and Lazy Evaluation
Due Friday, March 24 Computer Science (1)21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Problem Set 4: Streams and Lazy Evaluation Reading Assignment: Chapter 3, Section 3.5.
More informationProgramming Systems in Artificial Intelligence Functional Programming
Click to add Text Programming Systems in Artificial Intelligence Functional Programming Siegfried Nijssen 8/03/16 Discover thediscover world at the Leiden world University at Leiden University Overview
More informationSOFTWARE ARCHITECTURE 6. LISP
1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are
More informationScheme in Scheme: The Metacircular Evaluator Eval and Apply
Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,
More informationClass Structure. Prerequisites
Class Structure Procedural abstraction and recursion 6.037  Structure and Interpretation of Computer Programs Mike Phillips, Benjamin Barenblat, Leon Shen, Ben Vandiver, Alex Vandiver, Arthur Migdal Massachusetts
More informationLISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.
LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,
More information4/19/2018. Chapter 11 :: Functional Languages
Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen
More information4.2 Variations on a Scheme  Lazy Evaluation
[Go to first, previous, next page; contents; index] 4.2 Variations on a Scheme  Lazy Evaluation Now that we have an evaluator expressed as a Lisp program, we can experiment with alternative choices in
More informationScheme Basics > (butfirst '(help!)) ()
Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst
More informationLisp Basic Example Test Questions
2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases
More informationSTREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?
STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a consstream a special form? Practice with Streams 1. Define a procedure (ones) that, when run
More informationCOP4020 Programming Assignment 1  Spring 2011
COP4020 Programming Assignment 1  Spring 2011 In this programming assignment we design and implement a small imperative programming language MicroPL. To execute MircoPL code we translate the code to
More informationFall 2017 December 4, Scheme. Instructions
CS 61A Final Review Fall 2017 December 4, 2017 Instructions Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once everyone understands
More informationThe purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about
Lesson 0.4 Introduction to Racket Preliminaries The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about Expressions Numbers, Booleans, and
More informationLisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13
Lisp: Question 1 Write a recursive lisp function that takes a list as an argument and returns the number of atoms on any level of the list. For instance, list (A B (C D E) ()) contains six atoms (A, B,
More informationFunctional Programming  2. Higher Order Functions
Functional Programming  2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class
More informationSCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015
SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationAdministrivia. Simple data types
Administrivia Lists, higher order procedures, and symbols 6.037  Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Project 0 was due today Reminder:
More informationScheme Quick Reference
Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries
More informationCS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))
CS61A Notes 02b Fake Plastic Trees Box and Pointer Diagrams QUESTIONS: Evaluate the following, and draw a boxandpointer diagram for each. (Hint: It may be easier to draw the boxandpointer diagram first.)
More informationUMBC CMSC 331 Final Exam
UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy
More informationNotes on Higher Order Programming in Scheme. by Alexander Stepanov
by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction  it allows us to implement ADT (abstact data types) in a very special way. The issue of
More information(Func&onal (Programming (in (Scheme)))) Jianguo Lu
(Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you
More informationCS61A Notes Disc 11: Streams Streaming Along
CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever
More informationTest 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.
CSE 3302 Test 1 1. Preprocessor macros are associated with: A. C B. Java C. JavaScript D. Pascal 2. (define x (lambda (y z) (+ y z))) is an example of: A. Applying an anonymous function B. Defining a function
More informationLecture Notes on Lisp A Brief Introduction
Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham
More informationProgramming Languages
Programming Languages Tevfik Koşar Lecture  XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages
More information61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013
61A LECTURE 18 SCHEME Steven Tang and Eric Tzeng July 24, 2013 What s happening today? We re learning a new language! After you know one language (Python), learning your second (Scheme) is much faster
More informationProgramming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington
Programming Languages Thunks, Laziness, Streams, Memoization Adapted from Dan Grossman s PL class, U. of Washington You ve been lied to Everything that looks like a function call in Racket is not necessarily
More informationLazy Evaluation and Parameter Transfer
Cristian Giumale / Lecture Notes Lazy Evaluation and Parameter Transfer Scheme comes with two interesting predefined functions: delay and force. The basic purpose of delay is to postpone the evaluation
More informationScheme Quick Reference
Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete
More informationPrinciples of Programming Languages 2017W, Functional Programming
Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.
More informationFunctional Programming in Scala. Raj Sunderraman
Functional Programming in Scala Raj Sunderraman Programming Paradigms imperative programming modifying mutable variables, using assignments, and control structures such as ifthenelse, loops, continue,
More informationCS 360 Programming Languages Interpreters
CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like
More informationScheme as implemented by Racket
Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the
More informationIntroduction to Logic Programming. Ambrose
Introduction to Logic Programming Ambrose BonnaireSergeant @ambrosebs abonnairesergeant@gmail.com Introduction to Logic Programming Fundamental Logic Programming concepts Related to FP General implementation
More informationModern Programming Languages. Lecture LISP Programming Language An Introduction
Modern Programming Languages Lecture 1821 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation
More informationAn Introduction to Scheme
An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification
More informationScheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.
Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu
More informationBuilding a system for symbolic differentiation
Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)
More information6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures
Today s topics Types of objects and procedures Procedural abstractions Capturing patterns across procedures Higher Order Procedures Types (+ 5 1) ==> 15 (+ "hi 5) ;The object "hi", passed as the first
More informationCS 314 Principles of Programming Languages. Lecture 16
CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:
More informationScheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7
Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are firstclass values E.g., functions
More informationCSE 341 Section Handout #6 Cheat Sheet
Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5
More informationMacros & Streams Spring 2018 Discussion 9: April 11, Macros
CS 61A Macros & Streams Spring 2018 Discussion 9: April 11, 2018 1 Macros So far, we ve mostly explored similarities between the Python and Scheme languages. For example, the Scheme list data structure
More information