CSCI305.github.io

CSCI 305 Homework 4

Due Date: April 2, 2018 @ Beginning of Class



Name:                                                                                        




Memory Locations

  1. Write the shortest ML function you can that would not work correctly if implemented using a dynamically allocated stack of activation records plus nesting links. Explain why it would fail.
fun f x =
  let
    fun g y = x + y
  in
    g
  end;

Fails since a dynamically allocated stack of activation records plus nesting links will not accommodate the return value of a function which requires the use of values from the containing function.

  1. For the following ML function, could the activation record for the function be deallocated as soon as the function returns? Explain why or why not.

    fun f x = fn y => x + y;
    

    No as it returns a function which in turn uses the parameter to the original function as a variable.

  2. The following ML function contains a function call that passes a function parameter f. Will the function f use its nesting link when called? Explain (Assume that the nesting link is not used to resolve references to predefined operators (like +) and functions (like map).)

    fun addall n theList =
      let fun f x = x + n;
      in map f theList
      end;
    

    Yes it does use its nesting link in order to find the value of the variable n.