Let In Expressions

Hey guys,

is there an equivalent of ‘let in’ expressions which you can find in other functional languages (nothing as powerful as ‘with do’ though) in Elixir?

Sort of obtuse, but I like Elixir’s single expression function notation, e.g.

def add(a, b), do: a+b

over using imperative statements.

And ‘let in’ would allow me to simplify these expressions…

Not a huge deal, just wondering.

1 Like

Not that I’ve ran across, but it would be fairly simple to build a let macro though, I could imagine this syntax for it:

let add a b = a+b

The in is a keyword in elixir so it would not work at the end of the context there, but ‘end-of-line’ would work well, just could not do multiple lets on a single line, plus the function call syntax would not be quite functional (ML’ish), I.E. you could not do this with macros:

let blah a = vwoop 42 a

But instead you’d have to do this weirdness:

let blah a = vwoop 42, a

And nowadays that will puke warnings (rightfully so considering uniformity with the rest of the language), so instead you would have to do this algol-looking ugliness:

let blah a = vwoop(42, a)

You’d also have to use a different call for in-function definitions as well, maybe letl or let local or so:

def blah(a), do
  letl min l r = if l < r, do: l, else: r
  min.(42, a)
end

Although ‘maybe’ you could get away with the macro detecting the details of the environment it is called in and adjusting accordingly to return either a def declaration or a variable and anonymous function declaration? Or force any local lets to be within an outer let?

This does however give me an idea for an entire ML’ish syntax macro library for elixir, though unsure if it should actually be used, probably just be an interesting fun project. ^.^

/me has tried very hard not to go off on Elixir’s syntax again, not a fan of the syntax and lack of types, but loves the macro system… ^.^

3 Likes

I did an little experiment for having this :D, take a look at an example def from the tests consider it toy quality tho. But If you are willing to send some PR or open issues to improve it I’d me more than thankful.

6 Likes

Hah, that is cute, I like it. ^.^

      having {b, c}
      | c = b * b
      | b = a * a
      | a = 2

That would indeed be an interesting addition to an ML-like Elixir syntax library. ^.^

3 Likes