Final Flashcards
Imperative Programming
Fundamental operation is the assignment statement
Control flow is dependent on the values of variables
Interpretation of code depends on program state
relies on side effects
Functional Programming
Principal operation is function application (function calls)
functions bay be passed as arguments to other functions
Functions may be returned by functions
Side effect
When a function relies on, or modifies, something outside its parameters to do something.
A function is pure if given the same inputs it:
- always returns the same output
- does not have any side effects
more difficult to reason about a program the relies on side effects
Compiler
source to machine code to run later
interpreter
source to run now
transpiler
source to source
syntax
How a program looks. Is specified / validated by a grammar
A program is syntactically valid if there is a parse tree
for the given grammar
Semantics
what a program does
what a program means
Sentence ( of grammar)
string of tokens from the grammar
Sentential form
String of terminals & non terminals from the grammar
derivation
sequence of sentential forms that starts with start symbols & ends with a sentence
a sentence is valid if there exists at least one derivation for it
grammar
A set of instructions about how to write statements that are valid for that programming language.
Grammars consist of
* Non-terminals (or “syntactic categories”)
* Terminals
* Productions
* A start symbol/non-terminal
Abstract Syntax Tree
A data structure that represents the text of a program
Lexer
A program that takes source code & turns it into a stream of tokens
Parser / Parsing
process of detecting whether a stream of tokens is a valid sentence in a grammar.
if fails : syntax error
if pass : return AST
what is produced by a parser
AST
Abstract Syntax Tree
briefly explain when type errors occur in different languages
statically typed: c, c++, java
- compile time
dynamically typed: python, ruby
- runtime
bonus:
SML cannot have runtime type errors
explain the difference between java and python
java is statically typed
variables have types
python is dynamically typed
variables have values. The values have types
HOF functions
Can take functions as parameters.
Can “build” functions that can be returned.
Functions can be “curried.” (built 1 param at a time)
Common HOFs
map, reduction (reduce, foldl, foldr), filter
map
transform a list that looks one way into a list that looks a different way
uses a unary function
filter
shrink a list
exclude certain values or include certain values
uses a unary function
reduce
combine a list into 1 value
uses a binary function
how would you use map
- map (fn x => x * 2) [1, 2, 3, 4];
val it = [2,4,6,8] : int list
polymorphism
Allows objects of different classes to be treated as objects of a common superclass. This enables a single action to behave differently based on the object it is acting upon.