Skip to content

Instantly share code, notes, and snippets.

@ebb
Created May 16, 2018 02:24
Show Gist options
  • Save ebb/7bb4f962a72571571735396f90dc8264 to your computer and use it in GitHub Desktop.
Save ebb/7bb4f962a72571571735396f90dc8264 to your computer and use it in GitHub Desktop.
Rosetta Code: Floyd's triangle
\ This program is a Rosetta Code exercise called "Floyd's triangle".
\
\ https://rosettacode.org/wiki/Floyd's_triangle
\
\ The goal is to print out a sequence of numbers in lines of increasing
\ length and to do so in such a way that the numbers appear in aligned
\ columns.
\
\ The program takes a command-line argument that determines how many lines to
\ print.
\
\ Usage example:
\
\ $ ./floyd 5
\ 1
\ 2 3
\ 4 5 6
\ 7 8 9 10
\ 11 12 13 14 15
Iterate {i row col} From {1 1 1}
Let bottom [m + col]
In
When [row <= n]
(repeat [(width bottom) - (width i)]
Func _ (print " "))
(print (Z.show i))
If [col < row]
(print " ")
(print "\n")
If [col < row]
(Continue [i + 1] row [col + 1])
(Continue [i + 1] [row + 1] 1)
End
Where
Let {n m}
Match If [OS.argc = 2] (Z.read (OS.argv 1)) 'nothing
| 'just.n {n [[n * [n - 1]] / 2]}
| 'nothing (OS.die "usage: floyd n")
;
Define (repeat n f)
Iterate i From 1
When [i <= n]
(f)
(Continue [i + 1])
End
Let width [Z.show >> STRING.length]
Let print STDIO.print
Where
Let OS Package "os"
Let STDIO Package "stdio"
Let STRING Package "string"
Let Z Package "z"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment