chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Named let*


From: Michele La Monaca
Subject: Re: [Chicken-users] Named let*
Date: Mon, 27 May 2013 23:14:12 +0200

>> R5RS doesn't  specify this kind of syntax (nor Chicken supports it):
>>
>> (let* loop ((a <init>) (b a))
>>   <body>)
>>
>> To me it seems a missing piece of syntax. Am I wrong?
>
> I've missed it occasionally as well, but I'm not sure it's *that* useful.

Of course that's something we all can live without, but let me expose
a concrete example just for reference.

Let's say I want to traverse a string (or a vector). A nice way to
write that is:

(let loop ((i 0) (ch (string-ref buf 0)))
  (do-something)
  (if (some-condition-is-true)
    (loop (+ i 1)
          (string-ref buf (+ i 1)))))

So far so good... well, almost. If you need to change the starting
position (0) you have to remeber to change it in two locations. But
this is still a minor annoyance.

Now, let's suppose the starting position is not a literal value but
the result of an evaluation:

(let loop ((i (some-function)) (ch (string-ref buf (some-function))))
  (do-something)
  (if (some-condition-is-true)
    (loop (+ i 1)
          (string-ref buf (+ i 1)))))

This is unsatisfactory and might not be even viable if some-function
can't be evaluated twice (e.g. some-function = random).

So writing down the options, we have:

(let* loop ((i (random N)) (ch (string-ref buf i)))
  (do-something)
  (if (some-condition-is-true)
    (loop (+ i 1)
          (string-ref buf (+ i 1)))))

vs.

(let ((start (random N)))
  (let loop ((i start) (ch (string-ref buf start)))
    (do-something)
    (if (some-condition-is-true)
      (loop (+ i 1)
            (string-ref buf (+ i 1))))))

vs.

(let ((ch '()))
  (let loop ((i (random N)))
    (set! ch (string-ref buf i))
    (do-something)
    (if (some-condition-is-true)
      (loop (+ i 1)))))


Now, I guess each of us has its own preference but I don't see the
point to rule out the first option. As I already said defining a let*
syntax but not a named version of it give me a sense of
incompleteness.  :-(

Ciao,
Michele



reply via email to

[Prev in Thread] Current Thread [Next in Thread]