Proposed addition to uLisp: bind


#1

For the next major release of uLisp, Release 4.9, I’m proposing to add a simplified version of the Common Lisp special form destructuring-bind, which lets you bind one or more variables to the values in a list returned by evaluating a function. I’m proposing to call the simpler version bind.

For example, suppose you have a function (lis3dh-xyz) that returns the x, y, and z values from a 3-axis accelerometer as a list of the three values:

> (lis3dh-g3d)
(0.0273438 0.0507813 1.02734)

Currently you might do something like:

(let* ((data (lis3dh-g3d))
       (x (first data))
       (y (second data))
       (z (third data)))
  (format t "x=~a, y=~a, z=~a~%" x y z))

With bind you will be able to replace this with:

(bind (x y z) (lis3dh-g3d)
  (format t "x=~a, y=~a, z=~a~%" x y z))

Does this seem a useful addition?

I’m proposing to call it bind to make it clear that it is a simplified version of destructuring-bind, and because the latter is not one of my favourite Common Lisp names. I’m open to alternative suggestions, such as do-bind.


#2

I think it’s useful. I need to bind single values from lists to a couple of variables all the time. This special form would probably provide a shortcut very often and shrink the code considerably. As for the name “bind”: Makes sense to me.


#3

I like this. What features of the Common Lisp destructuring-bind would you be omitting?


#4

My plan is that bind will be limited to the same format of list that you can include in a defun definition. So:

(bind (a b c) (myfunction) ...

(bind (a b &optional c) (myfunction) ...

(bind (a b &optional (c default)) (myfunction) ...

(bind (a b &rest others) (myfunction) ...

but not dotted pairs, nested lists, etc.


#5

Oh thats nice, I thought you were just gonna support the first case.


#6

FWIW, having dotted pairs, nested lists, is not too difficult, I implemented it in my fork:

However, it doesn’t include &optional and &rest support.


#8

I think &optional and &rest are more useful; see:

Language reference - bind