Arc Forumnew | comments | leaders | submitlogin
Local Scopes?
3 points by Bogart 6031 days ago | 7 comments
Is there a way to have variables only local to one function without using a let expression? In scheme, for instance, all variables defined in a lambda are not accessible from outside of it. It seems only function arguments act this way in arc.


4 points by Meeyu 6031 days ago | link

Yes and no. (= x 1) defines or changes the global value x (well, except if x was bound to a local variable, either in a let or as a formal parameter in a function). However, behind the scene, this :

  (def myfn (n)
    (let m (+ n 2)
      (+ m 2)))
is transformed as :

  (def myfn (n)
    ((fn (m) (+ m 2)) (+ n 2))
So, yes, by using "pure" lambda-calculus, by applying an anonymous function to the values defined by let, you can go without it.

That's the general answer. But why do you want to avoid let ?

-----

-1 points by applepie 6029 days ago | link

Because "=" is confusing and encourages the use of global variables.

IMHO, Arc got scope wrong, mixing definition with assignment. Didn't we learn from Python? (i.e. it's sad to see the same mistakes once and again).

-----

4 points by almkglor 6029 days ago | link

> Didn't we learn from Python? (i.e. it's sad to see the same mistakes once and again).

Eh? What mistake? THe only mistake I see in Python is the difficult anonymous function syntax and lack of Lisp macros.

-----

1 point by applepie 6028 days ago | link

  def make_counter():
    x = 0
    def inc():
      x = x + 1
      return x
    return inc

  c1 = make_counter()
  c1() # returns 1
That's Python.

It might seem nice at first glance :)

But... oops! it doesn't work.

It doesn't work because the "x" in "x = x + 1" is _not_ the same "x" as in "x = 0".

And that happens because definition and assignment are badly mixed up.

-----

4 points by almkglor 6027 days ago | link

...and? The equivalent (and correctly running!) Arc code is:

  (def make-counter ()
    (let x 0
      (fn ()
        (= x (+ x 1)))))
So... why do you want to avoid 'let ?

-----

3 points by skenney26 6029 days ago | link

Recently I've been thinking alot about scope and would be interested in hearing you expound on what Python and Arc got wrong.

-----

1 point by bOR_ 6029 days ago | link

Isnt' the massive use of global variables not something that is only for newbies?, and from which you learn what is bad about it by experience?

in which case it is no longer a problem for most of us ;)

-----