jueves, 28 de agosto de 2014

My first post on Haskell

So...as I get into functional programming thanks to Erlang...I decided to deep dive into Haskell...a pure functional programming language...

Of course...I'm reading a book for this...so when I finished it...which will take some time as I'm planning to start reading from the very first page again...soon you will read my review on Learn You a Haskell for Great Good...

Now...Haskell is hard...the hardest programming language I have ever dared to learn...and here's a couple of reasons why...
  • Variables are immutable...once assigned they cannot be re-assigned.
  • Functions only accept one parameter and one parameter only.
  • Functions can only call one function at a time.
  • There are no loops.
  • No side effects...everything is pure...a function will always return the same output

There's a really nice picture from XKCD explaining Haskell...


Anyway...Haskell makes you really forget everything you ever learn and make you learn everything again...for a developer like me...that's pure happiness -:)

So...I have done with already Ruby, Python, Julia and Erlang...I wanted my first Haskell application to be a LED Numbers application...so here goes the code...hang to you seat and watch this...BTW...I'm sure the code can be optimized...but I'm just a newbie...and my application works...so who cares about it -;)


LED.hs
showLED :: Int -> IO()
showLED(num) = do
 putStr(concat(addN(get_led(digits num) 1 num) (length(show num)) (length(show num))))

get_led :: [Int] -> Int -> Int -> [[Char]]
get_led (x:xs) n num = make_led_digit(x,n) ++ get_led(xs) n num
get_led [] n num = get_led2(digits num) (n+1) num

get_led2 :: [Int] -> Int -> Int -> [[Char]]
get_led2 (x:xs) n num = make_led_digit(x,n) ++ get_led2(xs) n num 
get_led2 [] n num = get_led3(digits num) (n+1) num

get_led3 :: [Int] -> Int -> Int -> [[Char]]
get_led3 (x:xs) n num = make_led_digit(x,n) ++ get_led3(xs) n num
get_led3 [] n num = []

digits :: Int -> [Int]
digits = map (read . (:[])) . show

addN :: [String] -> Int -> Int -> [String]
addN (x:xs) n ni
 | n > 0 = [x] ++ addN xs (n-1) ni
 | n == 0 = ["\n"] ++ [x] ++ addN xs (ni-1) ni
addN [] _ _ = [] ++ ["\n"]

make_led_digit :: (Int, Int) -> [[Char]]
make_led_digit(0,1) = [" _  "]; make_led_digit(0,2) = ["| | "]; make_led_digit(0,3) = ["|_| "];
make_led_digit(1,1) = ["  "];   make_led_digit(1,2) = ["| "];   make_led_digit(1,3) = ["| "];
make_led_digit(2,1) = [" _  "]; make_led_digit(2,2) = [" _| "]; make_led_digit(2,3) = ["|_  "];
make_led_digit(3,1) = ["_  "];  make_led_digit(3,2) = ["_| "];  make_led_digit(3,3) = ["_| "];
make_led_digit(4,1) = ["    "]; make_led_digit(4,2) = ["|_| "]; make_led_digit(4,3) = ["  | "];
make_led_digit(5,1) = [" _  "]; make_led_digit(5,2) = ["|_  "]; make_led_digit(5,3) = [" _| "];
make_led_digit(6,1) = [" _  "]; make_led_digit(6,2) = ["|_  "]; make_led_digit(6,3) = ["|_| "];
make_led_digit(7,1) = ["_   "]; make_led_digit(7,2) = [" |  "]; make_led_digit(7,3) = [" |  "];
make_led_digit(8,1) = [" _  "]; make_led_digit(8,2) = ["|_| "]; make_led_digit(8,3) = ["|_| "];
make_led_digit(9,1) = [" _  "]; make_led_digit(9,2) = ["|_| "]; make_led_digit(9,3) = [" _| "]

Here's a picture that show how it works...


I will keep learning Haskell and write another blog called "Decimals to Roman - Haskell Style"...stay tuned -;)

UPDATE!

Ok...I must face it...I couldn't leave the application like that knowing that I could make better...here's the new updated version -;)

LED_Fixed.hs
showLED :: Int -> IO()
showLED(num) = do
 putStr(concat(addN(get_led(digits num) 1 num) (length(show num)) (length(show num))))

get_led :: [Int] -> Int -> Int -> [[Char]]
get_led (x:xs) n num = make_led_digit(x,n) ++ get_led(xs) n num
get_led [] n num
 | n < 3 = get_led(digits num) (n+1) num
 | n == 3 = []

digits :: Int -> [Int]
digits = map (read . (:[])) . show

addN :: [String] -> Int -> Int -> [String]
addN (x:xs) n ni
 | n > 0 = [x] ++ addN xs (n-1) ni
 | n == 0 = ["\n"] ++ [x] ++ addN xs (ni-1) ni
addN [] _ _ = [] ++ ["\n"]

make_led_digit :: (Int, Int) -> [[Char]]
make_led_digit(0,1) = [" _  "]; make_led_digit(0,2) = ["| | "]; make_led_digit(0,3) = ["|_| "];
make_led_digit(1,1) = ["  "];   make_led_digit(1,2) = ["| "];   make_led_digit(1,3) = ["| "];
make_led_digit(2,1) = [" _  "]; make_led_digit(2,2) = [" _| "]; make_led_digit(2,3) = ["|_  "];
make_led_digit(3,1) = ["_  "];  make_led_digit(3,2) = ["_| "];  make_led_digit(3,3) = ["_| "];
make_led_digit(4,1) = ["    "]; make_led_digit(4,2) = ["|_| "]; make_led_digit(4,3) = ["  | "];
make_led_digit(5,1) = [" _  "]; make_led_digit(5,2) = ["|_  "]; make_led_digit(5,3) = [" _| "];
make_led_digit(6,1) = [" _  "]; make_led_digit(6,2) = ["|_  "]; make_led_digit(6,3) = ["|_| "];
make_led_digit(7,1) = ["_   "]; make_led_digit(7,2) = [" |  "]; make_led_digit(7,3) = [" |  "];
make_led_digit(8,1) = [" _  "]; make_led_digit(8,2) = ["|_| "]; make_led_digit(8,3) = ["|_| "];
make_led_digit(9,1) = [" _  "]; make_led_digit(9,2) = ["|_| "]; make_led_digit(9,3) = [" _| "]

Greetings,

Blag.
Development Culture.

No hay comentarios: