In many languages, functions can be nested, resulting in outer functions and inner functions. The inner function can access variables from the outer function. In most languages, the inner function can also modify variables in the outer function.



Task
 
Write a program consisting of two nested functions that prints the following text.

1. first
2. second
3. third

The outer function (called MakeList or equivalent) is responsible for creating the list as a whole and is given the separator ". " as argument. It also defines a counter variable to keep track of the item number. This demonstrates how the inner function can influence the variables in the outer function.

The inner function (called MakeItem or equivalent) is responsible for creating a list item. It accesses the separator from the outer function and modifies the counter.



References

 Nested function


--------------------- NESTED FUNCTION --------------------
 
-- makeList :: String -> String
on makeList(separator)
    set counter to 0
 
    -- makeItem :: String -> String
    script makeItem
        on |λ|(x)
            set counter to counter + 1
 
            (counter & separator & x & linefeed) as string
        end |λ|
    end script
 
    map(makeItem, ["first", "second", "third"]) as string
end makeList
 
--------------------------- TEST -------------------------
on run
 
    makeList(". ")
 
end run
 
 
-------------------- GENERIC FUNCTIONS -------------------
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
    -- 2nd class handler function lifted into 1st class script wrapper. 
    if script is class of f then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
    -- The list obtained by applying f
    -- to each element of xs.
    tell mReturn(f)
        set lng to length of xs
        set lst to {}
        repeat with i from 1 to lng
            set end of lst to |λ|(item i of xs, i, xs)
        end repeat
        return lst
    end tell
end map