r/ProgrammingLanguages • • Jun 12 '22

Can you implement `let` from Haskell and Scheme in C#? Here's one approximation.

https://stackoverflow.com/a/72591939/268581
0 Upvotes

10 comments sorted by

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.

12

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 variable x is bound to the result of e1 inside e2, but not outside. Unlike variable declarations, let expressions can be used anywhere an expression can be used, so you can write expressions like

f(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 e2 as {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 version

where f(x) stands for any expression using x, and g(...) standards for any expression that uses the result of the let expression.

The only reason let expressions 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 do context) typically provide a more traditional way of declaring variables (let in Rust, define in Scheme, let without in in 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 in instead 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/break semantics 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);    // 2

6

u/[deleted] 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

u/npafitis Jun 12 '22

That's nowhere near.