r/ProgrammingLanguages • • Apr 17 '23

Discussion Why do some programming languages use let instead of var? What does it stand for?

To me var is more clear

1 Upvotes

43 comments sorted by

78

u/[deleted] Apr 17 '23

Uh. The word let. It basically emulates the sentence structure “Let x be 10 + 2” often seen in math. This works best in languages like Haskell or OCaml, where you also delc1re functions with it so “let foo (of) x = (be) x + 10 in” makes sense (actual code doesn’t include the stuff in parentheses)

1

u/ricky_clarkson Apr 18 '23

In Haskell you don't usually use let, just foo x = x + 10 is enough. Let is for definitions within other definitions.

57

u/[deleted] Apr 17 '23

In mathematics you often see “let x = …” - so it probably comes from there.

15

u/everything-narrative Apr 17 '23

"Let X be a uniform probability distribution on the interval [a, b] and let x ~ X."

9

u/liam923 Apr 17 '23

Just read it as a sentence. "let x = 5" => "Let x equal five." It doesn't stand for anything, it just makes sense when reading it aloud

12

u/[deleted] Apr 17 '23 edited Apr 17 '23

I have these keywords with these meanings (other languages will be different):

const      Compile-time named constant
let        Assign-once variable
var        Assign as many times as you like

(The 'assign-once' is a bit of a misnomer; the value is assigned each time that let x = expr is encountered, which can be a lot more than once when used inside a function; you just can't do a normal assignment after that.)

BTW, while let is trendy now, it is far from modern in languages. It was used in the 1960s in BASIC, where variables were mutable.

But then, every assignment needed let.

A modern language that allows you to update a variable as many times as you like by using a fresh let, sort of defeats the purpose.

3

u/[deleted] Apr 18 '23

Nim actually does somewhat the same, and I personally also think that it's a really sensible choice :)

const - compile-time constant (or some expression that can be run at compile-time), assign once.

let in Nim means that the value itself is immutable, so for value types - you can't modify them at all, and for reference types - you can't modify the reference itself, only the underlying data. There's also deep immutability that can be achieved via func + strictFuncs feature.

var - can reassign the value at will.

1

u/arjungmenon Apr 18 '23

Why not do let mut instead of var?

1

u/mgorski08 Apr 21 '23

Y verbos when few word do trick? |/s>+|~/s>

28

u/JeffIrwin Apr 17 '23

I think we can all agree that C’s choice of auto makes the least sense compared to let or var

18

u/raevnos Apr 17 '23

C++ just reused an existing obsolete keyword that kind of fit instead of adding a brand new one.

8

u/JeffIrwin Apr 17 '23

Wow, TIL auto was used way back in B

https://stackoverflow.com/a/8272497/4347028

3

u/nacaclanga Apr 19 '23

Yes in "B" variable where introduced as "auto" (living on memory that gets automatically cleaned up when their scope ends, meaning the stack.) and "static" (living on fixed memory and keep their value, even after the their scope ends).

In C the type declarator replaced the function of "auto" in virtual every case.

1

u/lassehp Apr 19 '23

The concept of storage classes probably came to B from PL/I (which was the implementation language for Multics iirc.)

12

u/[deleted] Apr 17 '23

I think you mean C++

The idea of automatic assignment isn't just a C++ thing either. Go has a := funcThatReturnsComplicatedType() and it's extremely useful.

2

u/LighterningZ Apr 17 '23

Scala does this and it's fantastic. It gives the opportunity for far neater, and still very clear, code.

8

u/__2M1 Apr 17 '23

auto is not a replacement of let or var (which doesn’t exist in C to begin with) but a keyword saying: the compiler can and should derive the type of the variable. This is the same syntax as any variable declaration in C which is the ˋtype nameˋ pattern.

4

u/svick Apr 18 '23

auto in C++ is pretty much the same thing as var in C# and Java.

2

u/Accurate_Koala_4698 Apr 17 '23

That's effectively what let does in the languages I've used it in:

λ > let myObj = ("a", 1, ((), [1,2,3]))
λ > :t myObj
myObj :: (Num b, Num a) => (String, b, ((), [a]))

Or

fn print_type<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}
fn main() {
    let my_obj = ("1", 2, ["a","b","c"]);
    print_type(&my_obj);
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.11s Running target/debug/testrust 
(&str, i32, [&str; 3])

1

u/__2M1 Apr 18 '23 edited Apr 18 '23

But C doesn’t have var or let syntax! Auto can be treated as just another Type with inference which is not always fit for declaring a variable let/var on the other hand are always used. It is not meant to be a let or var, but servers a different purpose.

What you are talking about is type inference which many languages do have but C did not until auto was introduced in C23

5

u/Accurate_Koala_4698 Apr 18 '23

Can you explain the particular purpose? It seems like you’re saying type inference could fail, but in a language like Haskell I can explicitly define types and don’t have to rely on inference. It seems functionally equivalent to me

1

u/[deleted] May 02 '23

auto is used the same as let or var when using the almost always auto paradigm

0

u/JeffIrwin Apr 17 '23

1

u/__2M1 Apr 18 '23

No since you don’t use auto for all variables, but only in specific situations when type inference is desired and applicable.

Furthermore auto only exists in C23 which very few C Programmers use.

1

u/__2M1 Apr 18 '23

The link is a C++ header file…

1

u/nacaclanga Apr 19 '23

C's choice does make sense. What doesn't is C++'s.

8

u/mamcx Apr 17 '23

It's also a trend towards better "defaults": Inmmutable, unique, not nulls, not-lie types, etc that are now in most languages designer minds.

In the past, mutable/variable/type-that-lie were the DEFAULT, for "reasons". This is why in the past makes "sense" that if your DEFAULT is creating a mutable binding that has the type-that-lie int to use:

int a = null

But now, after billions of dollar in losses, is clear that those defaults are too dangerous. So we want to encourage better practices and that requires changing terminology so people think "ok, why Rust doesn't have var and now learn why this changed. Also, this reinforces in the mind that let a = 1 means that a is immutable, and then when you read the code have peace of mind, and only worry if you see var, let mut, mut etc.

In the future, will come the day that most will ask "Why JS has mutable variables as default that is stupid!!!" and that will be a glorious day!

2

u/rsclient Apr 20 '23

I first saw it in old BASIC programs where it was used for any assignment:

LET AMOUNT = 100
LET INTEREST = 1.05
FOR I = 1 TO 10
    LET AMOUNT = AMOUNT * INTEREST
NEXT I
PRINT "FINAL AMOUNT IS", AMOUNT    

Yeah, some BASICs required this, and others didn't. (And I'm skipping the line numbers, whih were also required by some versions of BASIC)

The value to having an explicit statement for LET is to force every single statement type to start with a keyword. It makes the parser more regular.

In my own version of BASIC, I picked "assignment" as the only statement with an optional starting keyword -- since with simple parsers you can only get one keyword-less statement, and assignment is by far the most common.

5

u/SoftEngin33r Apr 17 '23

let means immutable data by default (Rust, OCaml, Nim), It is more “mathematical” in a sense.

var just means a variable that can be changed to whatever you like at any point in time, it is less “mathematical” in the sense that you need to incorporate time to really understand what your variable will contain at a particular point in your program which leads to a more “informal” representation of what you try to achieve since the time is not directly written into the code of your program and only exists implicitly.

5

u/JeffIrwin Apr 17 '23

In JavaScript, let is also mutable, but the difference is mainly in scoping

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Edit: source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

8

u/SoftEngin33r Apr 17 '23

I know, JS is the exception here.

2

u/bigbughunter Apr 18 '23

I don’t think so - that matches the use of let in scheme and lisps

2

u/nwbrown Apr 18 '23

Let doesn't stand for anything, it's a word on its own.

2

u/ndrskcr Apr 18 '23

Lettuce?

1

u/jediknight Apr 18 '23

To me x = 1 is enough. I declare x to be 1. A bunch of programming language (both functional and imperative) get this right. I don't need let or var for declarations or assignments. let is fine for scoping as in let declarations in expression.

1

u/lIIllIIlllIIllIIl Apr 18 '23

JavaScript uses let instead of var, because var already existed and had inconsistent behaviors.

let is less clear than var, but it's still a three-letter word that makes sense in the context of declaring variables, so it's really not a bad alternative.

1

u/Stunning_Ad_1685 Apr 18 '23

Pony uses “let” to declare names with a fixed value, and “var” to declare names that can vary their value. Reference capabilities can then be added to control whether or not the value can be mutated through that name.

1

u/ProfessionalCoast812 Apr 19 '23

My take is that let is used to imply a name binding, while a var is more a variable declaration/definition. In LISP for example you have a let, which is if i recall a macro for smart lambdas, to have a name binding between some sexp value and a name so that you don't have to pass two or more times the same value in multiple locations. A name binding is usually immutable in the way that you cannot reassign it's value (obv with shadowing you can but that's not the point). This is not true in some languages like Rust where let is var under disguise.

1

u/ProfessionalCoast812 Apr 19 '23

I forgot to tell that there are languages where let and var coexist, like Javascript. Here var declares a global variable, while let declares a local variable.

1

u/lassehp Apr 19 '23

Am I the only one to think it is very ironic that the imperative verb "let" is used in languages that are "not" imperative? :-)

-16

u/vanilla-bungee Apr 17 '23

That’s why no one will remember your name