CS计算机代考程序代写 Java compiler javascript Spring 2019 Mid-term answers

Spring 2019 Mid-term answers
Q1:
True.
Q2:
False:
Q3:
Q4:
Q5:
No, you can also write Scala code for your browser with Scala.js (which compiles into
Javascript) and you can also run Scala natively using Scala Native (with LLVM).
val evaluates its expression exactly once (possibly zero times if marked lazy);
def defines an expression which is evaluated every time the identifier is referenced.
A val cannot be parameterized; A def can be parameterized.
A val must be declared–and, therefore, evaluated (or “initialized”)–before (above or to the left of) it is actually used–unless it is marked lazy (in which case it is treated more like a def).
A def may be declared anywhere in the appropriate scope where it is used.
A method is tail-recursive (i.e. tail-call optimized) provided that the result of any recursive call is returned
without further processing.
A tail-recursive (i.e.
“recursion.”
tail-call optimized) method will not throw a
The following code will likely cause a stack overflow:
In most simple situations, you can write a tail-recursive method by giving it two parameters:
• the parameter whose value will be returned when the terminating condition is reached;
• the parameter which represents the work remaining to be done by the method.
def factorial(x: BigInt): BigInt = if (x<=1) 1 else x * factorial(x-1) factorial(BigInt(100000)) In programming, recursion is the corresponding technique to the mathematical technique of proof by induction. Indeed, it is trivially easy to "prove" recursive code by using the substitution principle and then using induction. mystery is a higher-order function (method). The implementation of mystery (i.e. its right-hand-side) uses functional composition. The implementation (right-hand side) could be replaced by: t => for (x <- g(t)) yield f(x) The implementation (right-hand side) could be replaced by: g andThen (_ map f) mystery StackOverflowError, no matter how deep the The underscore ("_") denotes the value of what will be the input parameter of the function that is result of invoking the method. The function f will never actually be called if the result of function g is a Failure. Q6: Q7: Try(f(t)) for Failure(x) T Q9: The purpose of the mystery method is to create a function which will return the value of some t , provided that the result of invoking R g(t) (perhaps from some third-party library) but where that function f is a Success A "context bound" can be applied to a parametric type of a class or method such that there must be an implicit instance of the appropriate type class in scope. For example, declaring the following class requires that concrete instances of type X must have an appropriate Ordering: Methods defined by a trait which is a type class do not normally refer to this, that is to say they are not ordinary instance methods. For example, in the trait Zero (see below), the only method (zero) does not refer to this. g(t) yields a Failure(x) , then might throw an exception with will be returned by the resulting function. It is useful when you have a function f (or method) which takes a and returns an some inputs. In particular, this is useful when f is a Java method. A type class is a mechanism that allows a programmer to impute additional behavior (functionality) to another type without using inheritance. case class Sortable[X: Ordering](xs: Seq[X]) { def sorted: Seq[X] = xs.sorted } trait Zero[X] { /** * Method to create a zero/empty/nothing value of X * * @return an X which is zero (empty, etc.) */ def zero: X } trait IntZero extends Zero[Int] { def zero: Int = 0 } Numeric is a type class which extends Ordering. Q8: In a pattern-matching context, the identifier " by x x" matches any value and this value can then be referred to case token and preceding the rocket symbol "=>” in a
in the evaluated expression.
A pattern-matching context is present following a
match expression.
A pattern-matching context is present preceding the “<-" in a generator of a for comprehension. A pattern-matching context is present after val or var and before the "=" of a variable definition. In a pattern-matching context, an object can be "deconstructed" using a compiler-generated invocation of unapply. An example of this might be: caseh ::t => inner(r ++ h, t)
Here, the
unapply
method of the companion object to “::” is used to yield values for h and t.
When an implicit is defined in the same scope as its intended use, it must follow similar rules to an
val
An implicit thing (val, object, etc.) must be marked implicit if it is to be used implicitly.
implicit
ordinary
: it must be declared before it is referenced.
. If
The compiler will find an
according to the usual scope rules and, if no
matching implicit is found, it will then look in the companion object of the required type. If seeking an implicit
function, it will look in the companion object of each of the input and output types of the function.

Leave a Reply

Your email address will not be published. Required fields are marked *