lunes, 2 de marzo de 2015

LED is my new Hello World - OCaml Time

Learning OCaml is not easy...I have to admit that...but I'm moving forward -;) So, here's my take on LED Numbers...

LED_Numbers.ml
open Core.Std

let get_leds number =
let leds = [0, [" _  ";"| | ";"|_| "];
            1, ["  ";"| ";"| "];
            2, [" _  ";" _| ";"|_  "];
            3, ["_  ";"_| ";"_| "];
            4, ["    ";"|_| ";"  | "];
            5, [" _  ";"|_  ";" _| "];
            6, [" _  ";"|_  ";"|_| "];
            7, ["_   ";" |  ";" |  "];
            8, [" _  ";"|_| ";"|_| "];
            9, [" _  ";"|_| ";" _| "]] in
 for i = 0 to 2 do
  for j = 0 to String.length(number) - 1 do
   let line = List.Assoc.find_exn leds 
        (int_of_string(Char.to_string(number.[j]))) in
   printf "%s" (List.nth_exn line i)
  done;
  print_string "\n"
 done
 
let () =
 print_string "Enter a number: "
 let num = read_line() in
 get_leds num

The funny thing is that at first I tried to translate my previous Julia and Go codes...but then I remembered that even when OCaml can be made into some sort of an imperative language...it's actually Functional in its core...so I said...Ok...I need to reuse my Haskell code...but somehow I start thinking about a new whole different way of doing it...and I think I came out with a more cleaner and well done version...which is something that only learning a new programming language can give you...a new way of thinking about old problems -;)

Here are the screenshots as always -:)



Greetings,

Blag.
Development Culture.


No hay comentarios: