1

The following doesn't work:

Lol[x_, y_] = Module[ {w=x+1, z = w + 1}, z]

because z refers to w. Is there a way to make it so that variable definitions within Module can refer to other variable definitions?

This is similar to let constructs in other languages.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
George
  • 3,145
  • 7
  • 21

1 Answers1

5

I think you meant to use Lol[x_, y_]. At any rate, you can use With to do this, although the syntax coloring is bad:

Lol[x_, y_] := With[
    {w = x + 1},
    {z = w + 1},
    z
]

Then:

Lol[2, 3]

4

The syntax coloring should be fixed with the next version of Mathematica.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 1
    If With is the answer then it is a duplicate, if OP showed only a minimal example and Module is a key here, then it is not the answer. I know you know but readers may not know why Module was replaced with With without a word of explanation. – Kuba May 24 '19 at 09:12
  • @Kuba The original post refers to the "let constructs" found elsewhere. That pretty much implies a With-like approach should be used, i.e. Module should not be a key requirement. – Daniel Lichtblau May 24 '19 at 15:22
  • 1
    @DanielLichtblau thanks the comment. Seems like my initial comment is still valid though. – Kuba May 27 '19 at 13:56