Back
Close

CGLambda Lite

Statement

 Goal

In this puzzle, we'll write an interpreter for CGLambda Lite, a very simple yet Turing-complete functional programming language.

CGL doesn't flood the programmer's mind with variables, types and whatnot. There are no variables, and values have a single type: function! Unsurprisingly, a CGL program is just a function.

Functions have a single ability: application. To apply a function, you provide it with one single argument and it returns a function. Function application is performed using the language's only operator: / (slash), in prefix position. So for example, to apply function v to function r, we'd write the following code: /vr

(If you're mathematically-inclined, you'll notice that prefix notation makes parentheses unnecessary. Parentheses are for complicated languages; CGL is a simple language.)

The CGL function set is defined inductively:
b, x, k, w, v, r, _ (underscore) and all capital letters are “primitive” functions.
• for any functions F and G, the application of F to G, namely /FG, is a function.

Evaluation semantics:
• for any functions F and G, evaluating the application /FG first evaluates F, then evaluates G, and finally returns the function obtained by applying F to G.
b behaves such that for any functions A,B,C: ///bABC = /A/BC
x behaves such that for any functions A,B,C: ///xABC = //ACB
k behaves such that for any functions A,B: //kAB = A
w behaves such that for any functions A,B: //wAB = //ABB
v behaves such that for any function A: /vA = v
• for P one of _ and capital letters, P behaves such that for any function A: /PA = A. This application has the additional side-effect of printing the function name to standard output.
r behaves like one of the P functions above, printing a newline.

Most “interesting” CGL programs produce infinite output, but the CodinGame platform would unfairly consider your program as hung if you let it run for that long. To avoid this, please stop interpretation after the r function has been applied L times. L is provided in the inputs.

