aleix is currently certified at Journeyer level.

Name: Aleix Conchillo
Member since: 2001-08-06 14:27:22
Last Login: 2011-01-10 12:52:54

FOAF RDF Share This

Homepage: http://hacks-galore.org/aleix/

Notes:

I used to have some info in this part of the diary, but I don't really know what to say now...

Projects

Recent blog entries by aleix

Syndication: RSS 2.0

Tweak your Guile REPL

If you are new to Guile and start the REPL right away you will find yourself with no history, no completions, no colors and even the cursor keys not working as expected.

Fortunately, all these problems can be solved by loading a couple of modules.

The first one is to enable readline support. This will enable command history, completions (pressing TAB), cursors will work properly, you will have parenthesis matching, Emacs key bindings support and more.

Simply put this in ~/.guile:

(use-modules (ice-9 readline))
(activate-readline)

To colorize the REPL there is also an awesome new module, nala-repl. This module doesn't come with Guile yet, so you have to clone the repository and install it yourself:

$ git clone git://github.com/NalaGinrut/nala-repl.git
$ cd nala-repl
$ sudo make install

And, as before, add the following lines to your ~/.guile:

(use-modules (nala colorized))
(activate-colorized)

nala-repl provides some more nice modules, make sure to read the README file.

Now, go and enjoy the Guile REPL!

Syndicated 2013-04-30 22:31:52 from aleix's blog

32-bit and 64-bit return values in OS X

I've spent a couple of days with a silly compiler problem. I have to admit that it was my fault but, at least, I learned something in the process. To make a long story short: do not forget to include header files and enable all warnings (-Wall).

In C, you can use a function without including its header file. Then, depending on how you set the compiler flags, it might not complain as long as it can find the function at linking time.

Imagine you have this function (defined in foo.h and implemented in foo.c) :

unsigned long foo_add (void);

and you use it in another module bar.c without including the foo.h header:

//#include "foo.h"

unsigned long bar_add (void)
{
  return foo_add () + foo_add ();
}

Compile with: gcc -c -O2 bar.c (we just missed -Wall)

If we disassemble the code of bar.o we get:

$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_bar_add:
...
000000000000000b callq _foo_add
0000000000000010 movl  %eax, %ebx
...
0000000000000024 ret

Now, let's try to include the header foo.h and disassemble again:

$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_bar_add:
...
0000000000000009 callq _foo_add
000000000000000e movq  %rax, %rbx
...
000000000000001f ret

Note how the compiler uses two different instructions: movl (32-bit) in the first case, when it really doesn't know anything about the foo_add function, and movq (64-bit) when it knows that foo_add returns a 64-bit value.

This can lead to unexpected behavior as I have found in the code I was working on. And actually, I have only found this in OS X.

So, please do not forget to include header files and enable -Wall. It will save you from some headaches.

Syndicated 2013-04-25 00:01:08 from aleix's blog

32-bit and 64-bit return values in OS X

I've spent a couple of days with a dummy compiler problem. I have to admit that it was my fault but, at least, I learned something in the process. To make a long story short: do not forget to include header files and enable all warnings (-Wall).

In C, you can use a function without including its header file. Then, depending on how you set the compilers flags, it might not complain as long as it can find the function at linking time.

Imagine you have this function (defined in foo.h and implemented in foo.c) :

uint64_t foo_add (void);

and you use it in a program bar.c without including the foo.h header:

#include <stdint.h>
#include <stdio.h>

int
main (int argc, char *argv[])
{
  printf ("%d\n", foo_add ());

  return 0;
}

(yes, printf will complain in this case because of type mismatches, just imagine printf is something else).

If we disassemble the code of our program we get:

$ otool -tV bar
bar.o:
(__TEXT,__text) section
_main:
...
0000000000000006 callq  _foo_add
...
0000000000000012 movl   %eax, %esi
...
000000000000001e ret

Now, let's try to include the header foo.h and disassemble again:

$ otool -tV bar
bar.o:
(__TEXT,__text) section
_main:
...
0000000000000004 callq  _foo_add
...
0000000000000010 movq  %rax, %rsi
...
000000000000001d ret

