MATL has several clipboards, where you can store values (copy) to be retrieved later (paste). Some clipboards are automatic, which means that copying is automatically triggered by certain events. This challenge focuses on one of the automatic clipbards, called the function-input clipboard, or simply function clipboard.
This clipboard stores the inputs to the four most recent calls to normal, input-taking functions. Normal functions are the most common type of functions in MATL. Input-taking means that the function takes at least one input (functions that do not take any input are not considered by the function clipboard).
This is best explained with the following examples, which use two normal functions:
+, which pops two numbers from the stack and pushes their sum.
U, which pops one number and pushes its square.
3 2 + 6 + 12 4 U + +
produces the result
39. The code is interpreted as follows:
- Number literals such as
12get pushed to the stack
- Functions such as
+pop their inputs and push their outputs to the stack.
The function calls, in chronological order, are:
3 2 +gives
5 6 +gives
12 16 +
11 28 +gives
The clipboard can be viewed as a list of four lists. Each inner list contains the inputs to a function call, with most recent calls first. Within each inner list, inputs are in their original order.
So after running the code the clipboard contents are (in Python notation):
[[11, 28], [12, 16], , [5, 6]]
10 20 U 30 +
430 on the stack. The stack is displayed bottom to top at the end of the program.
The function calls are
400 30 +gives
Since there have been only two function calls, some of the inner lists defining the clipboard will be empty. Note also how
10 is not used as input to any function.
Thus, the clipboard contents after running the code are:
[[400, 30], , , ]
Example 3 (invalid):
10 20 + +
is considered invalid, because an input to the second
+ is missing (in MATL this would implicitly trigger user input).
Input: a string S with number literals,
U, separated by spaces.
Output: the contents of the function clipboard after evaluating the string S.
- You may use any two consistent symbols to represent those functions, other than digits. Also, you can use any consistent symbol as separator, instead of space.
- Only the two indicated functions will be considered.
- The input string will contain at least one number literal and at least one function.
- All numbers will be positive integers, possibly with more than one digit.
- It is possible that some number literals are not used by any function, as in the example 2.
- The input is guaranteed to be valid code, without requiring additional numbers. So a string as in example 3 will never occur.
- Trailing empty inner lists in the output can be ommited. So the result in example 2 can be
[[400, 30], ]
- Any reasonable, unambiguous output format is acceptable. For example, a string with comma as inner separator and semicolon as outer separator:
Input and output can be taken by any reasonable means.
Shortest code in bytes wins.
Input Output 3 2 + 6 + 12 4 U + + [[11, 28], [12, 16], , [5, 6]] 15 3 4 + 2 U 8 + U + [[7, 144], , [4, 8], ] 3 6 9 12 + + 10 8 U 6 [, [6, 21], [9, 12], ] 8 41 12 25 4 5 33 7 9 10 + + + + + + + + [[41, 105], [12, 93], [25, 68], [4, 64]] 10 1 1 + U U U U U [, , , ]