Toby DiPasquale on 10 Jun 2007 17:36:24 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Question about erlang presentation


On Sun, Jun 10, 2007 at 10:37:30AM -0400, Eric wrote:
> Toby:
> 
> I remember at least 2 of my questions regarding your Erlang
> presentation:
> 
> You said that = was in reality a binding instead of an assignment.
> I'm not sure I understand.  Are you saying that in erlang this
> statement:
> 
> Var = 2 + 2
> 
> puts something besides the number 4 into Var?

No, at the end of that expression evaluation, Var will surely point to the
value 4. However, Erlang's = is a binding operator, not an assignment
operator. Assignments insert a value into a memory location. Bindings
associated a name with a value. In Erlang, the = is "overloaded" (so to
speak) by also performing Prolog-style pattern matching. So, this
statement:

{car, Type, Power} = Car.

indicates multiple bindings.

I called it a "binding" operator because Erlang doesn't really have
"assignment" like imperative languages do. When you use = in Erlang,
you're not telling the compiler what to do with some particular piece of
memory, you're just telling it that "from now on, when I use X, I mean 
Y".

This feature exists in some other languages, too, in more limited form.
Lisp and Scheme have something called a "destructuring bind" which is like
the example above. This is less powerful than Erlang's pattern matching,
since it only "explodes" a list and nothing else. Prolog has full pattern
matching, however, as Erlang's was inspired by Prolog.

> Also, you referred to a specific type of recursion, tail or tail-end
> recursion,that I had not heard of.  I found it on Wikipedia so I
> _believe_ I now understand it (heh!)

Tail recursion is a way to formulate a recursive function such that the
function does not need to return to its parent caller to calculate the
correct value. Its an optimization technique: using tail recursion allows
the compiler to reuse the existing stack frame for the function call
rather than having to create another one (as with regular recursive
functions). This saves a lot of space and allows for indefinite recursion 
as your per-function space overhead is amortized to near zero on very
deep recursions.

In order to really be able to use tail recursion, though, your compiler or
language platform has to support it. Erlang supports it. C compilers do
not, in general. I think Java does not support it, either.

Anything that can be expressed recursively, however, can also be expressed
iteratively. Thus, if you're using a language/platform that does not
support tail recursion, you can always drop back to the iterative form of
the algorithm(s) when you might be recursing a lot of using multiple
recursive functions concurrently.

> Thanks again for a very interesting presentation.

Doitashimashite (^_^)  I was glad that you guys liked it :)

-- 
Toby DiPasquale
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug