Docsity

CMSC330 Midterm 1 Notes, Study notes of Programming Methodologies

Toggle sidebar
bg1
CMSC330 Midterm I
• Syntax: keywords, formatting expectations, and “grammar” for the language
• Semantics: defines what the program means and does
• Operational semantics: semantics written in math
• Paradigm: chooses which way to do it out of the various options
• Imperative language (procedural/van Neumann): building blocks are procedures and statements
• Functional (applicative) languages: favors immutability and high-order functions
• Immutability: variables are never ref-defined so new variables a function of old ones
• Higher-order functions: passed as arguments and returned as results
• Logic-programming languages (rule-based/constraint-based): program rules constrain possible
results
• Object-oriented languages: programs built from objects
• Dynamic (scripting) languages: rapid prototyping languages for common tasks
• For implementation, two broad ways:
• Compilation: source program translated to another language
• Interpretation: executes each instruction in source program one step at a time
Ruby
• Ruby: object-oriented, dynamically typed language
• No declaration of variables as recognized automatically = implicit declaration
• In command line, to run script
• ruby -w path/file.rb
• implicit vs. explicit
Implicit Explicit
Rapidly type code Would automatically complain about errors in
variable declaration
• static type vs dynamic type
Static type checking (C, Java) Dynamic type checking(Ruby)
Cannot use certain types throws errors
automatically
More possibility for mistakes
• control statements: one that affects which instruction is executed next
• while loops
■Example:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CMSC330 Midterm 1 Notes and more Study notes Programming Methodologies in PDF only on Docsity!

CMSC330 Midterm I

  • Syntax: keywords, formatting expectations, and “grammar” for the language
  • Semantics: defines what the program means and does
    • Operational semantics: semantics written in math
  • Paradigm: chooses which way to do it out of the various options
  • Imperative language (procedural/van Neumann): building blocks are procedures and statements
  • Functional (applicative) languages: favors immutability and high-order functions
    • Immutability: variables are never ref-defined so new variables a function of old ones
    • Higher-order functions: passed as arguments and returned as results
  • Logic-programming languages (rule-based/constraint-based): program rules constrain possible results
  • Object-oriented languages: programs built from objects
  • Dynamic (scripting) languages: rapid prototyping languages for common tasks
  • For implementation, two broad ways:
    • Compilation: source program translated to another language
    • Interpretation: executes each instruction in source program one step at a time Ruby
  • Ruby: object-oriented, dynamically typed language
  • No declaration of variables as recognized automatically = implicit declaration
  • In command line, to run script
  • ruby -w path/file.rb
  • implicit vs. explicit Implicit Explicit Rapidly type code Would automatically complain about errors in variable declaration
  • static type vs dynamic type Static type checking (C, Java) Dynamic type checking(Ruby) Cannot use certain types throws errors automatically

More possibility for mistakes

  • control statements: one that affects which instruction is executed next
    • while loops ■ Example:
  • i = 0
  • while i < n
    • i = i + 1 # i<n = “guard”
  • end
  • conditionals ■ Example:
  • if grade >= 90 then
  • puts “You got an A”
  • elsif grade >= 80 then
  • puts “You got a B”
  • else
  • puts “you failed”
  • end #delimiter
  • delimiter: givs you a range for which code applies (end/{})
  • only false and nil are false 0 is true
  • other control statements
  • unless = if not ■ unless cond then ___ else ___ end
  • until = while not ■ until cond body end
  • function
  • def foo(x,y) #formal parameters with no type declarations ■ …
  • end
  • foo(3,4); #to call it using actual arguments
  • last expression in function body returned (can add return)
    • operator concatenates two strings/add two numbers
  • Everything is an object (primitives, classes, etc.)
  • Object.class returns class
  • Example:
  • meta programming: calling a method that accesses other methods
  • inheritance
    • instead of extends, B < A
    • every class inherited from Object
    • super(arg) invoke method parent
    • when B < A ■ b.is_a? A = true ■ b.instance_of? A = false
  • mix-in inclusion
  • include A: “inlines” A’s method at that point ■ adds those methods to that class
  • global variables: class variables beginning with @@ (same across all instances)
  • arrays indexed from 0 -> go past initialized parts of array and get nil
  • @y and $y (when no initialized) returns nil
  • Y && @@y (when no initialized) returns error
  • If x = [1,2,3]
  • x[-1] = 3
  • x[-2] = 2 array wraps around
  • x.push pushes element to end of array
  • x.delete_at(1) deletes element at index 1
  • x.pop pops element from end of array
  • arrays are heterogeneous (can carry things of many different types)
  • x.length returns length of an array
  • x.each { |y| puts y} iterates through each element, represented by y and returns receiver object
  • hash a = {}
  • can make key and value anything ■ a[“foo”] = :error ■ a[:sym] = 42 ■ a[3] = “nice”
  • for non-defined keys, returns nil
  • a.keys returns array of keys
  • a.values returns array of values
  • a.keys.each iterates through all of the keys
  • functions for Integer class
  • 3.times {puts “a”} a a a
  • 3.upto(10) {|x| puts x} – 3 4 5 6 7 8 9 100
  • Functions for Array class
  • [1,2,3,4,5].find {|x| x%2 == 0} 2 (only first element)
  • [1,2,3,4,5].delete_if {|x| x%2 == 0} {1,3,5}
  • Functions for File class
  • open: takes code block with file argyments ■ file automatically closed after block executed
  • readlines: reads all lines from a file and returns an array of the lines read (use each to iterate)
  • eof: returns true if end of file
  • close: close file
  • regular expressions use =~ (returns index of where it matches)
  • /Ruby|Ocaml/ | = disjunction
  • /R(uby|egular)/ only accepts Ruby or Regular
  • Kleene star ■ /(Ruby)*/ 0 or more occurrences of Ruby ■ /(Ruby)+/ 1 or more occurrences of Ruby ■ /(Ruby)?/ 0 or 1 occurrence of Ruby
  • Character class /[]/ ■ /[abcd]/ a or b or c or d ■ /[a-zA-Z]/ ranges of a-z and A-Z ■ /[^0-9]/ any character except 0- ■ /[\t\n]/ tab,newline
  • Special characters ■ $ = end of line ■ $ = dollar sign
  • no need to think about state
  • easier (and more powerful) ways to build correct programs and concurrent programs Ocaml
  • study functional programming because
  • garbage collection (no need to deallocate memory)
  • generics
  • higher-order functions
  • type inference
  • pattern matching
  • features of ML
  • first-class functions
  • type inference
  • garbage collection
  • favors immutability
  • data types and pattern matching
  • value: an expression that is fully evaluated
  • 34 is value while 32+2 is not a value
  • If you evaluate an expression, will yield a value of type t because an expression has type t (e:t)
  • Conditionals
  • If e1 then e2 else e3 (no end/semicolon but each thing can only take a single expression) ■ If e1 evaluates to true and e2 evaluates to v, whole thing evaluates to v ■ If e1 evaluates to false and e3 evaluates to b, whole thing evaluates to b
  • ;; evaluates everything to left
  • If ‘e1:bool’ and ‘e2:t’ and ‘e3:t’ then if…else…:t
  • = comparison
  • To define functions
  • Let rec fact n = (defines function as recursive) ■ If n = 0 then
  • 1 ■ Else
  • n*fact(n-1)
  • comments (…)
  • ‘t1 t2’ = function that takes a t1 as an argument and returns a t2 as a result
  • t1: domain
  • t2: codomain
  • ‘t1 t2 t3’ = function that takes t1 and t2 as arguments and returns t3 as a result
  • To bind variables
  • Let x = 37;;
  • Print_string “____” prints string
  • #use “a.ml” loads that file
  • Can have nested comments (blah(yes))
  • Lists
  • Arbitrary length
  • “homogeneous” all elements of same tpe
  • [] “nil” but in reality defines ‘a list = [] where ‘a is any type
  • To put an element in the list 3::[];;
  • To insert before 2::(3::[]);; (can take away parentheses since it is right associative)
  • For function, need parameters or () which indicates no parameters
  • Let a () = [1;2;3]
  • a();; returns [1;2;3]
  • let f x = x+3;;
  • f 2;; returns 5
  • evaluation of match
  • match e with ■ p1 e ■ | p2 e ■ … ■ | pn en
  • pi’s are paters made up of [],::,constants, and pattern variables
  • evaluate e to a value v
  • Evaluate e1 to a value v
  • Evaluate e2 to a value v2 and return it ■ Inside of e2, refers to v
  • Each variable refers to nearest binding in scope not where it is evaluated
  • Tuples: fixed size but can be heterogeneous
  • Anonymous functions: use keyword fun in front of it and can be passed as parameters
  • Fun x y x+y
  • (fun x y x+y) 3 42;; would evaluate to 45
  • To make enumerated values
  • type coin = ■ | H ■ | T
  • Type shape = ■ | Rect of int * int (need to pass in two int parameters when making this type) ■ | Circ of int
  • Map: takes in anonymous function and typicall used to change element in lists
  • Map (fun x -x) [1;2;3]
  • Fold: takes in anonymous function, list, and accumulator and tells you something about the list as a whole
  • Fold function accumulator list
  • No multi argument functions take arguments one at a time and return intermediary function values (currying)
  • Don’t need parentheses with two different functions
  • Closure captures its environment when it is created can refer to any variable within the scope
  • For currying
  • -> associates to the right
  • Function arguments associate to the left
  • Static scoping: names refer to their nearest binding in the program text
  • Ref pointer to a value -! gets a value from pointer
  • := repoints a value
  • To update contents of a record, use
  • Arrays are mutable
    • To change values, x.(0) = 5;; (0 is the index)