Wednesday, April 21, 2010

Blog changes and haskell exercise

Hey there ! Just as I've promised I've came up with some Haskell exercises (with answers). To do that I had to post some code, which is not really convenient on Blogger. Fortunately there is a solution ! What I've found on the web is easy syntax highlighting for blogger.
Enjoy !

So now with the exercises :

1. Pretty standard problem - find all prime numbers up to a number given in the argument.
2. Function that takes a function (two numbers as arguments) and a list of numbers and returns a list of those two argument functions but already applied to numbers in the list given. That way we gain a list of functions taking one number.
3. Find all automorphic numbers up to a given number.
4. Rewrite the following functions so they use (.) operator :
one = sum ( takeWhile (<10000) (filter isPrime  [1..]) )
two = map (\x -> cos (sqrt x))

allPrime :: (Integral a) => a -> [a]
allPrime range = filter isPrime [2..range]

isPrime :: (Integral a) => a -> Bool
isPrime x = not (isNotPrime 1 x)

isNotPrime :: (Integral a) => a -> a -> Bool
isNotPrime dv nr
    | dv == nr = False
    | dv == 1 = False || isNotPrime 2  nr
    | otherwise = nr `mod` dv==0 || isNotPrime (dv+1) nr
partialL ::( Num a ) => (a-> a -> b) -> [a] -> [ a->b ]
partialL _ [] = []
partialL f (x:xs) = f x : partialL f xs
Usage :
let bla = partialL (\x -> \y -> x+y) [1,2,3,4]
map ($ 2) bla

automorphic :: Int -> [Int]
automorphic y = [ x | x <- (take y [1..]), show x == (splitter x) ]

splitter :: (Num a) =>  a -> [Char]
splitter x = snd (  splitAt ( length (show sq) - length (show x) ) (show sq)  )
 where sq = x^2

one = sum . takeWhile (<10000) . filter isPrime  [1..]
two = map (cos . sqrt)

Sunday, April 18, 2010

Ferrari F40 robot

Hey, I haven't been writing for a while - that's because I've been caught up with a project we (my group) have been assigned on Computer Systems Architecture class. The assignment was to build a robot that was supposed to drive through an 8-shaped track marked with white paper boarders marked black. To do that we had to steer him through LPT port using a computer and 8259 for interrupt handling. The interrupts occurred when the sensors, positioned in the front left and right of the robot, had changed state. After that the software was supposed to read their state from STATUS(input) pins of the LPT and set proper signals on DATA(output) pins so the engines steer our robot in the right way.

The whole thing took us combined of one week work. The most problems we had with electronics, especially with engine circuitry. We have melted a lot transistors. I was responsible mostly for software (written in C and partially assembler).

That's the (almost) final state of the robot. As you can see it's build on  Ferrari F40, red plastic.

 In case you ever wanted to send something to 8259, 8253 or your LPT port here's a code snippet :

void aoutPort(short port,short data)
    asm cli
    asm mov dx, port
    asm mov ax, data
    asm out dx, ax
    asm sti

short ainPort(short port)
    short tmp;
    asm cli
    asm mov dx, port
    asm in ax, dx
    asm mov tmp, ax
    asm sti
    return tmp;

It does work on a computer with DOS or FDOS operating system. Ofcourse if your using FDOS/DOS you could also use dos.h and outport and inport functions already implemented. Some groups last year had a similar task and used Linux.

And here is an exemplary code we used to update the system remembering the previous state of the sensors and detecting a change.

void refreshDiode()
    aoutPort(DATA, lastEng+lastAck+lastLight);
    aoutPort(DATA, lastEng+lastAck+lastLight);

ACK_ON is defined as 32 because we use DATA5 pin on the LPT, DATA is defined as 0x378 which is the address of data line in LPT port. We don't want to change the state of the other DATA pins so we add the state of the engine and light pins (D0-D4).

The next assignment is to use a micro-controller instead of a computer - the robot will be autonomous then. The down side is we'll have to use only assembler to program it.

Monday, April 5, 2010

Happy Easter and some more Haskell

Happy Easter everyone ! :)

Last time I've written about good ways to learn Haskell, now I'd like to present why should you tackle this language.
  1. Its purely functional. If you have no idea what that means you can visit wiki or this page. Some most notable characteristics are : 
    • recursion is used instead of loops,
    • everything you can do with some data can also be done with functions (you can say that functions are 'first class') - this is pretty awesome,
    • 'side-effects' are very visible (something I'd like when working with OpenGL),
    • based on Lambda Calculus (explanations of the last two you could find here),
    • (...).
  2. The first argument may not be convincing very much - there are lots of stuff you won't ever understand, why get into lambda ? For a standard Programmer, the bread and butter are languages of imperative or object orientation. It turns out that some of these languages, like C#, Python, possibly Java, are coming with functional-like ways of programming. It may be tricky to understand at first and some say its the best to learn it in a pure form.
  3. Haskell can be and is used in real projects.
  4. It is very efficient. Maybe not as fast as C, but the development is faster. Usually the speed up C would give you is not that big compared to efficiency you would gain by implementing a better algorithm. I've seen (rather small in number of code lines) some tests showing that Haskell beats languages like Python and Ruby by a lot.
  5. Some people, for example Simon Peyton, call it the most beautiful language.
  6. A new look at the concurrent computing with Software Transactional Memory (STM). Simon Peyton Jones in his chapter of Beautiful Code shows the faulty of achieving multi-threading by using locks and gives a solution to the 'Santa' problem using Haskell. I'll surely write about this, when I find out more. Some (small) tests I've seen prove its efficiency and elegance. This is the aspect of Haskell that I'm personally most excited about.
I suppose there are a lot more reasons to learn Haskell. I've name only these 6 due to lack of my knowledge. Please post your own reasons for learning this language.

I've promised some Haskell-exercises and I shall post some in the near future.

Sunday, April 4, 2010

Learning Haskell

I've had a try with Haskell, at the begining of my studies with little success. Now I'm getting back to it.

I've started with some reading. Mostly Gentle Introduction to Haskell 98 and Learn you a Haskell , these two I find complementing themselves - the first one being more theoretical, the latter more practical.
Unfortunately that has not given as much as I hoped. I realised that if I was given a certain task to do in Haskell I'd have to begin with a long scratch on my head and a long Internet search.
I realised that what I need is some exercise.

This is what I've found so far to help me with that : 6 problems and 99 problems.
I've done a bit of both and now I feel more like a Haskell Programmer. More like 0.0002 per mill more. ;)
99 problems are really problems for Prolog (a declarative programming language) solved in Haskell. I've had to learn some Prolog 2 semesters back thus I'm not that excited to be doing them.

I will definitely look for more and post some problems of my own (with solutions) soon.

First Post

I'm finding myself hitting backspace and deleting what I've written, looking for better thoughts. That's because its my First Post and its kind of awkward. The fact that I'm not a native speaker is definitely not helping. Using English instead of Polish I'm hoping for a broader audience. Also a bit of practice never hurts, right ?

This is something I've been wanting to do for quite some time. Create my own blog that is. The reasons ? Quite a few so I'm gonna dot out some of  them :

  • Blogs are cool,
  • I'd like to express myself some more and drawing is not an option.
The next ones touch the title of my blog, which is "Programmer wannabe" :
  • Programming is cool,
  • I'd like my path to becoming a Programmer to be 'saved' somewhere, what place better that The Internet ?
  • I'd like others to know they are facing similar problems and maybe even find some answers here.
What will my blog be about ? A bit about me and a bit more about programming. The languages I'm interested in at the moment are (in priority) : Java, Haskell, C/C++ and Python. If I had to order them by the level I am proficient in them, it'd be something like this : C/C++, Java, Python, Haskell.

Other things which I'm interested in and you are probably not are games, books, anime and some other stuff.
I might mention some of them on occasion.

Enough for now. Can't wait for my next, more concrete post, so I'm gonna finish now. Probably get back to some explanations later.

One last thing : please don't be too outraged by the mistakes I'll (or already did) make. Although I'd love you to correct me if I'm wrong in any (linguistic/syntactic/conceptional/other) way.