[Haskell-cafe] Re: New slogan for haskell.org

Laurent Deniau laurent.deniau at cern.ch
Thu Nov 29 05:19:30 EST 2007


apfelmus wrote:
> Laurent Deniau wrote:
>> apfelmus wrote:
>>
>> Back then, I was given the task to calculate some sequence
>> of numbers which I did in one page of C code.
>>
>> import Data.Set
>>
>> xs = let f x m = x: let y = x `div` 2
>>                     in f (if member y m then 3*x else y) (insert x m)
>>      in f 1 (singleton 0)
>>
>>> As said, it's two lines if the terminal is too small :)
>>
>> I can't see how it could be one page of C unless the page is 10 lines 
>> long ;-) The following code is the direct translation of your Haskell 
>> code (except that it prints the result instead of building a list).
>>
>> a+, ld.
>>
>> #include <stdio.h>
>> #include <intset.h>
>>
>> void f(int x, intset s) {
>>   printf("%d, ", x);
>>   f (intset_elem(s, x/2) ? 3*x : x/2, intset_put(s, x));
>> }
>>
>> int main(void) {
>>   f (1, intset_put(intset_new(), 0));
>> }
> 
> Well, I only remember that it took _me_ a page of C code :D Basically 
> due to a hand-coded intset and user interaction (no REPL for C, after all).

I think this is the point. Providing an intset is a work for libraries 
and you get it once for ever, so almost any language could write this 
code in a short manner (inset itself could be done in 20-40 lines of C). 
The big difference is in the next step (not shown in your example), that 
is the flexibility in reusing the sequence.

a+, ld.



More information about the Haskell-Cafe mailing list