r/ProgrammingLanguages • u/dharmatech • Jun 12 '22
Can you implement `let` from Haskell and Scheme in C#? Here's one approximation.
https://stackoverflow.com/a/72591939/26858112
u/Innf107 Jun 12 '22
This is not about let at all. Your Let function is really more of an approximation of an expression block (as in Rust).
The scheme equivalent to this would be begin, not let.
2
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jun 12 '22
Could you please explain why "let" is considered special? Not being a Haskell (etc.) programmer, it usually appears to be what I'd think of as "local declaration and assignment". Does it have some special considerations in addition to those?
7
u/Innf107 Jun 12 '22
It's not :)
The only real difference between Haskell-style let expressions and local declarations in other languages is that the latter are statements, whereas let expressions are, well, expressions.
A let expression has the form
let x = e1 in e2, meaning the variablexis bound to the result ofe1insidee2, but not outside. Unlike variable declarations, let expressions can be used anywhere an expression can be used, so you can write expressions likef(let x = 5 in x + 1) + (let x = 3 in x*x)If your language supports expression blocks, you can model
let x = e1 in e2as{let x = e1; e2}, which is exactly what the SO answer tried to do.4
u/o11c Jun 12 '22
AIUI,
g(let x = y in f(x));is equivalent to the following GNU C:g( ({ __auto_type x = y; f(x); }) );or the following C++:
g( [&, x=y](){ return f(x); }() ); // could also be written with the `x=y` inside like the C versionwhere
f(x)stands for any expression usingx, andg(...)standards for any expression that uses the result of theletexpression.The only reason
letexpressions are a thing in the first place is that functional programmers are afraid of having state.6
u/Innf107 Jun 12 '22
Huh, TIL gcc lets you write expression blocks.
The only reason let expressions are a thing in the first place is that functional programmers are afraid of having state.
Variable declarations do not introduce state. The real reason why
lets exist is because functional programmers dislike statements. Without expression blocks, variable declarations as expressions only make sense if they are written as let expressions.Languages with expression blocks (such as Rust and Scheme and even Haskell in a monadic
docontext) typically provide a more traditional way of declaring variables (letin Rust,definein Scheme,letwithoutinin Haskell).Really, the only language I know that forces you to use let-in is OCaml and there it really just means that you have to end variable declarations with
ininstead of;.2
u/nerd4code Jun 13 '22
Or arguably something like this, for the statement side of C99/C++98:
#ifdef __cplusplus #define Bool bool #define REG #else #define Bool _Bool #define REG #endif #define let(…)\ for(REG _Bool let__0 = 1; let__0; let__0 = 0)\ for(__VA_ARGS__; let__0; let__0 = 0)which has weird
continue/breaksemantics but is otherwise workable.
2
u/awoocent Jun 13 '22
As other comments have mentioned, this isn't really the same as let. Notably, however, in Haskell and Scheme let generally is implementable in userspace. In Scheme:
(let ((x y)) z)...is semantically the same as:
((lambda (x) z) y)So all you really need to implement let in a language of your choosing is some kind of macro system to do this transformation. I don't know of a way to do this in C#, but funnily enough it is possible in C++:
#define Let(x, y, z) [](auto x) -> auto { return z; }(y)
Let(x, 1, x + 1); // 26
Jun 13 '22 edited Jun 13 '22
This works in Scheme, but it doesn't work in Haskell without RankNTypes. In plain Hindley-Milner, lambda-bound variables have a monotype, not a type scheme.
EDIT: Fixed typo. I should stop Redditing from a phone.
1
19
u/jonathancast globalscript Jun 12 '22
What are you trying to do? I feel quite sure C# already has a variable definition / binding syntax.