miércoles, 5 de agosto de 2015

My first post on Mercury

So...my new language for a couple of weeks...is Mercury -:)

You may ask...what is Mercury?

Mercury looks like Prolog, but feels like strict Haskell or pure OCaml.
 In other words is a logical, functional and object-oriented programming language...

As always...I needed to develop an app to learn how to use it...and while it gave me more than one headache...here it is -;)

fibo.m
:- module fibo.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module string, int.

:- pred fibo(int::in, int::in, int::in, string::out) is det.

fibo(NUM, A, B, FIBS) :-
 ( 
  if A = 0 
  then fibo(NUM-1,A+B,B,FIB), FIBS = int_to_string(A) ++ " " ++ int_to_string(B) ++ 
                                     " " ++ int_to_string(A+B) ++ " " ++ FIB
  else if A > 0, NUM > 1
  then fibo(NUM-1,A+B,A,FIB), FIBS = int_to_string(A+B) ++ " " ++ FIB
  else FIBS = ""
 ).
 
main(!IO) :-
 io.write_string("Enter a number: ",!IO),
 io.read_line_as_string(Result, !IO),
 ( if
   Result = ok(String),
   string.to_int(string.strip(String), N)
  then
   fibo(N,0,1,FIBS),
   io.write_string(FIBS,!IO)
  else
   io.write_string("Not a number...",!IO)
 ).

The cool thing about Mercury is that it forces you to not allow your app to fail...as the "else" is mandatory...if you enter a number then it's fine...but if you enter a letter, you are forced to do something...

Here are the pics...



 Now...I just need to break my head a little bit more trying to make my LED_Numbers app -:D

Greetings,

Blag.
Development Culture.