1 / 41

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

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 1. Outline. Administration Dr. Scheme Functional vs. Imperative Programming Compiler vs. Interpreter Evaluation Rule Scope. תרגולים ועזרים. ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס ( אתר הקורס: www.cs.tau.ac.il/~scheme מתרגלים מעבדה

lawson
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. תרגולים ועזרים • ספר הקורס (ניתן לקריאה ברשת, קישור מאתר הקורס( • אתר הקורס: www.cs.tau.ac.il/~scheme • מתרגלים • מעבדה • פורום (קישור מאתר הקורס)

  4. http://www.cs.tau.ac.il/~scheme

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

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

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

  8. בדיקת תחביר

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

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

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

  12. Machine vs. High-Level Language A Compiler is a software program that translates(compiles) programs written in high-level language into machine(binary) code

  13. Compiler vs. Interpreter Dr. Scheme is an interpreter

  14. Functional vs. Imperative Programming Scheme is a functional programming language (not purely functional)

  15. 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 (# 5 7) -is syntactically correct but has no meaning (f 5 7) –meaning(value) depends on the meaning of the symbol f

  17. Postfix, Infix , Prefix notation (* (+ 5 7) 8) : Scheme uses the prefix notation for arithmetic expressions

  18. Reminder: The Scheme Language

  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

  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. Example in DrScheme

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

  24. מספרים גדולים • Scheme תומך ב"מספרים גדולים", כאלה שגדולים מהמספרים המקסימליים שארכיטקטורת מחשב תומכת בהם ( > 232-1, > 264-1)

  25. 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 Environment Table >(+ x 5) 10 >(f 5) 25

  26. 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 • Reduce step: Evaluate all sub-expressions (in any order) If the procedure to apply is a primitive, just do it. (+ (* 5 2) (* 5 4)) • 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) What is the order of the application of step 1 and step 2? (f (* 5 2))

  28. Normal vs. Applicative order (define (f x) (+ x 1)) • Usually same value if expressions have no side effects • Consider ((lambda(x y) (+ x 2)) 1 (/ 1 0))

  29. 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. 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*: Applicative Order !! *works for Beginning Student Language level only

  31. Reminder: Special Forms Evaluation not according to standard rule or/and may have side effects

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

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

  34. Evaluation of An Expression The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment • To Evaluate a combination: (other than special form) • Evaluate all of the sub-expressions in any order • Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values. Do not substitute for occurrences that are bound by an internal definition.

  35. Scoping • We have seen two methods to introduce names into a program: • Define form introduces a name associated with a procedure or an expression: (define x 5) (define (f x) (* x x)) • Lambda expression introduces names for formal parameters (define (g a b) (+ a b)) ((lambda(a b) (+ a b)) 2 3) • Need rules to interpret names. (define x 5) ((lambda(x)(+ x 1)) x)

  36. Bounded variables • A procedure definition binds its formal parameters (their names) • These names are called bounded variables. • Other variables are free variables (define y 7) (define (foo x) (+ xy)) x is bounded to foo, while y is free

  37. Scope • The set of expressions for which binding defines a name is called a scope of this name • A bound variable has its binding function’s body as a scope, and is unknown outside its scope • Free variables are known anywhere in the program • For variables with the same name and overlapping scopes, the more internal binding overrides the other

  38. Example (define y 7) (define z 5) (define (foo x z) (+ x z y)) • Foo binds x and z. x and z are bounded to foo • Scope of x and z is the body of foo • y is known everywhere, z=5 is only outside foo • The meaning of foo will not change if we’ll change the names of its formal parameters.

  39. Internal Definitions • Can define a name inside the lambda expression (local to this procedure/lambda expression) • It’s scope is limited to the rest of the body of the procedure • (define (g x) (define a 5) (* x a)) • Note: Body of a procedure(lambda expression) can consist of multiple sub-expressions. • The value of the last one is defined to be the value of the procedure

  40. Evaluation of An Expression The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment • To Evaluate a combination: (other than special form) • Evaluate all of the sub-expressions in any order • Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values. Do not substitute for occurrences that are bound by an internal definition.

More Related