;CMPU101, Spring 2013 ;Lecture #1 ; ; Reading assignment: Posted lecture notes, Part 1, pages 1-21. ; ; In my lectures this semester I will often refer to Racket and Scheme. ; Treat these names synonymously. ; ; Lecture 1: ; ; * Syllabus on-line at: ; (https://www.cs.vassar.edu/courses/cs101-201301/top) ; ; * We will use a FUNCTIONAL COMPUTER PROGRAMMING LANGUAGE (Racket) ; because: ; ; a) The programming experience of students is less variable when ; the language is new to everyone. ; ; b) This language has simple syntax and uses a default rule to ; evaluate functions that is similar to the way mathematical ; functions are evaluated. (Functions are pieces of code that ; take input and produce a single output.) ; ; * Lecture notes: Often I will post lecture notes (like these) ; that are actual Racket programs, but that are incomplete ; (because I want you to solve problems in class). Normally, ; I will post the incomplete versions before class and post a ; completed version sometime after class. ; ; You are encouraged to take notes and work on programs during ; class. Be advised that I can bring any session you are running ; on a classroom machine to display on the screen for the whole ; class to see. This may be helpful if you are getting an error ; because the whole class can participate in solving your problem. ; ; * All students who are enrolled for the course will have 24/7 ; access to OLB, using id cards after 7 pm. It may take a few ; days to get all your names on the card list, so please give me ; updates on your success/failure to get into OLB. ; ; ; ; High-Level Programming Language: ; ; Set of well-defined syntax and associated semantic rules that allow ; us to communicate with computers. ; ; - Syntax: Legal words, expressions and statements specified by a ; set of rules. ; ; The functional languages we will use have a smaller syntax set ; than do programming languages such as Java and C++. ; ; - Semantics: Meaning of legal syntax. ; ; ; All computer languages have a set of legal syntax, or legal words. ; Each language also has a correct way to use those words to create a ; program. ; ; Once you have written a program, it must be translated into a form ; that the computer can read...machine language. ; ; When you type a program into the computer, you need to think about ; both the syntax and also the meaning of the syntax, the semantics. ; Luckily for us, the computer checks much of the syntax each time it ; attempts to translate our programs, and it reports any errors it ; finds. This makes programming an iterative process, in which you ; write code that eventually contains no errors. An important part ; of this iterative process is saving the code every time you change ; it because any unsaved work will be lost if any machine problems ; occur. ; ; Rule of thumb with computers: ALWAYS EXPECT THE UNEXPECTED. ; ; The errors found during translation of programs into machine ; code include errors in both syntax and semantics. These are ; generally lumped into the category of SYNTAX ERRORS. At first, you ; may hate getting errors, but after awhile you know how to fix ; every one. Every computer programmer remembers the first time they ; got a non-trivial program to translate with no error messages!! ; ; The more insidious errors are LOGIC ERRORS (aka BUGS, a term coined ; by Admiral Grace Hopper (VC'28)). Bugs are not found during program ; translation, but they cause the program to produce the wrong result. ; This type of error requires a delicate process known as DEBUGGING. ; ; ; * A program is a combination of: ; ; 1. functions and variable definitions written and named by us ; ; 2. code written in libraries ; (functions and constant definitions written by others and ; distributed with the software, aka primitive functions) ; ; ; Programming entities in Racket (expressions): ; ; * The types of data listed in this box evaluate to themselves. ; ; 1. PRIMITIVE ENTITIES (aka DATA TYPES, VALUES, LITERALS, ATOMS): ; ; * No primitive entity can evaluate to more than one type. ; Another way to say this is that no primitive entity can ; have more than one meaning over time. ; ; a) numbers: integers, real numbers, fractions, and inexact ; real numbers, interpreted in base 10 unless otherwise ; indicated ; ; b) booleans: true (#t) and false (#f) ; ; c) quoted symbols: e.g. 'wzx, 'yeow. Any combination of ; keyboard characters, excluding spaces, and preceded by ; a single quote. ; ; d) null, empty, or '(): Each of these signify the null entity, ; also known as the empty list. ; ; e) characters: Each character literal is preceded by #\, ; so #\a is a and #\$ is $. ; ; f) primitive function (operator) names: e.g., addition, ; subtraction, multiplication, division (+, -, *, /), ; and over 200 more. ; ; g) void: the type specifying nothing as a return type. Some ; functions return nothing! ; ; ; 2. COMPOUND ENTITIES (these are also data types and also ; self-evaluating): ; ; a) strings: a sequence of characters inside matching ""s, ; shown in green text inside DrRacket. ; ; Strings are indexed sequences of characters. The characters ; in a string are numbered sequentially from left to right, ; starting at 0. For example: "Hello, world!", "CMPU101". ; ; b) images (pictures) and scenes (.png, .jpg, .gif). ; ; c) valid non-empty lists: characters enclosed in a pair of ; ()s, preceded by a single quote: ; ; I. quoted lists: E.g., '(1 2 3 4). Containers of data. ; ; II. unquoted lists: ; ; 1) function invocations: lists in which the first element ; is a function and the rest of the elements are valid ; racket entities, called inputs or arguments to that ; function. ; ; 2) special forms: lists that start with a keyword. ; The keywords dictate the way the internal elements ; of this type of non-empty list are evaluated. ; ; SPECIAL FORMS: Keywords that have special meaning ; when preceded by a left parenthesis. ; ; Some of the first keywords we will use are: ; DEFINE, LAMBDA, AND, OR, COND, IF, ELSE, and LOCAL. ; ; c) structs: containers of more than one primitive or ; compound entity. Created by defining a structure. These ; correspond to objects or records in other programming ; languages. ; ; d) vectors: A container for sequential, indexed data. Like ; arrays used in other languages (e.g., Java & C++). The ; contents of a vector are numbered sequentially from left ; to right, starting at 0. ; ; Every primitive and compound data type listed above can be used ; as input or returned as output. ; ; ; INTRODUCTION TO DRRACKET Integrated Development Environment (IDE): ; ; DrRacket is available for free download and I encourage you ; to install the software on your own machines. If you have ; difficulty installing the software, ask your prof or a coach. ; ; * Link to download DrRacket is on left side of course web page. ; ; * Inside DrRacket: ; Definitions window (top) and the Interactions window (bottom). ; ; • Interactions window (IW): used for quick entry and ; evaluation of expressions (like a calculator). ; ; • Definitions window (DW): used to type programs to save. ; Results of running programs are displayed in IW. ; ; * Choosing a Language. ; ; Sub-language we will use: ; ; "How to Design Programs" Advanced Student Language (ASL) ; ; ; ; ; Global Environment (GE) and table lookup ; ; The GE is a table with name and value pairs (often called a lookup ; table). When you open DrRacket, a GE is set up in memory. ; ; The global environment always contains a core set of entries. ; For example, ; ; the symbol + ==> the addition function ; the symbol - ==> the subtraction function ; the symbol * ==> the multiplication function ; the symbol / ==> the division function ; the symbol sqr ==> the square function ; the symbol sqrt ==> the square root function ; the symbol expt ==> the exponent function ; ... (and somewhere around 200 more) ; ; At the time you start up DrRacket, all the functions that are ; already written are stored in a lookup table (the GE). ; ; ; Evaluation of non-empty lists (==> signifies evaluation): ; ; Evaluation is the only action that is performed in the ; translation of functional languages. All programs are ; composed of non-empty lists. ; ; A non-empty list is a compound Racket entity, an ordered ; sequence of Racket entities separated by spaces and inside ; matching ()s. ; ; Valid non-empty lists: ; • (+ 3 4) ==> 7 ; function call ; • (abs (- 4 6)) ==> 2 ; function call within function call ; • (+ 6 (* 3 4)) ==> 18 ; function call within function call ; • (define age 34) ==> void ; sp form writes age and 34 to GE ; • '(3 4) ==> (3 4) or (list 3 4) ;a data container ; • (quote abc) ==> 'abc ; sp form shields argument from eval ; ; the same as ' ; ; ; The evaluation of UNQUOTED non-empty lists falls into one of ; three distinct cases: ; ; (1) Default Case: function invocations (aka function calls) ; like the first three bulleted cases shown above. ; ; (2) Special Forms: have unique evaluation and results. ; ; (3) Undefined, causing an error ; ; -------------------------------------------------------------- ; ; Default evaluation of (E1 E2 E3 ... En): E1 is function and ; E2...En are INPUTS (aka ARGUMENTS). E1 is identifier looked up ; in the GE. Then E2 ... En are each evaluated. ; ; Possible errors in what appear to be valid function calls: ; ; * the function E1 might require a different number or type ; of inputs (try the expressions below in the IW) ; ; Ex: (sqrt 7 8 9) ; (+ "hello" "world") ; (* true false) ; ; * one or more of the entities E2, . . . , En might be ; undefined ; ; Ex: (+ y 2) ;; y is not defined ; ; * the function E1 might not be in the GE. ; ; ; File extensions and their meanings: ; ; 1. Files ending with ".rkt": ; ; The files you download for labs and assignments will often have ; a .rkt extension (e.g., lab1.rkt). These files must be opened ; within DrRacket because they contain "meta-data" that cannot be ; displayed with text-only viewers. In particular, if the file ; contains any images or comment boxes, these cannot be properly ; displayed as text. ; ; 2. Files ending with ".txt": ; ; Plain text files can be opened in DrRacket's DW and, if the text ; they contain follows the rules of the language, will run as they ; are written. ; ; The advantage to using files with ".txt" extensions is that ; the files can be read through a browser. The down side is that ; ".txt" files can't contain images or comment boxes whereas ; ".rkt" files can. ; ; 3. Files ending with ".rkt~": ; ; The ~ at the end of a .rkt file extension indicates the file is ; an old version. DrRacket always keeps the last saved .rkt file ; as a backup when you create a new one. ;