lunes, 29 de marzo de 2010

Scheme and home-made objects

Scheme is functional. Functional and minimalist.

But its power to build higher abstractions with the basic toolset it provides, allows us to build a pseudo Object oriented system by ourselves.

That's what SICP 3.1 - 3.4 exercices are about:

Using the closure property we have learnt about in chapter 2, and the just revealed set! function, we can build objects with private state to the world.

We'll use the classical bank account exercice. We create it with a initial amount of money and we can deposit money and withdraw money (iif we have enough money there).

At the end of the 4 exercices we should have a way to generate password protected bank accounts, which remember not only the password but how many wrong passwords you have entered in a row. If you fail thrice, it'll call the cops.

To solve this little problem (that, on the other hand I love because of its well defined objectives and dose of reality) we'll first have a make-monitored function, that counts how many times it's been called. make-monitored procedure gets a procedure as a parameter, and outputs a prodecure that is like the one entered, but with the counter stuff added. we could say it makes wrapped functions (decorated if you come from python).

The password protected account uses just an extra local variable to the closure that has the password.



the usage is like

(define pw-acc (make-protected-account 'secretp4ss 100))
((pw-acc 'secretp4ss 'deposit) 10) => ok
((pw-acc 'wr0ngpass 'deposit) 10) => fail
((pw-acc 'wr0ngpass 'deposit) 10) => fail
((pw-acc 'wr0ngpass 'deposit) 10) => call-the-police


I don't like that when a wrong pass is passed as an argument, the output is applied to the argument (10 in this case), and scheme doesn't know how to apply it.

I've worked partial solutions but none convinced me one being flattening the way to call the functions (pw-acc 'pass 'deposit 10) but this way, the password protected account had to know about how many parameters received each function. The other solution is returning a dummy functions that accepts one parameter in case of wrong password. Unfortunately, here we just accept 1 parameter, so we are limited too.

Any suggestions are very welcome

viernes, 26 de marzo de 2010

Let's go everywhere

Last month has been a pretty busy one.

Breakout 4.0, erlang course, Barcelona.PM, Valencia (Fallas) gts, and some hard partying. It's quite difficult to make a synopsis of all the new info and thoughts I've been flooded with, but anyway, this is not a personal explain-my-life blog, or is it? anyway.

On the hands-on techincal side, I've started a web using Pier. I've never worked with any cms to do anything productive, and I decided to use pier due to my mental commitment to smalltalk. It's one of the best languages I've found, and I'd like to learn it well, not only to do a few hacks here and there.

I've been reading The last seaside book, which goes right to the point (IMHO) and is pretty well structured (maybe too verbose at times, but well).

Being a great language itself, smalltalk has a few drawbacks that make me think of going to something more mainstream.

Probably it's more a problem of me being unexperienced in this environment than a problem of pier or smalltalk on their own, but anyway.

Image based persistance is enough for me, but in case I needen some db backend, using magma is not interoperable with other systems, so data is closed to the world.

Apart from that(and random hangs) creating a new class for products, a template view for them, and edit view is really easy, introspection friendly, and fun.

I've been dabbling a bit with tornado, a python minimalist framework to develop simple and fast RESTful Webapps(similar to Dancer and Mojolicious) I'm on a secret project for world domination, and it'll be in python (bummer, I always thought world domination would be in Perl.

miércoles, 24 de marzo de 2010

Ada Lovelace day

Ada Lovelace Day is an international day of blogging to draw attention to women excelling in technology.

I could think of several women that have done really amazing work in technological side (audreyt,etc.). I have read some technical books written by women too (Smalltalk-80 The language by Adele Goldberg).

But for me, being involved in software projects is the first step to be (maybe) a great software programmer.

So my 'tribute' to these (still today) pioneer women involved in a world traditionally forbidden to them is a talk by Kirrily Robert at OSCON '09 called "Standing out in the crowd". It's quite interesting IMO. Here are some notes on the talk.

viernes, 12 de marzo de 2010

Finite automata in erlang... well, in perl

I've been hacking some erlang after the third class with Johan Montelius. Last lecture was about failure handling, exeptions, linking and monitoring processes.

I've been tempted to translate some classical problems of concurrent or parallel computing to erlang. First chapters in some books are about what they call LTSA. They are mostly state machines, that can be composed, concatenated, intersected, and some other operation.


At first, I started doing an erlang little program that has three states, and receives messages to travel from one state to other.

Here's the basic code for the hello->converse->goodbye LTSA


Theres's a couple of things to note here:
  • Erlang syntax is a bit tricky. so many different line endings (, ; . nothing).
  • It's an ad-hoc code, but it follows a clear pattern.

Then, I thought about going meta. Why can't we build a program builder, that with a simplifyed input of our desired behaviour, writes erlang code?

I've used perl to hack a simple script that builds the code structure (or the whole program depending on your needs). I won't have to tell you it's quite unstable, and you can make it produce failing codes. Of course, it's quite shaky, but well... Just for the fun of it.

Try it, play with it, break it, or fix it. I don't think it can be useful for anything in this state, but maybe implementing composing functions...

For the moment the program just gets information on states, transitions, and destination states.
Then it builds a function for every state, and builds a receive statement, with each possible transition, and and ending _ -> fun to discard unrecognized messages. Keep in mind it won't keep order, so non disjoint cases aren't guaranteed to work. When entering a function, it's name (and actual parameters) are printed on the screen.


Yeah, the perl syntax highlight problem... try here to see a hightlighted version.

Check out my other erlang posts

lunes, 8 de marzo de 2010

My name is erlang

I've been introduced to erlang by Prof.Johan Montelius.

I'm enjoying the lectures a lot (the two so far). He's a great speaker (is's quite fun to attend classes) and he managed to explain the basic concepts of concurrent erlang programming in 2 hours (provided you already knew the basic erlang syntax).

I'm taking some notes at class, and I'll eventually clean them and upload them in a blogpost format.

For the moment, here is a silly code I wrote the other day while I was in the bus.

It's interesting how:

  • Concepts learnt previously in scheme and perl (Higher Order Perl ftw!) become useful and appliable here.
  • Pattern matching is stressed to become the basis of method dispatch (with or without guards), variable binding, if/case/any_conditional and message receiving.
  • The Message Passing system is GREAT. Not just the "forget about low level sockets" but the mailbox
    system of each process, and the transparency that frees you from thinking low level.
  • Being a functional language, it does tail call optimization, and in most cases you better take advantage of it, because processes (if you think of them as DFA's) have a long life, and they just travel from one state to other by calling themselves recursively with an updated argument (you can
    pass the state along through the function arguments).


Here's the code. Just a Proof of concept. Two processes sending pings and pongs one to the other. Pretty simple, but there's pretty much a synopsis of the erlang (ugly) syntax and expressions.

miércoles, 3 de marzo de 2010

Hola

Este es un saludo a toda la gente que nunca saludo, a las personas que
nunca saludaré y a los que no he saludado.

A aquella gente que me encuentro en el bus, cada día, y casi ni nos
miramos. A los que estan en la puerta del edificio, lloviendo y
pasando frío, y compartimos un cigarro de silencio. A quién lleva un
libro en la mano que he leído y me gustó, y podría pasar horas
hablando de él. Pero no nos decimos nada.

A un telefono que acabo de descolgar, y me he quedado mudo para que no
me vendieran enciclopedias ("hola buenas tardes" - han dicho). A los
que tengo al lado en un concierto de Mamá Ladilla.

A los que hacen guasas de Top Secret y Mucachada Nui en la mesa de al
lado en un bar.

A los visitantes silenciosos del blog,

Personalmente necesito empezar a decir "hola", porque por lo general,
el día a día me lleva al asco más absoluto. Mientras digo "hola", no
me estoy cagando en, ni arrepintiendo de, ni pensando si, ni
conspirando contra.

Hola.

Ala, aquí el temazo del dia. Estranged.