Functional Programming
|
|
- Anthony Patterson
- 6 years ago
- Views:
Transcription
1 14 Functional Programming Exercises 14.1 a. 5 5 = 25 b. ((λy.3 + y + z)2) = z = 5 + z c. (λv.(λw.w)(y(λz.z))) 14.2 The results are the same. In part b, where there are two beta reductions, lazy evaluation gives a different order: ((λx.x+2+z)3) = 3+2+z = 5+z Evaluate the following expressions using your Scheme interpreter: (a) #t (b) #f (c) a (d) ((b c) d e) (e) (b c) 14.4 (sum ) = (+ 1 (sum )) = (+ 1 (+ 2 (sum 3 4 5))) = (+ 1 (+ 2 (+ 3 (sum 4 5)))) = (+ 1 (+ 2 (+ 3 (+ 4 (sum 5))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 (sum ()))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 0)))))... = (define (elements lst) (if (null? lst) 0 (if (list? (car lst)) (+ (elements (car lst)) (elements (cdr lst))) (+ 1 (elements (cdr lst))) 63
2 FUNCTIONAL PROGRAMMING 14.6 (m-expression (plus (times (variable a) (variable b)) (value 2)) ((a 2) (b 4))) = (+ (* (m-expression (variable a) ((a 2) (b 4))) (m-expression (variable b) ((a 2) (b 4)))) (value 2)) =... = (+ (* 2 4) 2) = (+ 8 2) = Add the following to the case before the else in applybinary: ((lt) ((le) ((eq) ((ne) ((gt) ((ge) (< leftval rightval)) (<= leftval rightval)) (= leftval rightval)) (not (= leftval rightval))) (> leftval rightval)) (>= leftval rightval)) 14.8 (define (m-assignment statement state) (let ((target (car statement)) (source (cdr statement))) (onion target (m-expression source state) state) )) 14.9 Add the following case before the else in m-expression: ((not) (not (m-expression (cadr expr) state))) Below is a sketch of the essentials of a static type checker for Clite. First, assume the abstract syntax of program and declarations to be: ; Program = (declarations block) ; Declarations = ((v1 t1) (v2 t2)...) ; where vi = variable identifier ; ti = type, i.e., int, bool, float, or char) Because Declarations already has the structure of a Clite TypeMap, a separate typing function to create a TypeMap is unnecessary. Then it is only necessary to implement the typeof function and the various validity functions V. For example: (define (v-program p) (let ((tm (car p)) (body (cadr p))) (and (v-decls tm) (v-stmt body tm)) )) (define (v-decls tm) (if (null? tm) #t (let ((d (caar tm)) (tail (cdr tm))) (if (isin? d tail) #f
3 65 (v-decls tail) )))) (define (isin? id lst) (if (null? lst) #f (if (equal? id (caar lst)) #t (isin? id (cdr lst)) ))) Here, e.g., is the validity check for assignment: (define (v-assign s tm) (let ((target (cadr s)) (source (caddr s))) (and (isin? target tm) (v-expr source tm) (equal? (get target tm) (typeof source tm)) ))) Note that because a state and a type map have the same structure, we can use our get function on a TypeMap to retrieve a type. Here are the beginnings of a function typeof: (define (typeof e tm) (let ((kind (car e)) (opnd (cadr e))) (case kind ((value) (boolean? opnd)) ((variable) (get opnd tm)) ((plus minus times div) (typeof opnd)) (else bool) ))) Most of this is summarized in Section , and many of the missing pieces are included in earlier exercises. The challenge here is to complete the implementation of the Clite lnterpreter in Scheme and then demonstrate its completeness by interpreting a variety of abstract Clite programs Using a caret to denote exponentiation, add the following case to diff: ((^) (if (equals x u) (list * v (list ^ x (list - v 1))) (list * v (list * (list ^ u (list - v 1)) (diff x u))))) Here are the results: a. > (diff x (+ (^ x 2) (+ (* 2 x) 1))) (+ (* 2 (^ x (- 2 1))) (+ (+ (* 2 1) (* x 0)) 0)) b. > (diff x (/ (- (* 5 x) (* 2 y)) (+ (^ x 2) 1))) (/ (- (* (+ (^ x 2) 1) (- (+ (* 5 1) (* x 0)) (+ (* 2 0) (* y 0)))) (* (- (* 5 x) (* 2 y)) (+ (* 2 (^ x (- 2 1))) 0))) (* (- (* 5 x) (* 2 y)) (+ (^ x 2) 1)))
4 FUNCTIONAL PROGRAMMING The rules can be as extensive as desired; here will we consider only the two rules given as part of the problem, plus mutltiplfication by 0: a. x + 0 = 0 + x = x x 1 = 1 x = x x 0 = 0 x = 0 b. (define (simpl expr) (if (not (list? expr)) expr (let ((op (car expr)) (u ((simpl cadr expr))) (v ((simpl caddr expr)))) (case op ((+) (if (equal? u 0) v (if (equal? v 0) u (list + u v)))) ((*) (if (or (equal? u 0) (equal? v 0)) 0 (if (equal? u 1) v (if (equal? v 1) u (list * u v))))) (else (list op u v) )))) c. c1 + c2 = c3 where c3 = c1 + c2 c1 c2 = c4 where c4 = c1 c2 d. (define (constarith expr) (if (not (list? expr)) expr (let ((u (constarith (cadr expr))) (v (constarith (caddr expr)))) (if (or (not (number? u)) (not (number? v))) expr (case (car expr) ((+) (+ u v)) ((*) (* u v)) (else (list (car expr) u v)) ))))) e. (define (simplify expr) (let ((se (constarith (simpl expr)))) (if (equal? expr se) expr (simplify se) ))) a. No solution prints. b. The solution is: ( ) c. The solution is: ( ). Backtracking is required.
5 The Knights tour solution is in the file knight.scm in the ch14code directory The function try expects the initial knight position to be given, e.g.: (try ((3 3))) The board is kept in reverse order; the current position of the knight is at the front of the list. Each knight position is given as a list of row, column entries; in the example above, the knight is at row 3, column 3. A move is kept as an integer from 1 to the length of trial, which is a list of knight moves relative to its current position. This implementation required a slight modification to the function trywh Evaluate the following expressions using your Haskell interpreter: a. 3 b. Error: index (5) too large. c. 1 d. [2,3,4,5] a. [( Jane, ),( Bob, ),( Allen, ),( Bob, )] b. [( Allen, ),( Bob, )] deleteentry :: Person -> Phonebook -> Phonebook deleteentry p pb = [entry entry <- pb, fst(entry) /= p] elements [] = 0 elements (x:xs) = 1 + elements(xs) The file Clite.hs in the ch14code directory is the skeleton of a Clite interpreter in Haskell. It contains the definitions in Section as well as the following function. m (Block b) state b == [] = state otherwise = m (Block (tail b)) (m (head b) state) The file Clite.hs in the ch14code directory is the skeleton of a Clite interpreter in Haskell (including int and bool values, but not char and float). It contains the following expansion of the eval function on page 403, along with a new apply function:
6 FUNCTIONAL PROGRAMMING eval :: Expression -> State -> Value eval (Var v) state = get v state eval (Lit v) state = v eval (Binary op e1 e2) state = apply op (eval e1 state) (eval e2 state) apply :: Op -> Value -> Value -> Value apply "+" (Intval a) (Intval b) = (Intval (a + b)) apply "-" (Intval a) (Intval b) = (Intval (a - b)) apply "*" (Intval a) (Intval b) = (Intval (a * b)) apply "/" (Intval a) (Intval b) = (Intval (a div b)) apply "<" (Intval a) (Intval b) = (Boolval (a < b)) apply "<=" (Intval a) (Intval b) = (Boolval (a <= b)) apply "==" (Intval a) (Intval b) = (Boolval (a == b)) apply "!=" (Intval a) (Intval b) = (Boolval (a /= b)) apply ">=" (Intval a) (Intval b) = (Boolval (a >= b)) apply ">" (Intval a) (Intval b) = (Boolval (a > b)) apply "&&" (Boolval a) (Boolval b) = (Boolval (a && b)) apply " " (Boolval a) (Boolval b) = (Boolval (a b)) The file Clite.hs in the ch14code directory contains the following expanded definition of Expression given on page 403, along with expanded definitions of eval and apply given above, and the new function applyunary: data Expression =... Unary Op Expression deriving (Eq, Ord, Show) eval (Unary op e) state = applyunary op (eval e state) applyunary :: Op -> Value -> Value applyunary "!" (Boolval a) = (Boolval (not a)) Here is a summary: Let s = [("x", (Intval 5)), ("y", (Intval 3)), ("z", (Intval 1))] Then eval (Binary "+" (Var "y") (Lit (Intval 2))) s = apply "+" (eval (Var "y") s) (eval (Lit (Intval 2)) s) = apply "+" (get "y" s) (eval (Lit (Intval 2)) s) = apply "+" (Intval 3) (Intval 2) = Intval 3+2 = Intval length [] = 0 length (w:ws) = 1 + length ws a. fibslow(25) delivers the result in about 20 seconds. fibslow(50) may still be running as you read this solution! The reason for this
7 69 performance is that fibslow(n) recalculates fibslow(k) twice for every value of k < n. (b) This recalculation is eliminated by fibfast, which computes each number in the sequence only once, and then picks the last number in the sequence at the end of this single chain of calls. Thus, fibfast(25) delivers the result immediately, and fibfast(50) delivers the result immediately See the file simplify.hs in the directory ch14code, which implements the answer given in Exercise 14.14a Two Haskell files that solve modest Sudoku puzzles are given in the ch14code directory. These programs assume that the Sudoku game board squares are numbered from left to right from 0 to 80. So row 0 = [0..9] and col 0 = [0,9,18,27,36,45,54,63,72]. Given that we can compute these, then the board values in row 0 are: [ board!!i i <- row 0 ] Thus, we can use notelem to determine whether or not a trial move is safe. The basic strategy in these programs mimics that of the Eight Queens problem. Instead of backtracking, we keep a list of boards and each board has the same number of zeros (designating empty slots). Proceeding bottom-up and using set theory, we first substitute for a single board bd and a single zero index z, all values from 1 to 9, replicating the entire board each time that the substituion is safe: replaceone z bd = [ replace z v bd bd!!z == 0, v <- [1..9], v safe at position z ] Moving up a level, we substitute for position z for all boards b in the generated list bs. replaceall z bs = [ replaceone z b b <- bs ] To solve for all zeros, we must use recursion: solve bs [ ] = bs solve bs z:zs = solve (replaceall z bs) zs Thus, a puzzle is solved by the function: sudoku bd = solve [bd] (zeros bd)
8 FUNCTIONAL PROGRAMMING There is only one remaining problem. The replaceall function generates an extra [ ], giving [[[Int]]] instead of the desired [[Int]]. Two possible solutions for this problem are: a. Combine replaceone and replaceall into one function. This is shown by the function add in the program sudoku1.hs b. Flatten the lists. This is shown in the program sudoku2.hs To solve a sample puzzle, load one of these programs into the Haskell interpreter and then make the call: sudoku easy1 at the prompt. To solve other puzzles than easy1, change the argument to this call Some of the abstract syntax for Clite is modeled in Haskell the file Clite.hs, which has solutions to Exercises Use Appendix A for a complete definition of the Clite type checking functions. The Clite type map can be modeled as follows: type Typemap = [(Variable, Type)] type Variable = String type Type = "int" "boolean" "char" "float" The function typing needs Haskell definitions of the abstract concepts of Declaration and Program, and can be defined as a function. All the validity functions can be defined using these definitions together with a complete Haskell recursive data type definition of Clite abstract syntax Most of this is summarized in Section , along with the file Clite.hs which has solutions to Exercises The challenge here is to complete the implementation of the Clite lnterpreter in Haskell and then demonstrate its completeness by interpreting a variety of abstract Clite programs. Use Appendix A for a complete definition of the Clite meaning functions This problem is reminiscent of a classical exercise in functional programming where students built a Scheme interpreter in Scheme. Usually a small subset of the Scheme function library was implemented, including functions cons, car, cdr, conditional expressions, and function definition and call. Fortunately, Scheme syntax is simple, and can be defined in just a few BNF rules. Tokens in Scheme are either parentheses, symbols, or numbers. Thus, the abstract syntax is also simple. Implementing the run-time semantics of Scheme in Java requires the design of a read-eval-print loop. At each iteration, the user either calls or defines a Scheme function and
9 71 then the system evaluates (or stores) it and displays the result, returning to the user with a prompt to begin the next iteration. Dealing with Haskell syntax for this exercise is a bit more cumbersome, since the syntax is more complex. Moreover, the TokenStream class will be more difficult to implement due to a greater variety of operators and reserved words. If Haskell is chosen as the language for this exercise, we recommend that students limit their attention to a subset that has only a few arithmetic operators and the ability to define recursive functions on them. Anything more ambitious risks becoming tedious and unrewarding.
10 FUNCTIONAL PROGRAMMING
Functional 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 sub-expressions (if any).
More informationCSc 520 Principles of Programming Languages
CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian
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 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 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 informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More informationIt is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
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 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 informationCSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.
CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:
More informationNormal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?
Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated
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 first-class values E.g., functions
More informationA Brief Introduction to Scheme (II)
A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data
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 information6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson
6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator
More informationUsing Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add
Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1
More informationCS61A Midterm 2 Review (v1.1)
Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the
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 informationAn Explicit-Continuation Metacircular Evaluator
Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information
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 informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationThese notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without
These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create
More informationWhy do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.
.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationDocumentation for LISP in BASIC
Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have
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 informationIntroduction to Functional Programming in Haskell 1 / 56
Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order
More informationCSC 533: Programming Languages. Spring 2015
CSC 533: Programming Languages Spring 2015 Functional programming LISP & Scheme S-expressions: atoms, lists functional expressions, evaluation, define primitive functions: arithmetic, predicate, symbolic,
More informationChapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)
Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical
More informationIntermediate Representations
Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address
More informationShell programming. Introduction to Operating Systems
Shell programming Introduction to Operating Systems Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number
More informationLecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.
Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)
More informationHaske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell
Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationObject-Oriented Programming
13 Object-Oriented Programming Exercises 13.1 Using Java as an example: 13.2 Code reuse: inhertiance, interfaces. In the case of an interface, any class implementing the Comparable interface can be sorted
More informationJim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4
Jim Lambers ENERGY 211 / CME 211 Autumn Quarter 2008-09 Programming Project 4 This project is due at 11:59pm on Friday, October 31. 1 Introduction In this project, you will do the following: 1. Implement
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More informationFunctional Languages. Hwansoo Han
Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm
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 informationSCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017
SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 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 informationIntroduction to lambda calculus Part 3
Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that
More informationCPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia
CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia This is a closed book exam; no notes; no calculators. Answer in the space provided. There are 8 questions on 14 pages,
More informationSCHEME AND CALCULATOR 5b
SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 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 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 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 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 Micro-PL. To execute Mirco-PL code we translate the code to
More informationSCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012
SCHEME COMPUTER SCIENCE 6A October 29th, 202 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, we will eventually
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler
More informationLecture 19: Functions, Types and Data Structures in Haskell
The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming
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 informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationAnnouncements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop
Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00
More informationHaskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder
Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted
More informationSpring 2018 Discussion 7: March 21, Introduction. 2 Primitives
CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 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
More informationFall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 207 Discussion 7: October 25, 207 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 informationCS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called
CS109A ML Notes for the Week of 1/16/96 Using ML ML can be used as an interactive language. We shall use a version running under UNIX, called SML/NJ or \Standard ML of New Jersey." You can get SML/NJ by
More information6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter
6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,
More informationPROGRAMMING IN HASKELL. Chapter 2 - First Steps
PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited
More informationScheme: Strings Scheme: I/O
Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of
More informationControl Structures. CIS 118 Intro to LINUX
Control Structures CIS 118 Intro to LINUX Basic Control Structures TEST The test utility, has many formats for evaluating expressions. For example, when given three arguments, will return the value true
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 informationDecaf Language Reference Manual
Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented
More informationSemantics. Semantics
Semantics Semantics Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: Operational Semantics The meaning attached by compiling using compiler
More informationCS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College
CS5 Programming Languages Handout # 9 Prof. Lyn Turbak March, 00 Wellesley College Postfix: A Simple Stack Language Several exercises and examples in this course will involve the Postfix mini-language.
More informationCSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8
Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define
More informationCSE Programming Languages Midterm - Winter 2010
CSE 341 - Programming Languages Midterm - Winter 2010 Your Name: Open book and notes. No laptop computers, PDAs, internet-equipped cellphones, or similar devices. Please answer the problems on the exam
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 informationCommon LISP Tutorial 1 (Basic)
Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)
More informationParsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau
Parsing a primer Ralf Lämmel Software Languages Team University of Koblenz-Landau http://www.softlang.org/ Mappings (edges) between different representations (nodes) of language elements. For instance,
More informationStructure and Interpretation of Computer Programs
CS 6A Spring 203 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
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 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 informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
More informationSemantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?
Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate
More informationCPS 506 Comparative Programming Languages. Syntax Specification
CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens
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 informationCSC312 Principles of Programming Languages : Type System. Copyright 2006 The McGraw-Hill Companies, Inc.
CSC312 Principles of Programming Languages : Type System Ch. 6 Type System 6.1 Type System for Clite 6.2 Implicit Type Conversion 6.3 Formalizing the Clite Type System Type System Type? Type error? Type
More informationHaskell 98 in short! CPSC 449 Principles of Programming Languages
Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has
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 informationCSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp http://www.cs.washington.edu/341/ Lists (list expr2... exprn) '(value1 value2...
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 informationPrinciples of Programming Languages
Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,
More information1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,
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 informationfor (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }
Ex: The difference between Compiler and Interpreter The interpreter actually carries out the computations specified in the source program. In other words, the output of a compiler is a program, whereas
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell: Declaring Types and Classes Dr. Hyunyoung Lee 1 Outline Declaring Data Types Class and Instance Declarations 2 Defining New Types Three constructs for defining types:
More informationPolymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy
06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic
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 informationCompilers. Compiler Construction Tutorial The Front-end
Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers
More informationLecture 5: Lazy Evaluation and Infinite Data Structures
Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a
More informationExample Scheme Function: equal
ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to
More informationCA4003 Compiler Construction Assignment Language Definition
CA4003 Compiler Construction Assignment Language Definition David Sinclair 2017-2018 1 Overview The language is not case sensitive. A nonterminal, X, is represented by enclosing it in angle brackets, e.g.
More informationTyped Racket: Racket with Static Types
Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type
More informationComp 311: Sample Midterm Examination
Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief
More information