Week7 Flashcards
(12 cards)
what is functional programming
type of language that :
focus on what is to be computed not how to compute it
ie: we transform inputs into outputs without changing state or having side effects
Functional advantages
. Shorter programs ( made possible through lamba expressions and higher order functions like map , filter , reduce…)
. easier to understand
. easier to design than imperative
Functional disadvantages
.slower than imperative (remember immutability creates new copies per transformation -> more overhead and slower)
. less flexible -> immutability restricts flexibitlity we cant change state we must create a new copy ( remember val in scala )
What do we mean by evaluation
Evaluating is just simplifying an expression step-by-step until we reach a value or normal form that can’t be simplified any further.
Properties of evaluation
1) Unicity of normal form:
value uniquely dependent on components of expression and not dependent on the order of reduction
DONT GET CONFUSED THO I GOT CONFUSED AND I WILL ADDRESS BELOW :
If any reduction sequence terminates (i.e. reaches a normal form),
then all terminating sequences must reach the same result.
2) Non terminate :
Evaluation of an expression may not terminate ie doesnt have a value
Call by name
Reduce the function first using its definition, then evaluate the argument
call by value
Evaluate the argument first, then reduce the function using its definition
Call by name vs call by value
. Call by name will always find a value if there is one
. Call by value is more efficient but may not lead to a value (remember fourtytwo infinity function in slide)
What are the advantages and the disadvantages of the call-by-value strategy of evaluation
for functional languages?
Advatages:
more effiencnt as it removes redundant computational steps since argument evaluated first before applying function definition
easy to implement
disadvantages:
may lead to you not terminating and FAILING TO FIND A VALUE FOR A TERM EVEN IF IT HAS A NORMAL FORM
WHAT DOES HASKELL USE
Haskell uses lazy evaluation :
call by name + sharing
deferes evaluation of an expression until when its result needed for a computation
How does currying work and whats its type
Currying is the process of transforming a function that takes multiple arguments into a chain of functions, each taking one argument at a time.
curry :: ((a, b) -> c) -> a -> b -> c
uncurry
uncurry :: (a -> b -> c) -> (a, b) -> c