I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form.
This is my best attempt in a simple function to determine difference between map-nodes (works, just not sure why it works):
(define distance-between
(lambda xs
(let ([input-list (list* xs null)])
(letrec ([f (lambda (xs acc)
(if (null? (cdr xs))
acc
(f (cdr xs) (+
(abs (- (map-node-x (car xs)) (map-node-x (cadr xs))))
(abs (- (map-node-y (car xs)) (map-node-y (cadr xs))))
acc))))])
(f (car input-list) 0)))))
As you can see, it's an ugly solution and involves some of what seems like magic to me - why is the improper list coerced into list form when I include it in a list*? (note: this sentence is misleading, this does not occur).
I'd rather have a pretty solution and no magic. Can anyone help?
For example a typical input would be:
(distance-between (map-node 1 2) (map-node 2 3) (map-node 3 4))
with the expected result:
4
(a distance of 2 between map-node (a) and m-n (b), plus a distance of 2 between map-node (b) and map-node (c)).
Alternatively one might simply input:
(distance-between (map-node 1 2) (map-node 2 2))
and get an answer of:
1
If I attempted this on the raw input, without my (let ([input-list...])...) statement, it would cause an error as (? not actually sure why given response to this question).
The function works as expected.
Your list is not improper. When your argument is not a pair, like
(lambda xs body ...)
or(define (fun . xs) body ...)
all your arguments gets slurped into a list. Eg..(fun 1 2 3)
would make xs'(1 2 3)
. Doing(list* '(1 2 3) '())
makes'((1 2 3)
which you undo right away by calling your loop withcar
which makes it'(1 2 3)
again.Other than that your procedure works as intended. You might clean up your procedure a little, but since there is no list comprehensions that glides over a list folding over the two next elements it won't become much smaller. Below is basically the same code, but abstracting out the procedure that does the work (which if existed a foldl-pair you could have used) and with a
named let
as a iterator loop (which is syntactic sugar for a letrec+call).EDIT: About syntax sugar,
named let
andletrec
.is syntactic sugar for a anonymous procedure call
A
named let
is just giving that procedure a name, though as if byletrec
, making a recursive binding. you call it with the name you give and the arguments will be what you supply instead of the initial value in the let. I'm used to them and prefer them today. It might take some time to get used to though.Most of the code we write is syntactic sugar for some lower level stuff. The macros are nested so that your
letrec
form could get reduced down lambdas eventually. The whole procedure without syntactic sugar would look like this:There's nothing improper about the list received as a variadic argument list (meaning: variable number of arguments). For example:
In the above example, the
xs
parameter is a normal, plain, vanilla list, there's nothing improper about it. You can iterate over it as you would over any other list. There's no need tocar
it, it's already a list! Also, notice that the same function can be written like this:Just for reference: an improper list is one that does not end with the null list. For example:
'(1 2 3 . 4)
. Again, that's not how a variadic argument list looks.I also don't understand how your variadic argument list could be improper.
But to answer your original question (how to iterate over a possibly improper list, somewhat more elegantly), here is one way using
match
:However needing to do this, at all, is probably an indication you want to fix or change something else.