240 32 827KB
English Pages [16] Year 2022
A few Func Prog questions to think about Joshua Freeman October 7, 2022 Abstract These are a few questions I asked myself while taking notes in cs210. Some might not make sense, but I hope most can be useful to you. Feel free to reach out at joshua dot freeman at epfl dot ch if you want to participate. I would like to extend a warm thank you to Fran¸cois Dumoncel, who has helped with proofreading and bettering this document. Also thank you to • Antoine Tran Link to check if your version of the pdf is up to date
Question 1. Q. What is the difference between call-by-value and call-by-name ? A. In call-by-value we evaluate arguments before applying the function (LTR), in call-by-name after. Question 2. Q. How to force a parameter of a function to be expressed with call-by-name ? A. To make an argument called by-name, simply prepend => (rocket symbol) to its type. def test(byname: => Int) = ???. This is similar to a function taking 0 arguments and returning the given value when called Question 3. Q. Tail recursion (flat complexity) vs recursion in general ? A. Tail recursion consists in a tail-call (call that is the last action) of a function in its own body. Question 4. Q. How to require for a function to be tail-recursive ? A. The @tailrec annotation. Question 5. Q. Special syntax for functions that return other functions ? 1
A.
def sum(f: Int => Int)(a: Int, b: Int): Int = if a > b then 0 else f(a) + sum(f)(a + 1, b)
Question 6. Q. How to require a precondition on a class ? A. Calling method require(Boolean cond, String msg) at the very beginning of the class. Question 7. Q. assert function vs require function ? A. (1) IllegalArgumentException for require, AssertionError for assert, (2) checking user input vs checking the code of the function itself. Question 8. Q. How to overload the constructor of a class ? A.
class Rational(x: Int, y: Int): def this(x: Int) = this(x, 1)
Question 9. Q. How to redefine addition for a given class ? A. With extension(x: C) infix def + (y: C) : C2 = ??? Question 10. Q. What is the keyword ”object” useful for ? A. To define a singleton object of a class (useful if we want to reuse it). it can have the same name as the class(companion) Question 11. Q. What is the point of a companion object ? A. To define something akin to static methods. Question 12.
2
Q.
given
A. Then answer is the common type of false and 1 : AnyVal. Question 13. Q. Which design pattern are case classes particularily useful for ? A. For pattern matching Question 14. Q. To make a list (always linked), what is the construction operator ? A. :: Question 15. 3
Q. How do operators ending in a colon associate ? A. To the right: (a ::
b ::
c ::
Nil) === (a ::
(b ::
(c ::
Nil)))
Question 16. Q. What are the three patterns we can use to pattern-match a list ? A.
Question 17. Q.
DRAW THE TREE A.
Question 18. Q. Two usages for enums in scala ? A.
i. Hierarchies of case classes (ADT) ii. Define a data type accepting different values (like in java)
Question 19. Q. What do S >: T and S