Skip to content

Instantly share code, notes, and snippets.

@samebchase
Created May 1, 2014 21:40
Show Gist options
  • Save samebchase/3df6238aceaf8d7b32e5 to your computer and use it in GitHub Desktop.
Save samebchase/3df6238aceaf8d7b32e5 to your computer and use it in GitHub Desktop.
Print a triangle
(* http://www.problemotd.com/problem/party-hat/ *)
open Core.Std;;
open Sequence;;
let print_party_hat height =
let hat_width line_no =
2*line_no + 1 in
let print_char_n char n =
String.make n char
|> print_string in
let line_width = hat_width height in
let print_party_line n =
let hat_char_width = hat_width n in
let half_space_width = (line_width - hat_char_width) / 2 in
print_char_n ' ' half_space_width;
print_char_n '*' hat_char_width;
print_char_n ' ' half_space_width;
print_newline ()
in
iter print_party_line (int_range ~start:0 ~stop:(height - 1))
;;
let () =
print_party_hat (int_of_string Sys.argv.(1))
;;
(*
$ corebuild -pkg core,sequence print_triangle.byte
$ ./party_hat.byte 4
*
***
*****
*******
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment