lunes, 10 de agosto de 2015

LED is my new Hello World - Mercury Time

This one was a little bit tricky and longer than I expected...it also...forced me to spend a lot of time reading the documentation as there aren't many tutorials or books about Mercury...

Anyway...in the end it's working fine, so I'm quite happy -:)

Mercury has a lot of pretty convenient functions...you just need to find them out -;)

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

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

:- implementation.
:- import_module list, string, int, array, char.

:- pred get_leds(list.list(character)::in, list.list(character)::in, 
                 int::in, list.list(string)::out ) is det.
 
get_leds(LDIGITS, LDIGITS_AUX, N, RESPONSE) :-
 (
  LEDS = array([array([" _  ","| | ","|_| "]),
                array(["  ","| ","| "]),
                array([" _  "," _| ","|_  "]),
                array(["_  ","_| ","_| "]),
                array(["    ","|_| ","  | "]),
                array([" _  ","|_  "," _| "]),
                array([" _  ","|_  ","|_| "]),
                array(["_   "," |  "," |  "]),
                array([" _  ","|_| ","|_| "]),
                array([" _  ","|_| "," _| "])
               ]),
  list.length(LDIGITS,LEN),
  ( if LEN > 0 then
   HEAD = det_head(LDIGITS),
   TAIL = det_tail(LDIGITS),
   char.to_int(HEAD, HEAD_I:int),
   HEAD_N:int = HEAD_I - 48,
   LINE = elem(HEAD_N, LEDS),
   SUB_LINE = elem(N, LINE),
   get_leds(TAIL, LDIGITS_AUX, N, RESULT),
   RESPONSE = [SUB_LINE] ++ RESULT
    else if N < 2 then
   get_leds(LDIGITS_AUX, LDIGITS_AUX, N+1, RESULT),
   RESPONSE = ["\n"]  ++ RESULT
    else if N = 2 then
   RESPONSE = ["\n"]
    else
   RESPONSE = [] )
 ). 
 
main(!IO) :-
 io.write_string("Enter a number: ",!IO),
 io.read_line_as_string(Result, !IO),
 ( if
   Result = ok(String),
   NUM = string.strip(String),
   to_char_list(NUM,LDIGITS),
   get_leds(LDIGITS, LDIGITS, 0, RESPONSE)
  then
   io.write_string(string.join_list("", RESPONSE), !IO)
  else
   io.write_string("Not a number...",!IO)
 ).


Anyway...here are the screens -;)



Hope you like it -;)

Greetings,

Blag.
Development Culture.

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.