Note how the compiler uses two different instructions: movl (32-bit) in the first case, when it really doesn't know anything about the foo_add function, and movq (64-bit) when it knows that foo_add returns a 64-bit value.

This can lead to unexpected behavior as I have found in the code I was working on. And actually, I have only found this in OS X.

So, please do not forget to include header files and enable -Wall. It will save you from some headaches.

Syndicated 2013-04-24 22:56:42 from aleix's blog

add1, sub1 and + recursive process

In The Little Schemer you are asked to write the function + using the functions zero?, add1 and sub1, such as the result of (+ 46 12) is 58.

(define add1
  (lambda (n) (+ n 1)))

(define sub1
  (lambda (n) (- n 1)))

The solution given is:

(define o+
  (lambda (n m)
    (cond
      ((zero? m) n)
      (else (add1 (o+ n (sub1 m)))))))

which is correct but has a little problem.

If you are like me (hopefully not), you might have read twenty times the first chapters of SICP (and haven't gone any further). Remember about recursive and iterative processes and recursive procedures? In our o+ case we have a recursive procedure that generates a recursive process, as the process generates a chain of deferred add1 operations.

With a recursive process we can easily blow our stack:

scheme@(guile-user)> (o+ 10 100000)
While executing meta-command:
ERROR: Throw to key `vm-error' with args `(vm-run "VM: Stack overflow" ())'.

This is because this will generate:

(add1 (add1 (add1 (add1 ...... n))))

with as many add1 as m.

So, how to improve this? With a recursive procedure that generates an iterative process:

(define o+
  (lambda (n m)
    (cond
      ((zero? m) n)
      (else (o+ (add1 n) (sub1 m))))))

which will generate:

(o+ 11 99999)
(o+ 12 99998)
...
(o+ 100010 0)

and doesn't overflow the stack.

scheme@(guile-user)> (o+ 10 100000)
$1 = 100010

Yes, this is a tail-recursive function.

Syndicated 2013-04-13 14:56:15 from aleix's blog

The Law of Car

In The Little Schemer, The Law of Car is defined as:

The primitive car is defined only for non-empty lists.

In the implementation given for (firsts l), it seems to me that the law is broken:

(define firsts
  (lambda (l)
    (cond
      ((null? l) '())
      (else (cons (car (car l))
                  (firsts (cdr l)))))))

As (firsts '()) is '(). So, this would actually fail (in guile):

$ (firsts '((a b) () (e f)))
In procedure firsts:
In procedure car: Wrong type argument in position 1 (expecting pair): ()

I think a correct implementation would be:

(define (firsts l)
  (cond
   ((null? l) '())
   (else (cond
          ((null? (car l)) (firsts (cdr l)))
          (else
           (cons (car (car l))
                 (firsts (cdr l))))))))

in which we take care of the non-empty list before getting the first typical element (car (car l)). This would result in:

$ (firsts '((a b) () (e f)))
(a e)

Syndicated 2013-04-10 06:18:09 from aleix's blog

67 older entries...

 

aleix certified others as follows:

  • aleix certified jao as Journeyer
  • aleix certified fxn as Journeyer
  • aleix certified rms as Master
  • aleix certified alan as Master
  • aleix certified raph as Master
  • aleix certified jono as Journeyer
  • aleix certified jarod as Apprentice
  • aleix certified linus as Master
  • aleix certified dmerrill as Master
  • aleix certified cerquide as Apprentice
  • aleix certified mwk as Journeyer
  • aleix certified rasmus as Master
  • aleix certified jrf as Master
  • aleix certified TheCorruptor as Journeyer
  • aleix certified robocoder as Apprentice

Others have certified aleix as follows:

  • fxn certified aleix as Apprentice
  • jao certified aleix as Apprentice
  • jono certified aleix as Apprentice
  • jarod certified aleix as Journeyer
  • dmerrill certified aleix as Apprentice
  • cerquide certified aleix as Journeyer
  • mwk certified aleix as Apprentice
  • jrf certified aleix as Journeyer
  • TheCorruptor certified aleix as Journeyer
  • araumntl06 certified aleix as Journeyer
  • chakie certified aleix as Journeyer

[ Certification disabled because you're not logged in. ]

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page