Last active
March 22, 2018 23:17
-
-
Save rdavison/5057897215fe79e960213cb8b0b4f561 to your computer and use it in GitHub Desktop.
Implements the same diamond printing algorithm using the y-combinator (fix). Needs to be compiled with the -rectypes flag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Core; | |
let fix = (f, g) => ((x, a) => f(x(x), a))((x, a) => f(x(x), a), g); | |
let make_line = (n, width) => { | |
let blanks = String.make((width - n) / 2, ' '); | |
let stars = String.make(n, '*'); | |
blanks ++ stars ++ blanks ++ "\n"; | |
}; | |
let make_rest = (width, step, combine, f, n) => | |
if (n <= 0) { | |
""; | |
} else { | |
let rest = f(step(n)); | |
let this = make_line(n, width); | |
combine(rest, this); | |
}; | |
let diamond = n => { | |
let concat = (++); | |
let step = n => n - 2; | |
let above = make_rest(n, step, concat); | |
let below = make_rest(n, step, Fn.flip(concat)); | |
let this = make_line(n, n); | |
String.concat([fix(above, step(n)), this, fix(below, step(n))]); | |
}; | |
let main = { | |
let n = Sys.argv[1] |> int_of_string; | |
printf("%s", diamond(n)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment