Re: Dylan equivalent to Lisp "let*"

56 views
Skip to first unread message
Message has been deleted

Peter Housel

unread,
Feb 26, 2019, 4:16:55 PM2/26/19
to dylan-lang
On Tuesday, February 26, 2019 at 12:26:56 PM UTC-8, Stewart M wrote:
Is there an equivalent to Lisp's "let*" in Dylan?  In my coding style, I like to name intermediate parts of a calculation for documentation or multiple uses of a subexpression.

So in Common Lisp I have:

(let* ((x 2)
       (y (/ x 2))
       (z (/ x 4)))
  z)

in Dylan the best I could come up with is:

let x = 2;
let y = x / 2;
let z = x / 4;
z

I'm aware of the Dylan "values" syntax, but this isn't really what I need.  The multiple "let"s are OK, but I was wondering if there was anything more elegant.

Thanks for any info.

The way you've written it is the idiomatic way. In Lisp let* is necessary because otherwise with let the nested scopes would result in highly nested s-expressions. In Dylan the scope of let is implicitly everything that follows it in the enclosing block.

An random example of my code that names a lot of intermediate values:


-Peter-

Carl Gay

unread,
Feb 26, 2019, 4:30:19 PM2/26/19
to dylan-lang
I would just add that if you prefer to give the bindings the smallest possible scope, then you can introduce a new scope: 

begin
  let x = 2;
  let y = x / 2;
  let z = x / 4;
  ...do stuff with z...
  z
end

But that's not really idiomatic in Dylan.  If you keep functions relatively small, it doesn't seem to be an issue.

-Carl


--
You received this message because you are subscribed to the Google Groups "dylan-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dylan-lang+...@googlegroups.com.
To post to this group, send email to dylan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dylan-lang/a3f83855-54e5-437c-b8e0-b6f0a4981d05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Carl Gay

unread,
Feb 26, 2019, 9:03:06 PM2/26/19
to dylan-lang
I'm not sure if this is what you meant, but here's a macro I used for a while. In the end I decided the built-in let was enough.

// ----------------------------------------------------------------------
// bind introduces new bindings a la "let", but also introduces a new
// block to limit the variables' scope.
//
// bind (x = 1, y :: <string> = "y")
//   x + y
// end
//
define macro bind
    { bind (?bindings) ?:body end }
 => { begin
        ?bindings
        ;
        ?body
      end }
bindings:
    { } => { }
    { ?binding, ... } => { ?binding; ... }
binding:
    { ?var:variable = ?val:expression }
 => { let ?var = ?val }
end;


On Tuesday, February 26, 2019 at 8:01:11 PM UTC-5, Stewart M wrote:
I know in Lisp, let* is commonly transformed by a macro into nested plain lets.  Makes me wonder if I could write such a macro in Dylan.  My Common Lisp models do have large numbers of let bindings, e.g.:


I could also just decompose my models into more functions with Dylan.  In any case, thank you (and Carl) for your replies.

Bruce Hoult

unread,
Feb 26, 2019, 9:45:54 PM2/26/19
to Stewart M, dylan-lang
A Dylan begin/end block with a series of "let" is exactly semantically equivalent to a Common Lisp or Scheme let*, except more flexible because you can intersperse side-effect function calls with the declarations, where in Common Lisp or Scheme you'd have to resort to a progn or begin:

begin
  let x = 2;
  let y = x / 2;
  debugPrint("y = ", y);
  let z = x / 4;
  debugPrint("z = ", z);
  z;
end

(let* ((x 2)
       (y (/ x 2))
       (z (progn (debugPrint "y = " y) (/ x 4))))
   (debugPrint "z = " z)
   z)

Ugly.

On Tue, Feb 26, 2019 at 12:26 PM 'Stewart M' via dylan-lang <dylan...@googlegroups.com> wrote:
Is there an equivalent to Lisp's "let*" in Dylan?  In my coding style, I like to name intermediate parts of a calculation for documentation or multiple uses of a subexpression.

So in Common Lisp I have:

(let* ((x 2)
       (y (/ x 2))
       (z (/ x 4)))
  z)

in Dylan the best I could come up with is:

let x = 2;
let y = x / 2;
let z = x / 4;
z

I'm aware of the Dylan "values" syntax, but this isn't really what I need.  The multiple "let"s are OK, but I was wondering if there was anything more elegant.

Thanks for any info.
 

--
You received this message because you are subscribed to the Google Groups "dylan-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dylan-lang+...@googlegroups.com.
To post to this group, send email to dylan...@googlegroups.com.
Message has been deleted
Message has been deleted

peter...@gmail.com

unread,
Feb 27, 2019, 4:45:07 AM2/27/19
to dylan-lang
As a matter of interest, is there a reason why we couldn't have multiple bindings in one let? Parser, presumably?
e.g.
let x = 10, y = x * x;

Pete

Carl Gay

unread,
Feb 27, 2019, 10:10:52 AM2/27/19
to peter...@gmail.com, dylan-lang
I suspect that's doable, but I would argue that it's a lot easier to read one binding per line.
And of course we also have let (x, y, z) = values(1, 2, 3);
Reply all
Reply to author
Forward
Message has been deleted
0 new messages