Rebol3 Code Examplex


Pascal's triangle

Generate the triangular array where each number is the sum of the two directly above it, often used for binomial coefficients and related patterns.

Rebol [
    title: "Rosetta code: Pascal's triangle"
    file:  %Pascal's_triangle.r3
    url:   https://rosettacode.org/wiki/Pascal's_triangle
]

pascal-triangle: function [
    "Prints the first N rows of Pascal's triangle"
    rows [integer!] "Number of rows to print"
][
    row: #(u32![ 1 ])     ;; current row, starts with [1]
    loop rows [
        print row
        left:  copy row
        right: copy row
        insert left  0    ;; shift right  → [0 1 2 1]
        append right 0    ;; shift left   → [1 2 1 0]
        row: left + right ;; element-wise sum → next row
    ]
]

pascal-triangle 7

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1