Questions tagged [let]

In Lisp-like and functional languages, introduces a list of local variables, each (possibly optionally) with its initial value.

In Lisp-like and functional languages, introduces a list of local variables, each (possibly optionally) with its initial value. It is illegal to refer to any of the new variables while calculating their initial values ( lets you do that), though in Haskell it is legal (its let is actually letrec).

LET is a special form in Common Lisp, Scheme, Clojure and other Lisp dialects which introduces a list of local variables as pairs of name-value bindings, for use within its body. For instance, in this expression:

(let ((variable1 (+ 1 1)))
  variable1)

variable1 gets bound to the 2 value, and the whole expression returns 2 as a result.

728 questions
40
votes
6 answers

let vs def in clojure

I want to make a local instance of a Java Scanner class in a clojure program. Why does this not work: ; gives me: count not supported on this type: Symbol (let s (new Scanner "a b c")) but it will let me create a global instance like this: (def…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
40
votes
3 answers

What does let 5 = 10 do? Is it not an assignment operation?

If I say let 5 = 10, why does 5 + 1 return 6 instead of 11?
user2763173
39
votes
10 answers

Is there a Python equivalent of the Haskell 'let'

Is there a Python equivalent of the Haskell 'let' expression that would allow me to write something like: list2 = [let (name,size)=lookup(productId) in (barcode(productId),metric(size)) for productId in list] If not, what would be the…
Perseids
  • 12,584
  • 5
  • 40
  • 64
37
votes
4 answers

Redefining a let'd variable in Clojure loop

OK. I've been tinkering with Clojure and I continually run into the same problem. Let's take this little fragment of code: (let [x 128] (while (> x 1) (do (println x) (def x (/ x 2))))) Now I expect this to print out a sequence…
MBCook
  • 14,424
  • 7
  • 37
  • 41
32
votes
5 answers

Swift Error: Ambiguous reference to member 'subscript'

I'm new to coding and picked up some open source project to get the idea. I'm getting the error: Ambiguous reference to member 'subscript' in the code below: let pictures = ( selectedRestaurant["Pictures"] as! NSArray ) // Error let picture = (…
jonasdickel
  • 321
  • 1
  • 3
  • 4
31
votes
6 answers

What's the point of lambda in scheme?

I am learning scheme. I know how to use both lambda and let expressions. However I'm struggling to figure out what the point is of using lambda. Can't you do everything with let that you can with lambda? It would be especially helpful to see an…
Cam
  • 14,930
  • 16
  • 77
  • 128
30
votes
4 answers

GHCi "let" -- what does it do?

I'd appreciate if someone could point to docs on what "let" does in GHCi, or failing that, explain it convincingly. So far as I can tell, "let" (without "in") is not part of the Haskell language per se, and on the other hand, it doesn't appear to be…
gwideman
  • 2,705
  • 1
  • 24
  • 43
29
votes
2 answers

Confused by the difference between let and let* in Scheme

Can anyone explain the difference simply? I don't think I understand the concept from the textbooks/sites I have consulted.
user2095626
  • 315
  • 1
  • 3
  • 7
27
votes
3 answers

Swift - Lazy Var vs. Let when creating views programmatically (saving memory)

I'm a beginner and I sort of understand Lazy Var vs. Let. I've noticed that it saves a ton of memory usage when using Lazy Var especially with ImageViews. But the tutorials and guides I've seen so far don't use Lazy Var very often, so I'm feeling…
Jazure
  • 461
  • 2
  • 6
  • 15
25
votes
3 answers

Why is using `let` inside a `for` loop so slow on Chrome?

MAJOR UPDATE. Thought as yet not on the Chrome major release the new Ignition+Turbofan engines for Chrome Canary 59 has solved the problem. Test show identical times for let and var declared loop variables. Original (now moot) question. When using…
Blindman67
  • 51,134
  • 11
  • 73
  • 136
23
votes
6 answers

Clojure's 'let' equivalent in Scala

Often I face following situation: suppose I have these three functions def firstFn: Int = ... def secondFn(b: Int): Long = ... def thirdFn(x: Int, y: Long, z: Long): Long = ... and I also have calculate function. My first approach can look like…
tenshi
  • 26,268
  • 8
  • 76
  • 90
23
votes
2 answers

Using a `let` binding to increase a values lifetime

I wrote the following code to read an array of integers from stdin: use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { let xs: Vec = line.unwrap() .trim() …
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
22
votes
2 answers

Why does const work in some for-loops in JavaScript?

I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I…
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
21
votes
4 answers

Why no destructing in def form?

In a let form (Clojure here) I can doing something like (let [[u s v] (svd A)] (do-something-with u v)) where svd returns a list of length three. This is a very natural sort of thing to do, so why isn't that we don't we have (def [u s v] (svd…
Gabriel Mitchell
  • 979
  • 5
  • 17
21
votes
1 answer

Variable scope + eval in Clojure

In Clojure, (def x 3) (eval '(prn x)) prints 3, whereas (let [y 3] (eval '(prn y))) and (binding [z 3] (eval '(prn z))) generate an 'Unable to resolve var' exception. According to http://clojure.org/evaluation, eval, load-string, etc generate…
gilesc
  • 1,969
  • 1
  • 14
  • 16
1
2
3
48 49