Last active
March 22, 2018 22:41
-
-
Save rdavison/3cadd2e1e3bfd2df4c48ec23b49b4750 to your computer and use it in GitHub Desktop.
Prints diamonds to the screen
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 make_line = (n, width) => { | |
let blanks = String.make((width - n) / 2, ' '); | |
let stars = String.make(n, '*'); | |
blanks ++ stars ++ blanks ++ "\n"; | |
}; | |
let rec make_rest = (n, width, concat) => | |
if (n <= 0) { | |
""; | |
} else { | |
let rest = make_rest(n - 2, width, concat); | |
let this = make_line(n, width); | |
concat(rest, this); | |
}; | |
let diamond = n => { | |
let concat = ( ++ ); | |
let make_rest = make_rest(n - 2, n); | |
let this = make_line(n, n); | |
String.concat([ | |
make_rest(concat), | |
this, | |
make_rest(Fn.flip(concat)) | |
]); | |
}; | |
let main = { | |
let n = int_of_string(Sys.argv[1]); | |
printf("%s", diamond(n)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment