Friday, September 28, 2012

Haskell Network not found (Ubuntu)

So for a bit recently, I've been looking into Haskell. I really want to get into some functional programming. On thing I like doing is network programming because it actually gives me something extra to interact with than my own I/O or bugging a friend to help out or check something out. So I gave a quick glance at sockets and wanted to just see what the types look like on ghci, and of course it cannot find anything related to the network package.

> import Network.Socket

:
    Could not find module `Network.Socket':
      it is not a module in the current program, or in any known package.
Leaving GHCi.
$ ghc-pkg field network exposed-modules
ghc-pkg: cannot find package network

After a few minutes of banging my head against my desk, I say screw the gui package manager and just poke around with apt-cache search. Manage to find the library and for some reason, it was not installed by default. So here's what lead me to the package and the quick and easy fix for it.

$ apt-cache search haskell | grep network
libghc6-network-dev - Haskell network library for GHC
libghc6-network-doc - Haskell network library for GHC; documentation
libghc6-network-prof - Haskell network library for GHC; profiling libraries
$ sudo apt-get install libghc6-network-dev

Then problem solved, hoped over to ghci and it's there.

$ ghc-pkg field network exposed-modules
exposed-modules: Network Network.BSD Network.Socket.Internal
                 Network.Socket Network.URI
$ ghci
> :t Network.Socket.socket
Network.Socket.socket
  :: Network.Socket.Internal.Family
     -> Network.Socket.SocketType
     -> Network.Socket.ProtocolNumber
     -> IO Network.Socket.Socket
> 

So yeah, if you run into this, there you go. I think everyone looking into this can probably figure it out on their own, but if not, hopefully this helps. Edited the command stuff to just remove some information and lots of text in between.

Thursday, September 20, 2012

Recursive Fibonacci

So after bashing my head against the wall working on Fibonacci recursively and thinking "why on earth is this so slow," I realized the solution was reducing everything to a lot of ones and adding them all up. While addition and basic arithmetic for a CPU is exceptionally fast, this was slow due to the magnitude, and would cause way too much recursion for it to even be considered a correct solution. So I searched the web figuring someone far smarter than myself would have not only realized this, but come up with a real solution for it. Sure enough, I found one. The solution they used was in C, so I found it a bit ugly as it required two function calls (so you can call it with one argument). So I converted it to Haskell.

Before posting, the short explanation is this. Fibonacci numbers are found by adding up the previous two numbers. This is not to say you should call it inappropriately like this.

fib :: Integer-> Integer
fib 0 = 0
fib 1 = 1
fib x = fib (x - 1) + fib (x - 2)

However, this is often the solution I see in tutorials about recursion and very legitimate sources. I feel the reasoning for this is to use only one function call and as little code as possible to make an "elegant", or at least readable solution. The solution I found, while it is not the prettiest thing, it is far more efficient and very fast and after taking some time to really read it, makes far more sense true to the derivation of Fibonacci numbers. So here is the solution I found and converted to Haskell.

fib :: Integer-> Integer
fib 0 = 0
fib n =
    let
        fib2 1 _ n2 = n2
        fib2 x n1 n2 = fib2 (x - 1) n2 (n1 + n2)
    in
        fib2 n 0 1

Credit to the solution here. Glad to see people finding valid recursive solutions and letting people like myself know there's a better way.

Tag Cloud

.NET (1) A+ (2) addon (2) Android (2) anonymous functions (1) application (5) arduino (1) artificial intelligence (1) bash (2) camera (1) certifications (3) comptia (4) css (1) customize (8) encryption (1) error (11) exploit (4) ftp (1) funny (2) gadget (3) games (2) GUI (2) hardware (7) haskell (6) help (2) HTML (1) irc (1) java (2) javascript (11) Linux (13) Mac (3) malware (1) math (4) network (5) perl (2) php (3) plugin (2) programming (13) python (9) radio (1) regex (2) security (12) sound (1) speakers (1) ssh (1) story (1) Techs from the Crypt (2) telnet (1) tools (7) troubleshooting (3) Ubuntu (3) Unix (1) virtualization (1) web design (4) Windows (6) wx (1)