Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts

14 November 2009

Why I love Sage in 6 simple lines

This is why I love Sage:

e = sqrt(log(4*B) / log(P)) + (5 / (4*d))

g = [P^(a - i) * (x*B - R)^i for i in range(a)]
h = [(x*B - R)^a * (x*B)^i for i in range(d - a)]

m = matrix(Integers(), d, [list(k) + [0] * (d - len(list(k))) for k in g+h]).LLL()

w = sum([x^i * m[0][i] for i in range(d)])

return filter(lambda x: x[0].is_integral() and gcd(P, x[0] - R) < P**e , w.roots())

(That's a crucial part of my thesis.)

06 June 2009

I got to level one at Project Euler (and would like to thank my boss and everyone...)

I go to level one at Project Euler! I got this pretty picture as a reward:


Now what does this have to do with my boss...

First, let's precise that the term "my boss" means "those guys who make the decisions and I don't know which one decides what" - no sucking up! So, "the boss" transferred me to a Python project and with a bunch of guys, we're learning Python.

Remember how I wrote I started learnig Python by solving Project Euler problems? Well, that's what we're told to do at work! There's a contest going on, in which many problems are from there, so when I solved one, I submitted it both to work and Project Euler and it was the last one I needed to get to level one, so here I am.

Can something be more motiviating than a contest at work? I have to code...

28 February 2009

Ocaml: using Big_int, operator overloading and functors for dummies

I'm working on a functional programming project. I have encountered a few difficulties, so I will post here a few things that took me quite a while (and a lot of luck) to find. Especially operator overloading, where Google lead me only to endless discussions about wether it should exist or not and why it doesn't, when it actually does.

I will write a code snippet that will allow us to use integers and Big_ints the exact same way, for instance doubling them using one function named "twice". (In other words, putting two and two together with 4 modules and a functor - can something be more fun?)

First, let's get to the Big_ints:


    1 #load "nums.cma";;


    2 open Big_int;;




So far so good. Now, we'll declare two modules that can add numbers, one for integers, one for the Big_ints, but first, they'll need a signature:

    4 module type Add_sig =


    5   sig


    6     type t;;


    7     val (+): t -> t -> t


    8   end;;




Now, the modules. Notice how cool functional programming is, you can assign functions like other values:

   10 module Int_modl=


   11   struct


   12    type t = int


   13    let (+) = (+);;


   14  end;;


   15 


   16 module BigInt_modl =


   17   struct


   18     type t = big_int


   19     let (+) = add_big_int;;


   20  end;;



The "let (+) = (+)" looks stupid, but is required.

Now we'll write a functor to transform these two modules in two new modules that have a richer functionality (okay, "twice" is not "rich", but that's just an example and I hope you can see the big picture), that we will implement only once, though int and Big_int are two completely different data types and have completely different additions ("+" and "add_big_int") - again, notice how cool functional programming is.

   22 module Add =


   23  functor (Modl : Add_sig) ->


   24   struct


   25     type t = Modl.t


   26     let (+) = Modl.(+)


   27     let twice t = t + t;;


   28   end;;




Nice functor. Let's use it to create the previously mentioned modules and we're almost there:

   30 module Ints = Add(Int_modl);;


   31 module Bigs = Add(BigInt_modl);;




Okay, time to see if that works:

33 Ints.twice 2;;


34 int_of_big_int (Bigs.twice (big_int_of_int 2));;



We have a bit of hassle with creating and printing the Big_ints (thus the longer code), but in the end, we defined one function "twice" that uses the same operator "+" to perform technically completely different operations (with an emphasis on technically - you could use this technique to make "+" mean subtraction or multiplication for the integers and that would be nasty).

Finally, the entire code, ready to copy, paste and... run!

#load "nums.cma";;


open Big_int;;


 


module type Add_sig =


  sig


    type t;;


    val (+): t -> t -> t


  end;;


 


module Int_modl=


  struct


   type t = int


   let (+) = (+);;


 end;;


 


module BigInt_modl =


  struct


    type t = big_int


    let (+) = add_big_int;;


 end;;


 


 module Add =


 functor (Modl : Add_sig) ->


  struct


    type t = Modl.t


    let (+) = Modl.(+)


    let twice t = t + t;;


  end;;


 


module Ints = Add(Int_modl);;


module Bigs = Add(BigInt_modl);;


 


Ints.twice 2;;


int_of_big_int (Bigs.twice (big_int_of_int 2));;


19 December 2008

Yet another programming language

I blogged about learning Java and C#, and here I am again, learning a new language again: Python.



They made me. At work. First, I was a bit mad: I'm learning Objective Caml at school, C# for the 70-536 exam, can my brain take much more?

But as soon as I tried it, I loved it for the easy syntax and operations on lists. It also supports supports multiple programming paradigms (primarily object oriented, imperative, and functional) - how cool is that?


Okay, so I started learning. First thing I did was, of course, "Hello, World!". Then, my new favourite way of learning programming languages - Project Euler! That was cool - doing something I love, learning and getting paid! Ain't I a lucky geek?

26 November 2008

My new addiction: Project Euler

I've got a new dangerous addiction: Project Euler. It's a website with cool math problems, like:

Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.

These problems are quite easy to solve with a little piece of code, so it's great way of learning a new programming language. I try using that to learn ocaml for my functional programming class, but sometimes I forget about that and succumb to the tentation of writing a C# console application with great ease.

I have solved 12 problems out of 218 so far and when I start solving them, it's hard to stop! Yep, one more addiction...