To preserve the actual printed function, including its handling of nonpositive exponents and wrapping Int bases, use a tail-recursive accumulator with the same multiplication order:
def anotherfuncRec(x: Int, n: Long): Double = {
  require(x > 0)
  @scala.annotation.tailrec
  def loop(y: Int, i: Long, result: Double): Double = {
    if (i <= 0) result
    else {
      val nextResult = if (i % 2 == 1) result * y else result
      loop(y * y, i / 2, nextResult)
    }
  }
  loop(x, n, 1.0)
}
Each recursive call corresponds to one source iteration and carries exactly its next program state. Hence it reproduces the printed numerical behavior, rather than silently repairing it. For positive n, both routines use steps; for nonpositive n, both take constant time. The recursive call is in tail position, so Scala's tail-call optimization avoids a growing call stack; the annotation asks the compiler to check that condition.
If the intended exact power is required instead, change the base and accumulator parameters of this same recurrence to BigInt and require n >= 0, as in part (d). Its mathematical loop invariant becomes a recursion invariant, with the same logarithmic number of multiplication steps.
Method overloading gives one name several parameter signatures, such as show(A) and show(B). The compiler chooses an applicable signature using the receiver's static type and the argument expressions' static types, preferring a uniquely more specific applicable alternative. An argument whose type is a subtype of the parameter type is acceptable; ambiguous or inapplicable alternatives cause a compile-time error. The runtime type of an argument does not start a second overload search.
Method overriding replaces an inherited implementation of the same method signature. After overload selection, ordinary instance calls use dynamic dispatch on the receiver's runtime type to select the most-derived implementation of that signature. A concrete inherited method requires the override keyword in Scala; a super call invokes the corresponding superclass implementation instead of redispatching to the overriding body. In a trait stack, super follows the class's linearization.[ref][ref]
The maximally variant safe declarations are:
trait Rel1[+X, +Y] { val pairs: List[(X, Y)] }
trait Rel2[-X, +Y] { def relatives(x: X): List[Y] }
trait Rel3[-X, -Y] { def related(x: X, y: Y): Boolean }
Leaving one or both parameters invariant is also legal but admits fewer subtyping conversions. These are type variance annotations.
For Rel1, both parameters occur only as outputs. Scala's immutable List and Tuple2 are covariant, so a list of pairs of narrower types can safely be read as pairs of broader types. Thus both may be covariant type parameters. A contravariant declaration would permit, for example, treating a Rel1[Any, Int] containing (7, 0) as a Rel1[String, Int], then reading its first component as a String. That would be unsound; the compiler rejects the negative annotation at this output position.
For Rel2, X occurs as a method argument and therefore may be a contravariant type parameter; Y is returned in an immutable list and may be a covariant type parameter. A function that accepts every Any is safe to use where only String queries are issued. The opposite direction would be unsafe: if X were covariant, the following hypothetical conversion could call a string-only implementation with an integer:
val strings: Rel2[String, Int] = new Rel2[String, Int] {
  def relatives(x: String): List[Int] = List(x.length)
}
// Unsafe if Rel2 were incorrectly covariant in X:
// val arbitrary: Rel2[Any, Int] = strings
// arbitrary.relatives(7)
Making Y contravariant would instead allow an integer-valued Rel2[String, Any] to be read as a string-valued Rel2[String, String], recreating the output error.
For Rel3, both generic values are consumed and only a Boolean function result is produced, so both may be contravariant type parameters. A predicate accepting all values of broad types can answer queries restricted to narrower types. Covariance in either parameter would allow passing an unsupported value to a specialized implementation. For example, treating a string-only first-argument predicate as accepting Any would permit related(7, y) even though its body might call x.length. These are applications of the variance-position rule: method inputs reverse variance, whereas immutable outputs preserve it.[ref]
Peter Naur 2026-11-05
Peter Naur helped define ALGOL 60 and edited its language report. The revised report specifies both the syntax and operational meaning of expressions, statements, blocks and procedures. Its explicit separation of formal syntax from explanatory semantics made the language definition independent of any one compiler.[ref]
A nested block, for example, can declare a local name without exposing it throughout the entire program. The definition must specify which declaration an occurrence denotes and how procedure parameters behave. Such details make scope and parameter passing part of a language's semantics rather than conventions guessed from examples. ACM's award announcement credits Naur's report editing, language-definition work and compiler contributions.[ref]
Register allocation 2026-11-05
Register allocation maps temporary values or virtual registers in a compiler's representation to a limited supply of physical processor registers. A value's live range covers where it may still be needed. Overlapping live ranges constrain which values can share a processor register; register spilling moves some storage into computer memory when necessary.[ref]
For example, in a=x+y; b=a*z, the processor register holding a can hold b after a's final use, if the target operation permits. If both values are needed later, overwriting a would lose required information. An interference graph represents simultaneous storage requirements, connecting register allocation to graph coloring. Real targets add constraints from register classes, reserved registers, calling conventions and particular machine instructions; minimizing the number of names alone does not solve the whole problem.
Register spilling 2026-11-05
Register spilling is a compiler technique that keeps some values in computer memory when they cannot all remain in the available processor registers. Generated stores preserve a value before its register is reused; later loads recover it when needed. This is part of register allocation, whose choices affect the number of memory accesses.[ref]
For example, suppose three independently computed values must survive to later instructions, but only two suitable processor registers are available. The allocator may store one value in a stack slot, use its register for another value, and reload it before its next use. The best choice depends on live ranges, use frequency and instruction constraints; recomputing a cheap value can sometimes avoid the store.
The compiler's methodSig, typesFromOverridden, and assignTypeToTree use an inherited return type as an expected type when checking an overriding method body. This permits numeric widening of a literal in that body while still inferring more specific reference result types where appropriate.
Source code 2026-10-14
A program written in the input notation of a compiler or interpreter, before the particular translation being discussed.
Tail recursion 2026-10-24
Recursion in which the recursive call is the last operation required to obtain the current call's result. An accumulator can encode the state that would otherwise be updated by a loop. A suitable compiler can replace the call by a jump, avoiding growing call stack storage. In Scala, a tail-recursion annotation requests a check that the recursive method is eligible for this transformation.
Tony Hoare Created 2026-10-18 Updated 2026-11-05
Tony Hoare is a computer scientist whose contributions include quicksort, Hoare logic and communicating sequential processes. His industrial work included an early ALGOL 60 compiler; his institutional biography also describes an unsuccessful operating system project, giving concrete context for the difficulty of reliable systems engineering.[ref]
In his 1969 paper An Axiomatic Basis for Computer Programming by Tony Hoare, he set out assignment, composition and iteration rules for reasoning directly about program text. The assignment rule reasons backward from the required result, while a loop invariant compresses arbitrarily many iterations into a preservation obligation. The paper explicitly distinguishes these partial correctness results from successful termination and discusses how arithmetic and side effects affect the model.[ref]
Figure 1.
Portrait of Tony Hoare
. Dennis Hamilton (orcmid), Flickr; File caption identifies FLoC 2006, 18 August 2006. Creative Commons Attribution 2.0 Generic., via Wikimedia Commons. Source.
Undefined behavior 2026-11-05
Undefined behavior is behavior for which a programming language specification imposes no requirements when a specified erroneous condition occurs. It is not a promise to select one arbitrary but otherwise constrained result. The C programming language uses this category for conditions including signed integer overflow.[ref]
For example, evaluating x+1 as signed int when x is the largest representable int has undefined behavior in that model. A compiler can therefore reason that x+1 exceeds x on executions where the addition is defined, instead of preserving an assumed wraparound case. Unsigned integer arithmetic in C has different rules. Observing a particular result in one build is insufficient to establish a contract; correctness needs range checks, suitable representations or another explicitly defined operation.