sábado, 21 de febrero de 2015

My first post on OCaml

After tasting some Functional Programming with Erlang and Haskell I knew it was time to keep moving forward -;) This time...I choose to learn OCaml -:D

OCaml is a multi-paradigm, imperative, functional, object oriented programming language.

Of course...I'm reading a book to learn about it -:) So will see my review as soon as I finish it...


My first impressions on OCaml is that it looks like Haskell...but with some differences...which makes it akward because I tend to code in Haskell but then realized that some things are quite different...anyway...OCaml seems like a really nice language so far and of course...it's not a pure as Haskell...

As the best way to learn is to code...I build a Fibonacci Sequence Generator...based of course of my previous Haskell code -;)

Fibonacci.ml
open Core.Std

let rec fib num a b = 
 match num with 
 | num when a > 0 && num > 1 -> string_of_int (a + b) ^ " " ^ fib (num - 1) (a+b) a
 | num when a = 0 -> string_of_int a ^ " " ^ string_of_int b ^ " " ^ 
                     string_of_int (a + b) ^ " " ^ fib (num - 1) (a+b) b
 | num -> ""
 
let () =
 print_string "Enter a number: "
 let num = read_int() in
 printf "%s" (fib num 0 1)

When we run it...we're going to see this -;)



Greetings,

Blag.
Development Culture.

No hay comentarios: