Destructuring-bind


#1

I saw David’s clever use of destructuring-bind in quicksort and thought, it looks like a pretty simple special form to implement.

The core would be this:

object* destructure (object* structure, object* data, object** env) {
  if (structure == nil) return;
  if (symbolp(structure)) push(cons(structure, data), *env);
  else if (consp(structure)) {
    if (!consp(data)) error(canttakecar, data);
    destructure(car(structure), car(data), env);
    destructure(cdr(structure), cdr(data), env);
  }
  else error(invalidarg, structure);
}

I think that all that would be needed would be to wrap it in a special form function that can be called by uLisp. I would also want to add support for &optional in the structure form, and this could also be added to closure() so that lambdas can be destructed too.

If I implement this and get it working, I will add it to my page of uLisp patches.


#2

have you looked at dbind? It looks like an easy impl requiring macros.


#3

Where is this “dbind” implementation? I can’t find it.

Also, using macros would not avoid having to edit the C++ code because the user would have to add my macro patches first. David has expressed multiple times that he’s not going to be adding the macros to core uLisp anytime soon.


#4

desctructure looks like a better option


#5

The entire reason I was looking into it was for a destructuring-let, which of course someone has thought of:

…but they need better examples, like Clojure’s let:

https://clojure.org/guides/destructuring


#6

I just implemented destructuring-bind in my fork of uLisp. The changes are all in one commit so it should be easy to add to your own extensions file. Luckily, this isn’t one of the patches that requires access to the main uLisp source.