Skip to main content
grammar
Source Link
chunes
  • 27.8k
  • 3
  • 32
  • 55

Factor, 68 bytes

[ { 2 } swap [ reverse [ 1 + <array> ] map-index concat ] times Σ ] 

Try it online!

Explanation:

It's a quotation (anonymous function) that takes an integer from the data stack as input and puts an integer (the nth term of the Levine sequence) on the data stack as output. Assuming 4 is on top of the stack when this quotation is called...

  • { 2 } Push a sequence to the stack containing one element, 2.

    Stack: 4 { 2 }

  • swap Swap two objects (because times needs access to the 4 later).

    Stack: { 2 } 4

  • [ reverse [ 1 + <array> ] map-index concat ] Push a quotation for times to use later. This is a quotation that produces the next row of the Levine's Triangle from the previous one.

    Stack: { 2 } 4 [ reverse [ 1 + <array> ] map-index concat ]

  • times Call a quotation a certain number of times. Inside the quotation now for the first iteration...

    Stack: { 2 }

  • reverse Reverse a sequence.

    Stack: { 2 }

  • [ 1 + <array> ] Push a quotation for map-index to use later.

    Stack: { 2 } [ 1 + <array> ]

  • map-index Take a sequence and a quotation and apply the quotation to each element of the sequence, collecting the results into a new sequence of the same length. However, it also places the index of the element on top of the stack. Inside the quotation now for the first iteration...

    Stack: 2 0

  • 1 Push 1.

    Stack: 2 0 1

  • + Add.

    Stack: 2 1

  • <array> Create an array from a count and an element.

    Stack: { 1 1 }

  • Now map-index has exhausted its elements. Since we are mapping numbers to arrays, the result will be an array of arrays.

    Stack: { { 1 1 } }

  • concat Concatenate a sequence of sequences together into one sequence.

    Stack: { 1 1 }

  • Now we have produced the second row of Levine's Triangle. times will run the quotation three more times...

    Stack: { 1 1 2 3 }

  • Σ Sum a sequence.

    Stack: 7

Factor, 68 bytes

[ { 2 } swap [ reverse [ 1 + <array> ] map-index concat ] times Σ ] 

Try it online!

Explanation:

It's a quotation (anonymous function) that takes an integer from the data stack as input and puts an integer (the nth term of the Levine sequence) on the data stack as output. Assuming 4 is on top of the stack when this quotation is called...

  • { 2 } Push a sequence to the stack containing one element, 2.

    Stack: 4 { 2 }

  • swap Swap two objects (because times needs access to the 4 later.

    Stack: { 2 } 4

  • [ reverse [ 1 + <array> ] map-index concat ] Push a quotation for times to use later. This is a quotation that produces the next row of the Levine's Triangle from the previous one.

    Stack: { 2 } 4 [ reverse [ 1 + <array> ] map-index concat ]

  • times Call a quotation a certain number of times. Inside the quotation now for the first iteration...

    Stack: { 2 }

  • reverse Reverse a sequence.

    Stack: { 2 }

  • [ 1 + <array> ] Push a quotation for map-index to use later.

    Stack: { 2 } [ 1 + <array> ]

  • map-index Take a sequence and a quotation and apply the quotation to each element of the sequence, collecting the results into a new sequence of the same length. However, it also places the index of the element on top of the stack. Inside the quotation now for the first iteration...

    Stack: 2 0

  • 1 Push 1.

    Stack: 2 0 1

  • + Add.

    Stack: 2 1

  • <array> Create an array from a count and an element.

    Stack: { 1 1 }

  • Now map-index has exhausted its elements. Since we are mapping numbers to arrays, the result will be an array of arrays.

    Stack: { { 1 1 } }

  • concat Concatenate a sequence of sequences together into one sequence.

    Stack: { 1 1 }

  • Now we have produced the second row of Levine's Triangle. times will run the quotation three more times...

    Stack: { 1 1 2 3 }

  • Σ Sum a sequence.

    Stack: 7

Factor, 68 bytes

[ { 2 } swap [ reverse [ 1 + <array> ] map-index concat ] times Σ ] 

Try it online!

Explanation:

It's a quotation (anonymous function) that takes an integer from the data stack as input and puts an integer (the nth term of the Levine sequence) on the data stack as output. Assuming 4 is on top of the stack when this quotation is called...

  • { 2 } Push a sequence to the stack containing one element, 2.

    Stack: 4 { 2 }

  • swap Swap two objects (because times needs access to the 4 later).

    Stack: { 2 } 4

  • [ reverse [ 1 + <array> ] map-index concat ] Push a quotation for times to use later. This is a quotation that produces the next row of the Levine's Triangle from the previous one.

    Stack: { 2 } 4 [ reverse [ 1 + <array> ] map-index concat ]

  • times Call a quotation a certain number of times. Inside the quotation now for the first iteration...

    Stack: { 2 }

  • reverse Reverse a sequence.

    Stack: { 2 }

  • [ 1 + <array> ] Push a quotation for map-index to use later.

    Stack: { 2 } [ 1 + <array> ]

  • map-index Take a sequence and a quotation and apply the quotation to each element of the sequence, collecting the results into a new sequence of the same length. However, it also places the index of the element on top of the stack. Inside the quotation now for the first iteration...

    Stack: 2 0

  • 1 Push 1.

    Stack: 2 0 1

  • + Add.

    Stack: 2 1

  • <array> Create an array from a count and an element.

    Stack: { 1 1 }

  • Now map-index has exhausted its elements. Since we are mapping numbers to arrays, the result will be an array of arrays.

    Stack: { { 1 1 } }

  • concat Concatenate a sequence of sequences together into one sequence.

    Stack: { 1 1 }

  • Now we have produced the second row of Levine's Triangle. times will run the quotation three more times...

    Stack: { 1 1 2 3 }

  • Σ Sum a sequence.

    Stack: 7

Source Link
chunes
  • 27.8k
  • 3
  • 32
  • 55

Factor, 68 bytes

[ { 2 } swap [ reverse [ 1 + <array> ] map-index concat ] times Σ ] 

Try it online!

Explanation:

It's a quotation (anonymous function) that takes an integer from the data stack as input and puts an integer (the nth term of the Levine sequence) on the data stack as output. Assuming 4 is on top of the stack when this quotation is called...

  • { 2 } Push a sequence to the stack containing one element, 2.

    Stack: 4 { 2 }

  • swap Swap two objects (because times needs access to the 4 later.

    Stack: { 2 } 4

  • [ reverse [ 1 + <array> ] map-index concat ] Push a quotation for times to use later. This is a quotation that produces the next row of the Levine's Triangle from the previous one.

    Stack: { 2 } 4 [ reverse [ 1 + <array> ] map-index concat ]

  • times Call a quotation a certain number of times. Inside the quotation now for the first iteration...

    Stack: { 2 }

  • reverse Reverse a sequence.

    Stack: { 2 }

  • [ 1 + <array> ] Push a quotation for map-index to use later.

    Stack: { 2 } [ 1 + <array> ]

  • map-index Take a sequence and a quotation and apply the quotation to each element of the sequence, collecting the results into a new sequence of the same length. However, it also places the index of the element on top of the stack. Inside the quotation now for the first iteration...

    Stack: 2 0

  • 1 Push 1.

    Stack: 2 0 1

  • + Add.

    Stack: 2 1

  • <array> Create an array from a count and an element.

    Stack: { 1 1 }

  • Now map-index has exhausted its elements. Since we are mapping numbers to arrays, the result will be an array of arrays.

    Stack: { { 1 1 } }

  • concat Concatenate a sequence of sequences together into one sequence.

    Stack: { 1 1 }

  • Now we have produced the second row of Levine's Triangle. times will run the quotation three more times...

    Stack: { 1 1 2 3 }

  • Σ Sum a sequence.

    Stack: 7