K (Wikipedia), 15 characters:
p:{x{+':x,0}\1}
Example output:
p 10
(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
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1)
It’s also easily explained:
p:{x {+':x,0} \ 1}
^ ^------^ ^ ^
A B C D
-
p
is a function taking an implicit parameterx
. -
p
unfolds (C) an anonymous function (B)x
times (A) starting at1
(D). -
The anonymous function simply takes a list
x
, appends0
and returns a result by adding (+
) each adjacent pair (':
) of values: so e.g. starting with(1 2 1)
, it’ll produce(1 2 1 0)
, add pairs(1 1+2 2+1 1+0)
, giving(1 3 3 1)
.
Update: Adapted to K4, which shaves off another two characters. For reference, here’s the original K3 version:
p:{x{+':0,x,0}\1}