Variable argument lists?

March 10th, 2009

Re: multi-way if, how about something like

if := fun (cond, lazy then) `(conds, lazy thens) {
    case cond {
        false, 0, nil := case conds.len {
            0 := nil
            _ := if `(conds, thens)
        }
        _ := then
    }
}
else := true

if { x = 2,
    println("x is 2")
} { x < 2,
    println("x is less than 2")
} { else,
    println("x is more than 2")
}

Read the rest of this entry »

A language

March 10th, 2009

As if the pile-up of university assignments doesn’t give me enough to do, I’ve been thinking about writing a tiny programming language, where as few things are taken as primitive as possible, so the user can define almost any syntax they want. So far, though, what I’ve thought of amounts to little more than Lisp with the brackets in different places. Oh, and infix operators.

Note that none of this is implemented yet, I’m just throwing out any ideas I have. Even if they’re terrible.

Read the rest of this entry »

Slight updates to haskell.vim

March 8th, 2009

Since the Vim syntax file for Haskell was last updated, there have been a bunch of syntax extensions in GHC1, like rank-n types, arrow syntax, and allsorts. I decided to add a few of these.

You can get it here. You might want to back up your old one; I’m pretty sure I haven’t broken anything, but my testing wasn’t exactly thorough (mostly consisting of checking the new syntax worked, rather than whether the old syntax was broken).

Update! if anyone’s seen this yet, then please download it again. The first time, (#) would make the # part of an unboxed tuple bracket. That’s fixed now. (See: lack of testing.)

Read the rest of this entry »


  1. If you don’t use GHC perhaps these updates won’t be very useful… 

At least it was a slightly better reason than not being bothered

March 3rd, 2009

Well no endofunctor.net after all, probably. NearlyFreeSpeech.net charge a cent per day for a MySQL database, which of course is nothing in the grand scheme of things, but since the rest of the stuff1 would probably cost less than that per month, it kind of put me off a bit.

I might start posting here again, but I don’t think I need to say how that will work out.

Update! I’m writing an HTTP session library in Haskell, for future use somewhere. I wonder where..?


  1. Other than the domain name, obviously. 

As if anyone reads this anyway??

January 28th, 2009

So I’m moving this blog over to endofunctor.net (or whatever I’m calling it), most likely. When it’s done, that is, which could be a long time since I’m writing it all myself instead of plonking a stock WordPress installation on it (duplicated effort is fun towne, after all).

I was going to write it all in Haskell. But that would have to all be over CGI and no HAppS (not that I’ve tried either, but…). So web.py it is instead, which so far seems pretty nice I suppose.

Of course don’t be too surprised if this is the last you hear of all this.


In other news, I put Arch linux on my laptop to replace Kubuntu, and I like it a whole lot. It gives the (maybe false) impression that I actually have some idea of what’s going on, which is nice. Plus things come up in the package manager on the same day as they’re released elsewhere, often. (KDE 4.2 is good too.)

(this was written on an iPod while my computer’s doing other things, so sorry for amy typos.)

I just noticed something

December 6th, 2008

The applicative functor instance for (r ->) is:

instance Applicative ((->) r) where
    pure = const
    f <*> g = \x -> f x (g x)

Look familiar? Of course, I is missing, but it’s just pure <*> undefined1 (or id but that’s cheating) anyway.

I dunno what implications this has, other than that you could in theory write a whole program with just pure and (<*>) if you really wanted. But then it is 5.50am so there’s probably something obvious I’m just not seeing.


  1. I wanted to put pure <*> pure but when I checked this actually worked GHCi shouted at me for the second pure being ambiguous. So bleh. 

OCaml’s type system is awesome

September 12th, 2008

…If not just a little bit complex. But for objects, it has some sort of crazy static duck typing thing going on.

let f obj = obj#set_x 4;;
(* f: <set_x: int -> 'a; ..> -> 'a *)

The bit I mean is the argument to f, i.e. the <set_x: int -> 'a; ..> part, which is, broadly speaking, “any object with a set_x function accepting an int“. It’s like C++ templates without all the excessive verbiage (or, alternatively, like a dynamic language).

let obj = object
    val mutable x: int = 0;
    method set_x x': unit = x <- x'
end;;
(* obj: <set_x: int -> unit> *)

Now of course we can apply f to obj and it will automagically work. Also, as you can see, we have singleton objects, which is nice I suppose?

Shame that the rest of the language isn’t so good. Well, I mean, compared to something like C of course it is, but it just feels like such a pain in the ass sometimes. (Why aren’t all my lets rec, and all that.)

In other news: the website’s up for paying again. Hopefully I’ll remember before it’s taken down this time.

Sieve of Eratosthenes as a Regexp

September 4th, 2008

No, really.

sub is_prime { ('1' x shift) !~ /^1?$|^(11+?)\1+$/) }

Applicative functors and actions

August 29th, 2008

This is obvious and all, but it suddenly clicked why you can’t choose different paths inside of Applicative. What I mean is that things of this form aren’t possible:

something = do p' <- getABool
               if p then putStrLn "it was true"
                    else putStrLn "it wasn't"

However, I couldn’t think why. Well, the reason is that the methods of Applicative are pure and (<*>) (obviously), which are like return and ap (also obviously). But! Note that with (<*>) (and ap) you can only work inside the functor:

mf `ap` mx ≡ mf >>= \f -> mx >>= \x -> f x

On other words, you would have to evaluate both x and y in something, somewhat like this:

something' p x y = do p' <- p
                      x' <- x
                      y' <- y
                      return (if p then x' else y')
 
-- or
somethingA p x y = f <$> p <*> x <*> y
    where f p x y = if p then x else y

…evaluating both x and y and (in IO) performing both their side effects.

I’m sure this explanation is terrible, but it helped me at least?

PHP is now less crappy than Java

July 23rd, 2008

And by “is less crappy than Java”, I mean “has closures”.

The syntax goes thus:

function fun() {
    $a = 1;
    $b = 2;
 
    // variables being closed around need to be
    // explicitly specified:
    $closure = function() use($a) {
        // prints 1
        echo $a;
        // prints nothing (outer $b isn't in use)
        echo $b;
        // no effect outside $closure (outer $a not used as reference)
        ++$a;
    }
 
    //prints 1
    echo $a;
 
    $closure2 = function() use(&$a) { ++$a; }
    $closure2();
    // prints 2
    echo $a;
 
    // still prints 1 because $a was copied in the declaration
    // which is a bit of a wtf, but better than create_function
    $closure();
}

Now how long until PHP 5.3 is anything like widespread? I’m guessing three years or so.