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: copy.
Introduction
MATL's function clipboard stores ("copies") 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. The stored clipboard contents can be pushed onto the stack ("pasted").
This challenge will take the clipboard contents as input. It will be assumed that all functions that have produced that clipboard state took one or more positive integers as inputs. So the clipboard state can be represented by a list of lists of numbers. (For more information on how the clipboard is actually filled see the related challenge; but that's not necessary for the current one).
Interpreting the clipboard contents
Example 1
The first inner list refers to the most recent function call, and so on, Thus the clipboard state
[[11, 28], [12, 16], [4], [5, 6]]
indicates that the last function call took two inputs, namely 11
, 28
; the second-last call took inputs 12
, 16
; etc. (This clipboard state is produced by the code in the first example of the related challenge).
Example 2
If there have not been enough function calls, some trailing inner lists in the clipboard will be empty:
[[7, 5], [], [], []]
(This is produced by a program that simply adds 7
and 5
).
Example 3
Function calls can have any number of inputs, but always at least 1
(functions taking no inputs do not change the clipboard state). So the following is also possible.
[[3], [2, 40, 34], [7, 8, 15], []]
Accessing the clipboard contents
The contents of the function clipboard are pushed onto the stack using MATL's function M
(which, by the way, is not a normal function, but a clipboard function). This function takes a positive integer as input, and pushes some of the clipboard contents onto the stack, as follows. With reference to the clipboard state in example 1:
[[11, 28], [12, 16], [4], [5, 6]]
1M
returns all inputs to the most recent function call. So, for the considered example, it gives11
,28
.- Similarly,
2M
,3M
and4M
return all inputs to the second, third and fourth most recent function calls. So2M
gives12
,16
;3M
gives4
; and4M
gives5
,6
. - Numbers beyond
4
select individual inputs to function calls that took more than one input. So5M
returns the last input to the most recent such call. In our case this gives28
.6M
returns the preceding individual input, which is11
.7M
returns the last input of the second-last call, that is,16
, and8M
gives12
. Now,9M
gives6
. Note how input4
is skipped because it was the only input in its function call. Lastly,10M
gives5
.
For the clipboard state in example 3:
[[3], [2, 40, 34], [7, 8, 15], []]
1M
gives3
.2M
gives2
,40
,34
.3M
gives7
,8
,15
.4M
has undefined behaviour (for the purposes of this challenge), because there have only been three function calls.5M
gives34
.6M
gives40
.7M
gives2
.8M
gives15
.9M
gives8
,10M
gives7
.11M
,12M
, ... also have undefined behaviour.
The challenge
Input:
- the clipboard state, as a list of lists, or any other reasonable format;
- a positive integer n.
Output: the result of calling function M
with n as input. The output will be one or several numbers with an unambiguous separator, or in any reasonable format such as a list or array.
Clarifications:
- The clipboard state consists of four lists of numbers. Some of the trailing lists may be empty, as in examples 2 and 3. If preferred, you can input the clipboard without those trailing empty lists. So example 3 would become
[[3], [2, 40, 34], [7, 8, 15]]
. - All numbers in the clipboard will be positive integers, possibly with more than one digit.
- The number n is guaranteed to be valid. So for example 3 above,
n
cannot be4
or11
.
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
Clipboard state
Number
Output(s)
[[11, 28], [12, 16], [4], []]
2
12, 16
[[11, 28], [12, 16], [4], []]
5
28
[[7, 144], [12], [4, 8], [3, 4, 6]]
1
7, 144
[[7, 144], [12], [4, 8], [3, 4, 6]]
10
4
[[30], [40], [50, 60], [70, 80, 90]]
2
40
[[30], [40], [50, 60], [80, 90]]
7
90
[[15], [30], [2, 3, 5], [4, 5, 10]]
3
2, 3, 5
[[15], [30], [2, 3, 5], [4, 5, 10]]
7
2