12
CSE 130 : Fall 2008 Programming Languages Lecture 1: Hello, world. Lecture 1: Hello, world. Ranjit Jhala UC San Diego A Programming Language A Programming Language • Two variables –x,y Three operations L1: x++; y--; Three operations – x++ x-- (y=0)?L2:L1 L2: x (x=0)? L1:L2; L2: Fact: This language is “equivalent” to every PL! Try writing quicksort … or Windows, Google,… in this language. So why study PL ? So why study PL ? A different language is a different vision of life” F d i F lli i - Federico Fellini

CSE 130 : Fall 2008 1: Hello, world. Ranjit Jhala UC San Diegocseweb.ucsd.edu/classes/fa08/cse130/lectures/lec1.pdfCSE 130 : Fall 2008 Programming Languages Lecture 1: Hello, world

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

  • CSE 130 : Fall 2008

    Programming Languages

    Lecture 1: Hello, world.Lecture 1: Hello, world.

    Ranjit JhalaUC San Diego

    A Programming LanguageA Programming Language• Two variables

    – x,y

    • Three operations

    L1: x++;y--;Three operations

    – x++– x--

    (y=0)?L2:L1

    L2:x– (x=0)? L1:L2;

    L2: …

    Fact: This language is “equivalent” to every PL!Try writing quicksorty g q… or Windows, Google,… in this language.

    So why study PL ?So why study PL ?

    “A different language is a ff g gdifferent vision of life”

    F d i F lli i- Federico Fellini

  • So why study PL ?So why study PL ?

    Programming language

    shapesshapes

    P i th htProgramming thought

    So why study PL ?So why study PL ?

    PL characteristics affects how: • Ideas are expressed• Computation is expressed

    Course GoalsCourse Goals

    “Free your mind”-Morpheus

    Course Goals: LearnCourse Goals: Learn

    CLanguages/ConstructsNew ways to:New ways to:- describe- organize- think about think about computation

  • Enables you toEnables you to

    Write software that’sWrite software that s•Readable •Correct•Extendable• Modifiable• Modifiable•Reusable

    Enables you to learn new PLsEnables you to learn new PLs

    No Java (C#) 10 (5) years agoNo Java (C#) 10 (5) years ago• AJAX? Python ? Ruby ? F# ?

    Learn the anatomy of a PL • Fundamental building blocks• Fundamental building blocks• Different guises in different PLs

    Re-learn the PLs you already know

    Enables you to design new PLsEnables you to design new PLs…“who, me ?”,

    Buried in every extensible system is a PL Buried in every extensible system is a PL • Emacs: E-Lisp

    W d P i t M VBS i t• Word, Powerpoint: Macros, VBScript• Quake: QuakeC (Game Scripting)• Facebook: FBML• SQL, Renderman, LaTeX, XML …SQL, Renderman, LaTeX, XML …

    Enables you to choose right PLEnables you to choose right PL

    “ but isn’t that decided by …but isn t that decided by • libraries,

    t d d • standards, • and my boss ?”yYes.

    My goal: educate tomorrow’s tech leaders & bosses so you’ll make informed choices & bosses, so you ll make informed choices

  • MechanicsMechanics

    www cs ucsd edu/classes/fa08/cse130/www.cs.ucsd.edu/classes/fa08/cse130/

    Nothing printed, everything on Webpage!

    Meetings:• Lectures: Ranjit Jhala, Tu-Th 5:00-6:20pm @ WLH 2204• Section: William Matthews, W 4:00-4:50 pm @ Peterson104• Office Hours: RJ (Th 1-3p), WM (W 10-12)

    – Hours posted on web page (may change a bit)

    MaterialMaterial

    Outline: Outline: 1. Functional, OCaml, 5 weeks2 OO Python 3 weeks2. OO, Python, 3 weeks3. Logic, Prolog, 1 week

    No recommended Text: • Online lecture notes• Resources posted on webpagep p g• Pay attention to lecture and section!

    Requirements and GradingRequirements and Grading• The good news:

    – No Homework – No Midterm …

    • Weekly Quiz (best 6 of 9): 30%• Programming Assignments (8): 40%• Final: 30%

    Grading on a curve. Two hints/rumors:1. Lot of work2. Don’t worry (too much) about grade

    Weekly QuizWeekly Quiz

    • Start of every Thursday lecturey y

    • Best six will be considered for your grade– Some easier than others, give each your best shot

    Based on lectures section of previous Tue Thu• Based on lectures, section of previous Tue, Thu

    • Recommended ungraded HW problems posted • Recommended, ungraded, HW problems posted on previous Thursday are sample quiz questions

    • Webpage has samples for next Thu (10/9) quiz

  • Weekly Programming AssignmentsWeekly Programming AssignmentsSchedule up on webpage

    Due on Friday 5 PM

    Deadline Extension:– Four “late days”, used as “whole unit”– 5 mins late = 1 late day5 mins late 1 late day– Plan ahead, no other extensions

    PA #1 online, due 10/10, 5:00 PM

    Weekly Programming AssignmentsWeekly Programming Assignments

    Unfamiliar languages+ Unfamiliar environments

    Start Early!

    Weekly Programming AssignmentsWeekly Programming Assignments

    Scoring St le Test s iteScoring = Style + Test suite

    No Compile, No Score

    Weekly Programming Assignments

    Forget Java C C++

    Weekly Programming Assignments

    Forget Java, C, C++ …… other 20th century PLs… other 20 century PLs

    Don’t complain that Ocaml is hard… that Ocaml is hard

    … that Ocaml is retarded

  • Immerse yourself in new languageImmerse yourself in new language

    It is notIt is not.

    Free your mind.

    Word from our sponsor Word from our sponsor …• Programming Assignments done ALONE

    • We use plagiarism detection softwareWilliam is an expert– William is an expert

    – Have code from all previous classesMOSS is fantastic plagiarize at your own risk– MOSS is fantastic, plagiarize at your own risk

    Z T l• Zero Tolerance– offenders punished ruthlessly

    • Please see academic integrity statement

    Enough with the small talkEnough with the small talk

  • Say hello to OCamlSay hello to OCamlvoid sort(int arr[], int beg, int end){if (end > beg + 1){int piv = arr[beg];int piv = arr[beg];int l = beg + 1;int r = end;while (l != r-1){

    if(arr[l] []

    l++;else

    swap(&arr[l], &arr[r--]);}if(arr[l]

    let (l,r)= List.partition ((

  • ML: History VariantsML: History, Variants“Meta Language”D i d b R bi MilDesigned by Robin MilnerTo manipulate theorems & proofs

    Several dialects:Standard ML (SML)• Standard ML (SML)– Original syntax

    • Objective Caml: (Ocaml) Objective Caml: (Ocaml) – “The PL for the discerning hacker”– State-of-the-art, extensive library, tool, user support

    • F# (Ocaml+.NET) released in Visual Studio

    ML’s holy trinityML s holy trinityExpression Value

    TType

    • Everything is an expression• Everything has a valuey g• Everything has a type

    Interacting with MLInteracting with ML“Read-Eval-Print” Loop

    Repeat:Repeat:1. System reads expression e2 S l l 2. System evaluates e to get value v3. System prints value v and type t

    What are these expressions values and types ?What are these expressions, values and types ?

    Base type: IntegersBase type: Integers2 2

    2+2 4

    2 * (9+10) 38

    int

    2 * (9+10) -12 26

    Complex expressions using “operators”:(why the quotes ?)

    int

    p p g p ( y q )• +, -, * • div, moddiv, mod

  • Base type: StringsBase type: Strings

    “ab” “ab”

    “ab” ^ “xy” “abxy”

    string

    Complex expressions using “operators”:(why the quotes ?)p p g p ( y q )

    • Concatenation ^

    Base type: BooleansBase type: Booleanstrue true

    false false

    1 < 2 true

    “aa” = “pq” false

    (“aa” = “pq”) && (18); (4,false)

    int * bool

  • Complex types: Product (tuples)Complex types: Product (tuples)

    (9-3,“ab”^“cd”,(2+2 ,7>8)) (6, “abcd”,(4,false))(9 3, ab cd ,( , 8)) (6, abcd ,(4,false))

    (int * string * (int * bool))(int * string * (int * bool))

    • Triples,…p ,• Nesting:

    – Everything is an expression– Everything is an expression– Nest tuples in tuples

    Complex types: ListsComplex types: Lists[]; [] ’a list

    [1;2;3]; [1;2;3] int list

    [1+1;2+2;3+3;4+4]; [2;4;6;8] int list

    [“a”;“b”; “c”^“d”]; [“a”;“b”; “cd”] string list

    [(1,“a”^“b”);(3+4,“c”)]; [(1 “ab”);(7 “c”)] (int*string) list[(1, a b );(3+4, c )]; [(1, ab );(7, c )] (int string) list

    [[1];[2;3];[4;5;6]]; (int list) list[[1];[2;3];[4;5;6]];

    • Unbounded size• Can have lists of anything (e.g. lists of lists)Can have lists of anything (e.g. lists of lists)• but …

    Complex types: ListsComplex types: Lists

    [1; “pq”];

    All l t t h t

    [1; pq ];

    All elements must have same type

    Complex types: ListsComplex types: ListsList operator “Cons” ::

    [1] int list1::[];

    1::[2]; [1;2;3] int list

    “a”::[“b”;“c”]; [“a”;“b”;“c”] string list

    1::[“b”; “cd”];Can only “cons” element to a list of same type

  • Complex types: ListsComplex types: Lists

    List operator “Append” @List operator Append

    int list[1;2]@[3;4;5];

    @

    [1;2;3;4;5]

    [“a”]@[“b”]; [“a”;“b”] string list

    []@[1]; [1] string list

    1 @ [2;3];Can only append two lists @ [ ; ];y pp

    [1] @ [“a”;“b”];… of the same type

    Complex types: ListsComplex types: Lists

    List operator “head” hdList operator head

    inthd [1;2]; 1

    hd ([“a”]@[“b”]); “a” string

    hd [];Only take the head a nonempty list []y p y

    Complex types: ListsComplex types: Lists

    List operator “tail” tlList operator tail

    int listtl [1;2;3]; [2;3]

    tl ([“a”]@[“b”]); [“b”] string list

    tl [];Only take the tail of nonempty list []y p y

    Recap: Tuples vs Lists ?Recap: Tuples vs. Lists ?What’s the difference ?• Tuples:

    – Different types, but fixed number:“ b d”

    • pair = 2 elts

    (3, “abcd”) (int * string)

    (3, “abcd”,(3.5,4.2)) (int * string * (float* float))• triple = 3 elts

    • Lists:– Same type, unbounded number:

    S t

    [3;4;5;6;7] int list

    • Syntax: – Tuples = comma Lists = semicolon

  • So far a fancy calculatorSo far, a fancy calculator…

    what do we need next ? … what do we need next ?