מבוא מורחב למדעי המחשב בשפת Scheme תרגול 1. Outline Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation.

Slides:



Advertisements
Similar presentations
COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
Advertisements

Intermediate Code Generation
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.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 1. Outline Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
ISBN Chapter 15 Functional Programming Languages.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Semantics (1).
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
 Computer Languages Computer Languages  Machine Language Machine Language  Assembly Language Assembly Language  High Level Language High Level Language.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Functional Programming
Operational Semantics of Scheme
Functional Programming
Functional Programming Languages
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Expressions and Assignment
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Original material by Eric Grimson
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
Programming Languages and Compilers (CS 421)
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Programming Languages and Compilers (CS 421)
6.001 SICP Further Variations on a Scheme
Extended Introduction to Computer Science
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
Assignments and Procs w/Params
Lecture 3: Rules of Evaluation CS200: Computer Science
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 1

Outline Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation Rule 2

3 תרגולים ועזרים ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס( אתר הקורס: מתרגלים מעבדה פורום (קישור מאתר הקורס)

4

5 תרגילים תרגילים שבועיים שהגשתם חובה. הכנת התרגילים היא אישית. תוכניות יוגשו יחד עם דוגמאות הרצה שלהן (רלוונטי החל מהתרגיל השני). נדרש: לפחות 80% מהתרגילים בהצלחה. הגשה בדוא"ל: לכתוב שם, ת.ז., מספר קבוצה, לצרף הצהרה חתומה ולשמור את אישור ההגשה. ערעור לבודק בכתב – לתיבת הדואר האישית. פרטים באתר הקורס.

6 Dr. Scheme התקנת התקנת המשערך (גירסא 209): התקנה בבית, דרך אתר הקורס, או מותקן במעבדה בבניין שרייבר במערכת הפעלה Linux: drscheme-209 & בהפעלה הראשונה (בלבד) Language->Choose Language-> Graphical (under PLT tab) (לאחר השינוי יש ללחוץ על כפתור ה-Run)

7

8 חלון ההגדרות חלון האינטרקציות

9 בדיקת תחביר

10 שמירת קבצים והדפסתם

11 עבודה בחלון האינטרקציות

12 עבודה בחלון ההגדרות Click on “Run”

Machine vs. High-Level Language 13 Assembly Code Binary Code MeaningHigh-Level Language Code ADD Op. code of ADD command is 35 Adds contents of the memory cell number(address) 5 to the content of cell #7 and put the results into the cell #15 X:=Y+Z (+ Y Z) JEQ Op. code of JEQ command is 48 Examines the content of the memory cell #5. If it’s equal to zero it executes command located in the memory cell #8, otherwise executes command located in the memory cell #9 If(a==0) then ….. else …… end (IF a exp1 exp2) A Compiler is a software program that translates(compiles) programs written in high-level language into machine(binary) code

Compiler vs. Interpreter CompilerInterpreter Translate the whole program into a binary code understandable by hardware Evaluate commands (or expressions) one by one. Analyze code only onceAnalyze every expression each time it is executed Recompile whole program for any change Only changed expression/command is reevaluated 14 Dr. Scheme is an interpreter

Functional vs. Imperative Programming FunctionalImperative Basic paradigm Evaluation of expressions without side effects (changes in the memory state) Result of one expression can be used as an argument to another Execution of commands that change an environment (memory state) One command change (prepare) environment for another ExampleCompute: f(n)=“n* f(n-1) n>1 or 1 if n=1” on n=5; s:=1; k:=1 Repeat 5 times: s:=s*k; k:=k+1; EffectEvaluates function n! on n=5. No changes in the environment. Change state of the variable s so it contains 5!. Also change state of the variable k. 15 Scheme is a functional programming language (not purely functional)

Why functional programming? Program Optimization –Memorize value of expression instead of repeated evaluation –Do not evaluate an expression if its value is not used –No dependency => expressions can be evaluated in different order or in parallel Languages –Lisp, Scheme, Haskell, Erlang 16

Syntax vs. Semantics SyntaxSemantics Which expression is a legal expression? How to evaluate/execute a legal expression? A list is a legal expression in Scheme language Scheme uses the “evaluation rule” to compute value of an expression 17 (# 5 7) -is syntactically correct but has no meaning (f 5 7) –meaning(value) depends on the meaning of the symbol f

Postfix, Infix, Prefix notation 18 NameDescriptionExample InfixOperator is in- between operands (5+7)*8 PostfixOperator is after the operands * PrefixOperator is before the operands * (* (+ 5 7) 8) : Scheme uses the prefix notation for arithmetic expressions

Reminder: The Scheme Language ElementExampleSemantics Simple Expression <,+,586Element’s value Means of Combination (+ 1 3 (* 5 7))Value of an expression Means of abstraction (define x 5)Associate name with a value Means of abstraction (lambda(x) (* x x))Create compound procedure (function) 19

Primitive vs. Compound Procedures Primitive procedures are part of the Scheme language (not defined using lambda expressions) –Arithmetic(+,*), IF, DEFINE Compound procedures are created by programmer using the lambda expressions – (lambda(x) (+ x 2)) 20

Some Scheme Expressions ExampleDescription (+ (- 9 7) 3) Arithmetic expression (< 5 2) (AND #t (< 10 5)) (OR #f (<10 20)) Boolean expressions (define x (+ 5 2)) Name definition (define g (lambda(a) (+a 1))) Function (compound procedure) definition (define (g a) (+ a 1)) (define (g b c) (+ b c)) Function definition using syntactic sugar (g x) ((lambda(d) (+ d 1)) 2) Function call (application of compound procedure) 21

Reminder: Syntactic Sugar Shortcuts in the syntax, do not add power to the language –Can write (define (f x) (* x x) ) –Instead of (define f (lambda(x) (* x x)) ) 22

23 Example in DrScheme

24 מספרים רציונליים ומרוכבים

25 מספרים גדולים Scheme תומך ב"מספרים גדולים", כאלה שגדולים מהמספרים המקסימליים שארכיטקטורת מחשב תומכת בהם ( > , > )

Reminder: Environment table (define x 5) associates name x to value of 5 in an environment table (define f (lambda(x)(* x x))) associates name f to the function lambda(x)(* x x) in an environment table 26 NameValue x5 flambda(x)(* x x) Environment Table >(+ x 5) 10 >(f 5) 25

Reminder: How to evaluate an expression ? An expression is usually built out of sub- expressions –(expr_0 expr_1 … exp_n) The first sub-expression evaluates to a primitive or compound procedure –(f (+ 2 3) (g 2)) Need well-defined rule to evaluate any expression 27

Remainder: Evaluation rule 1.Reduce step: Evaluate all sub-expressions (in any order) If the procedure to apply is a primitive, just do it. (+ (* 5 2) (* 5 4)) 2.Expand step(substitution model) If the procedure to apply is a compound procedure: Substitute by the body of the procedure while replacing each formal parameter with the corresponding actual arguments (define (f x) (* x x)) (f 5)-> (* 5 5) 28 What is the order of the application of step 1 and step 2? (f (* 5 2))

Normal vs. Applicative order ApplicativeNormal Step 0(f (+ 2 3) ) Step 1Reduce: (f 5)Expand: (+ (+ 2 3) 1) Step 2Expand: (+ 5 1)Reduce: (+ 5 1) Step (define (f x) (+ x 1)) Usually same value if expressions have no side effects Consider ((lambda(x y) (+ x 2)) 1 (/ 1 0)) Scheme uses Applicative order

Why Applicative order? Think of the optimization (define (f x) (* x x)) (define (g x) (sqrt x)) Normal order evaluates (sqrt 2) twice (f (sqrt 2))  (* (sqrt 2) (sqrt 2)) What would happen if sqrt produced a side effect (e.g. prints on a display)? 30

31 What is the Scheme order? (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (foo a) (sum-of-squares (+ a 1) (* a 2))) (foo 5) With DrScheme stepper*: *works for Beginning Student Language level only

Reminder: Special Forms Evaluation not according to standard rule or/and may have side effects 32 FormEffectExample (DEFINE name expr)Changes environment table(DEFINE x (+ 2 1)) (DEFINE (f x) (+ x 1)) (IF a expr1 expr2)Only one out of expr1 and expr2 is evaluated depending on the value of a (IF (= x 0) (myprint 1) (myprint 2)) (AND exp1 exp2)If exp1 is false,exp2 is not evaluated (AND (> 0 a) (< 2 (/ 1 a)) (OR exp1 exp2 exp3)If exp1 is true, exp2 is not evaluated, if exp2 is true, exp3 will not be evaluated (OR (= 0 a) (> 1 (/ 2 a))

33 דוגמא – ערך מוחלט (define (abs x) (if (< x 0) (- x) x))

34 עוד דוגמא (define (foo a b) ((if (> b 0) + -) a b)) (foo 2 6) (foo 5 -4)