How to resolve the algorithm Closures/Value capture step by step in the Lingo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Closures/Value capture step by step in the Lingo programming language

Table of Contents

Problem Statement

Create a list of ten functions, in the simplest manner possible   (anonymous functions are encouraged),   such that the function at index   i   (you may choose to start   i   from either   0   or   1),   when run, should return the square of the index,   that is,   i 2. Display the result of running any but the last function, to demonstrate that the function indeed remembers its value.

Demonstrate how to create a series of independent closures based on the same template but maintain separate copies of the variable closed over. In imperative languages, one would generally use a loop with a mutable counter variable. For each function to maintain the correct number, it has to capture the value of the variable at the time it was created, rather than just a reference to the variable, which would have a different value by the time the function was run. See also: Multiple distinct objects

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Closures/Value capture step by step in the Lingo programming language

Source code in the lingo programming language

-- parent script "CallFunction"

property _code

-- if the function is supposed to return something, the code must contain a line that starts with "res="
on new (me, code)
  me._code = code
  return me
end

on call (me)
  ----------------------------------------  
  -- If custom arguments were passed, evaluate them in the current context.
  -- Note: in the code passed to the constructor they have to be referenced
  -- as arg[1], arg[2], ...
  arg = []
  repeat with i = 3 to the paramCount
    arg[i-2] = param(i)
  end repeat
  ----------------------------------------
  res = VOID
  do(me._code)
  return res
end

funcs = []
repeat with i = 1 to 10
  code = "res="&i&"*"&i
  funcs[i] = script("CallFunction").new(code)
end repeat

put call(funcs[3], _movie)
-- 9

funcs = []
repeat with i = 1 to 10
  code = ""
  put "res = "&i&"*"&i &RETURN after code
  put "repeat with i = 1 to arg.count" &RETURN after code
  put "  res = res + arg[i]" &RETURN after code
  put "end repeat" after code
  funcs[i] = script("CallFunction").new(code)
end repeat

put call(funcs[3], _movie, 23)
-- 32

put call(funcs[7], _movie, 4, 5, 6)
-- 64

  

You may also check:How to resolve the algorithm Playing cards step by step in the Fortran programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the Elixir programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Euler method step by step in the Sidef programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Standard ML programming language