This challenge is related to some of the MATL language's features, as part of the May 2018 Language of the Month event. Associated challenge: Function clipboard: paste.
Introduction
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.
Example 1:
3 2 + 6 + 12 4 U + +
produces the result 39
. The code is interpreted as follows:
- Number literals such as
3
or12
get 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 +
gives5
5 6 +
gives11
4 U
gives16
12 16 +
28
11 28 +
gives39
.
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], [4], [5, 6]]
Example 2:
10 20 U 30 +
leaves numbers 10
and 430
on the stack. The stack is displayed bottom to top at the end of the program.
The function calls are
20 U
gives400
400 30 +
gives430
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], [20], [], []]
Example 3 (invalid):
10 20 + +
is considered invalid, because an input to the second +
is missing (in MATL this would implicitly trigger user input).
The challenge
Input: a string S with number literals, +
and U
, separated by spaces.
Output: the contents of the function clipboard after evaluating the string S.
Clarifications:
- 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], [20]]
- Any reasonable, unambiguous output format is acceptable. For example, a string with comma as inner separator and semicolon as outer separator:
400,30;20;;
.
Additional rules:
Input and output can be taken by any reasonable means.
Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
Shortest code in bytes wins.
Test cases
Input
Output
3 2 + 6 + 12 4 U + +
[[11, 28], [12, 16], [4], [5, 6]]
15 3 4 + 2 U 8 + U +
[[7, 144], [12], [4, 8], [2]]
3 6 9 12 + + 10 8 U 6
[[8], [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
[[65536], [256], [16], [4]]
[[28, 11], [16, 12], [4], [6, 5]]
valid output for the first example? \$\endgroup\$M
? \$\endgroup\$M
. I will do it in the “paste” challenge \$\endgroup\$