(Note: CGL is a simplified derivative of programming language Unlambda. It's different enough that you won't find an implementation online, but similar enough that you can websearch it for implementation tips, if you're so inclined.)
Input
Line 1: N L, space-separated: number of input lines of code; output lines limit
N lines: CGLambda code
Output
Up to L lines: whatever the CGLambda program outputs
Constraints
N < 75
L < 50
Example
Input
1 15
/Xv
Output
X

Tags
CGLambdaFunctionalImpureCombinatorsLine Noise

Difficulty
Medium

Test cases
Print a single character Test
Input
1 15 /Xv
Output
X

Print a single character Validator
Input
1 15 /Jv
Output
J

Print multiple characters Test
Input
1 15 ////////////HELLO_WORLDrv
Output
HELLO_WORLD

Print multiple characters Validator
Input
1 15 ////////////OLLEH_DLROWrv
Output
OLLEH_DLROW

Multiline program Test
Input
2 15 /r/D/L/R/O/W /_/O/L/L/E/Hv
Output
HELLO_WORLD

Multiline program Validator
Input
2 15 /r/W/O/R/L/D /_/H/E/L/L/Ov
Output
OLLEH_DLROW

Multiline output Test
Input
1 15 /r/C/r/B/r/Av
Output
A B C

Multiline output Validator
Input
1 15 /r/X/r/Y/r/Zv
Output
Z Y X

Primitive k Test
Input
1 15 /r///kAZv
Output
A

Primitive k Validator
Input
1 15 /r///kMNv
Output
M

Primitive w Test
Input
1 15 /r///wCGv
Output
CGG

Primitive w Validator
Input
1 15 /r///wGCv
Output
GCC

Primitive x Test
Input
1 15 /r////xJBMv
Output
JMB

Primitive x Validator
Input
1 15 /r////xJMBv
Output
JBM

Primitive b Test
Input
1 15 /r////bJBMv
Output
BJM

Primitive b Validator
Input
1 15 /r////bBJMv
Output
JBM

Primitive v Test
Input
1 15 /r/C//vGv
Output
C

Primitive v Validator
Input
1 15 /r/X//vXv
Output
X

Church numerals Test
Input
9 15 /////b/bw//bbx/wk/k////b/bw//bbxr/// /b/bw//bbxD////b/bw//bbxL////b/bw//b bxR////b/bw//bbxO////b/bw//bbxW////b /bw//bbx_////b/bw//bbxO////b/bw//bbx L////b/bw//bbxL////b/bw//bbxE////b/b w//bbxH/wk////b/bw//bbx/wk////b/bw// bbx/wk////b/bw//bbx/wk////b/bw//bbx/ wk////b/bw//bbx/wk////b/bw//bbx/wk// //b/bw//bbx/wk////b/bw//bbx/wk/k/wk
Output
HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD

Church numerals Validator
Input
11 15 /////b/bw//bbx/wk/k////b/bw/ /bbxr////b/bw//bbxD////b/bw/ /bbxL////b/bw//bbxR////b/bw/ /bbxO////b/bw//bbxW////b/bw/ /bbx_////b/bw//bbxO////b/bw/ /bbxL////b/bw//bbxL////b/bw/ /bbxE////b/bw//bbxH/wk////b/ bw//bbx/wk////b/bw//bbx/wk// //b/bw//bbx/wk////b/bw//bbx/ wk////b/bw//bbx/wk////b/bw// bbx/wk////b/bw//bbx/wk/k/wk
Output
HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD HELLO_WORLD

Line limit Test
Input
1 1 /r/G/r/Cv
Output
C

Line limit Validator
Input
1 1 ////GrCrv
Output
G

Counting Test
Input
14 10 //////b/bw//bbx////b/bw//bbx/k/ /b/bw//bbx////b/bw//bbx/k///b/b w//bbx/wk////b/bw//bbx/kk////b/ bw//bbx/k///b/bw//bbx////b/bw// bbx/k//b/bw//bbxk////b/bw//bbx/ k////b/bw//bbx////b/bw//bbx//// b/bw//bbx/wk/kX/kr/wk/wk/k/wk/k /wk////b/bw//bbx////b/bw//bbx/k //b/bw//bbx////b/bw//bbx/k///b/ bw//bbx/wk////b/bw//bbx/kk////b /bw//bbx/k///b/bw//bbx////b/bw/ /bbx/k//b/bw//bbxk////b/bw//bbx /k////b/bw//bbx////b/bw//bbx/// /b/bw//bbx/wk/kX/kr/wk/wk/k/wk
Output
X XX XXX XXXX XXXXX XXXXXX XXXXXXX XXXXXXXX XXXXXXXXX

Counting Validator
Input
14 9 //////b/bw//bbx////b/bw//bbx/k/ /b/bw//bbx////b/bw//bbx/k///b/b w//bbx/wk////b/bw//bbx/kk////b/ bw//bbx/k///b/bw//bbx////b/bw// bbx/k//b/bw//bbxk////b/bw//bbx/ k////b/bw//bbx////b/bw//bbx//// b/bw//bbx/wk/kO/kr/wk/wk/k/wk/k /wk////b/bw//bbx////b/bw//bbx/k //b/bw//bbx////b/bw//bbx/k///b/ bw//bbx/wk////b/bw//bbx/kk////b /bw//bbx/k///b/bw//bbx////b/bw/ /bbx/k//b/bw//bbxk////b/bw//bbx /k////b/bw//bbx////b/bw//bbx/// /b/bw//bbx/wk/kO/kr/wk/wk/k/wk
Output
O OO OOO OOOO OOOOO OOOOOO OOOOOOO OOOOOOOO

ASCII Art Test
Input
8 37 /r/////b/bw//bbx/wk/k////b/bw//bbx////b /bw//bbx/kk///b/bw//bbx/wk////b/bw//bbx ////b/bw//bbx/wk/k////b/bw//bbx/k///b/b w//bbx/k////b/bw//bbxk////b/bw//bbxr/kX /wkr////b/bw//bbx/wk////b/bw//bbx/wk/// /b/bw//bbx/wk////b/bw//bbx/wk////b/bw// bbx/wk////b/bw//bbx/wk////b/bw//bbx/wk/ ///b/bw//bbx/wk////b/bw//bbx/wk/k/k/wk
Output
X XX XX XXX XXX XXX XXXX XXXX XXXX XXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX

ASCII Art Validator
Input
8 29 /r/////b/bw//bbx/wk/k////b/bw//bbx////b /bw//bbx/kk///b/bw//bbx/wk////b/bw//bbx ////b/bw//bbx/wk/k////b/bw//bbx/k///b/b w//bbx/k////b/bw//bbxk////b/bw//bbxr/kO /wkr////b/bw//bbx/wk////b/bw//bbx/wk/// /b/bw//bbx/wk////b/bw//bbx/wk////b/bw// bbx/wk////b/bw//bbx/wk////b/bw//bbx/wk/ ///b/bw//bbx/wk////b/bw//bbx/wk/k/k/wk
Output
O OO OO OOO OOO OOO OOOO OOOO OOOO OOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO

Fibonacci Test
Input
67 10 /////b/bw//bbx////b/bw//bbx////b/bw//bbx/wk/wk////b/bw/ /bbx/kk/k/wk/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bb x////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/k k/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx// //b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k //b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b /bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b /bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw //bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw //bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/kk////b/bw//bbx/kk/kr////b/bw//bbx////b/bw//bbx/k //b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b /bw//bbx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b /bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw //bbx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw //bbx////b/bw//bbx/kk/kk/k/wk////b/bw//bbx////b/bw//bbx /k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/kk/kF//// b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk/ ///b/bw//bbx/kk/wk////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx/kk/kk////b/bw//bbx/kk/wk////b/bw//bbx/k k/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw// bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw// bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx ////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx ////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk /kk////b/bw//bbx/kk/k////b/bw//bbx////b/bw//bbx/k//b/bw //bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx/kk/kk////b/bw//bbx/kk/k//b/bw//bbx////b/bw //bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//b bx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//b bx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/ k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//b bx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx/kk/kk////b/bw//bbx/kk/kk////b/bw//bbx////b /bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/k k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bb x////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bb x////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/k k/kk////b/bw//bbx/kk/kk////b/bw//bbx/kk/k/wk////b/bw//b bx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/ k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k// b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx//// b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/ k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx//// b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k// b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/b w//bbx/kk/kk////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k// b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx//// b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/ kk/kk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/kk/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/// /b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k/ /b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/ bw//bbx/kk/kk////b/bw//bbx/kk/kk////b/bw//bbx/kk/k/wk// //b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k k////b/bw//bbx/kk/k/wk////b/bw//bbx////b/bw//bbx/k//b/b w//bbx////b/bw//bbx/kk/kk/k/wk////b/bw//bbx/kk/k/wk
Output
F F FF FFF FFFFF FFFFFFFF FFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Fibonacci Validator
Input
67 9 /////b/bw//bbx////b/bw//bbx////b/bw//bbx/wk/wk////b/bw/ /bbx/kk/k/wk/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bb x////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/k k/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx// //b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k //b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b /bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b /bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw //bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw //bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/kk////b/bw//bbx/kk/kr////b/bw//bbx////b/bw//bbx/k //b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b /bw//bbx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b /bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw //bbx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw //bbx////b/bw//bbx/kk/kk/k/wk////b/bw//bbx////b/bw//bbx /k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/kk/kJ//// b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk/ ///b/bw//bbx/kk/wk////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx/kk/kk////b/bw//bbx/kk/wk////b/bw//bbx/k k/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw// bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw// bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx ////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx ////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk /kk////b/bw//bbx/kk/k////b/bw//bbx////b/bw//bbx/k//b/bw //bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx/kk/kk////b/bw//bbx/kk/k//b/bw//bbx////b/bw //bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//b bx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//b bx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/ k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//b bx/kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//b bx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx/kk/kk////b/bw//bbx/kk/kk////b/bw//bbx////b /bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/k k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bb x////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bb x////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/k k/kk////b/bw//bbx/kk/kk////b/bw//bbx/kk/k/wk////b/bw//b bx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/ k//b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k// b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx//// b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/ kk/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/ ///b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/ k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx//// b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k// b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/b w//bbx/kk/kk////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx/ ///b/bw//bbx/k//b/bw//bbx////b/bw//bbx////b/bw//bbx/k// b/bw//bbx////b/bw//bbx/kk/k//b/bw//bbx////b/bw//bbx//// b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/kk////b/bw//bbx/ kk/kk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//b bx/kk/kk/k/wk////b/bw//bbx////b/bw//bbx/k//b/bw//bbx/// /b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k/ /b/bw//bbx////b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/ bw//bbx/kk/kk////b/bw//bbx/kk/kk////b/bw//bbx/kk/k/wk// //b/bw//bbx////b/bw//bbx/k//b/bw//bbx////b/bw//bbx/kk/k k////b/bw//bbx/kk/k/wk////b/bw//bbx////b/bw//bbx/k//b/b w//bbx////b/bw//bbx/kk/kk/k/wk////b/bw//bbx/kk/k/wk
Output
J J JJ JJJ JJJJJ JJJJJJJJ JJJJJJJJJJJJJ JJJJJJJJJJJJJJJJJJJJJ

Solution language

Solution

Stub generator input