Week7 ACTUAL Flashcards
(14 cards)
What is functional programs and what does it consist of
program that consists of functions and focuses on what is to be computed not how it is computed
What is a function
mapping from set A to set B where A is domain and B is codomain
Advantages of function programs
.shorter programs
.easier to understand
.easier to design
Disadvantage
.Slower than imperative
. Less Flexible
Evaluation
Sequence of simplifying an expression (through reduction steps) until a value is obtained that cant be simplified further
Properties of evaluation
Unicity :
Value uniquely determined by components of expression and independent of the order of reduction
(ie all terminating reduction sequences must have the same value)
Non terminating:
Not all reduction sequences have a value , some may fail to terminate
What are the 2 main strategies of evaluation and explain them
Call by name:
reduce the function using its definition before evaluating its arguments
Call by value:
Evaluate arguments before reducing the function using its definition
Lazy evaluation
Haskell’s lazy evaluation is an implementation
of a call by name strategy, where arguments are evaluated if needed, using sharing to avoid
duplicating computations
Compare Call by name and call by value
. Call by name -> guaranteed to find a value if there is one
. call by value -> more effient but may fail to find a value even if there is one
Advantages of call by value
Advantages:
Since we evaluate arguments first (before reducing the function according to its defintion) no redundant computational steps [ ONLY MORE EFFICIENT IF ARGUMENT IS USED MORE THAN ONCE OTHERWISE EFFICIENCY IS THE SAME]
. easy to implement
Disadvantages:
may fail to find a value ( lead to you not terminating) even if there is a normal form associated to expression
What does Haskell use
Haskell uses lazy evaluation
lazy evaluation = call by name + sharing
It defers evaluating arguments until results are needed for computation
What is currying
When applying a function with more than 1 argument yields another function with n -1 arguments
This allows the creation of new functions via partial functions
Curry type
((a,b) -> c ) -> a -> b -> c
uncurry
(a -> b ->c ) -> (a,b) -> c