Showing posts with label Mercury. Show all posts
Showing posts with label Mercury. Show all posts

Monday, August 10, 2015

LED es mi nuevo Hello World - Tiempo de Mercury

Este fué un poco complicado y más extenso de lo que había planeado...además...me forzó a pasar bastante tiempo leyendo la documentación puesto que no hay muchos tutoriales o libros sobre Mercury...

En fín...a las finales está funcionando bien, así que estoy bastánte feliz -:)

Mercury tiene muchas funciones bastante convenientes...solo tienes que encontrárlas -;)

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)
 ).


En fín...aquí están las pantallas -;)



Espero que les guste -;)

Saludos,

Blag.
Development Culture.

Wednesday, August 05, 2015

Mi primer post en Mercury

Así que...mi nuevo lenguaje para el próximo par de semanas...es Mercury -:)

Seguro se preguntarán...qué es Mercury?

Mercury se ve como Prolog, pero se siente como un Haskell estricto o un OCaml puro.
En otras palabras, es un lenguaje de programación lógico, funcional y orientado a objetos...

Como siempre...necesitaba crear una aplicación para poder aprender a usarlo...y a pesar de que me dió más de un dolor de cabeza...aquí está -;)

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)
 ).

Lo que me encantá de Mercury es que te obliga a no dejar que tu aplicación falle...puesto que el "else" es obligatorio...si ingresas un número entonces está bien...pero si ingresas una letra, estás obligado a hacer algo...

Aquí están las imágenes...



Ahora...solo tengo que romperme la cabeza un poco más tratando de hacer mi aplicación de Números LED -:D

Saludos,

Blag.
Development Culture.