[erlang-questions] Simple Erlang Recommendation (last returned value)

Berlin Brown berlin.brown@REDACTED
Mon Jul 28 17:32:12 CEST 2008



On Jul 28, 6:10 am, "Jesper Louis Andersen"
<jesper.louis.ander...@REDACTED> wrote:
> On Mon, Jul 28, 2008 at 2:47 AM, Richard A. O'Keefe <o...@REDACTED> wrote:
>
> > Also, functional language with explicit 'let' allow
> >        let val x = 1 in
> >        let val x = x + 1 in
> >        x+1 end end
> > (SML) or
>
> This is infected with Ocaml syntax style :) The SML way would be:
>
> let
>   val x = 1
>   val x = x + 1
> in
>   x + 1
> end
>
> But at least one SML compiler has a warning with this style of coding
> since you are shadowing the 'x' value and it quickly becomes rather
> hard to follow what def-point is meant for each use (To the
> non-SML'ers, let .. in .. end works somewhat like Schemes LET*). With
> SML beginners, you often see "imperative" style code where the
> let-blocks evaluation order is used to force imperative code while a
> more functional solution is apparent to the experienced.
>
> What the let-binding tends to be used for is to declare a number of
> local functions (Which may be recursive) and then gradually combine
> these into more advanced functions, of which the last is called in the
> body of the binding.
> _______________________________________________
> erlang-questions mailing list
> erlang-questi...@REDACTED://www.erlang.org/mailman/listinfo/erlang-questions

Thanks for all of the responses.

I think this is the best solution for what I am doing.

lists:foldl(
   fun(X, Last_value) -> queue:add(X, Last_value) end,
   queue:new(),
   [joe, mike]).

-------------------

There are a couple of things that I wanted to make clear.  I did NOT
want to change the mutability aspect of Erlang, as many have
suggested, it goes to the core of Erlang and functional programming.
And ideally what I suggested was more from "stack/functional" based
languages like Factor or Forth not so much imperative languages like
Java/C++.

I as assuming there was a call stack or some internal mechanism in
Erlang that keeps track of the result of the last expression and maybe
there is a way to "dup"licate that expression or something similar.

In a stack oriented language, you push expression onto the stack and
perform operations against values that are on the stack.   There are
built in functions to duplicate, copy values on the stack. The
following code is equivalent in "Factor" a forth based language.
Square the values on the stack.

3 3 * .

Or

3 dup * .

Dup = duplicates the value on the stack.

================

Essentially, I was wishing there was a similar erlang function to do
something which might save keystrokes.

Q1 = queue(dog, NewQ),
Q2 = queue(dog, Q1),
Q3 = queue(dog, Q2)
...

Q1 = queue(dog, NewQ),
queue(dog, erlang:dup()),
queue(dog, erlang:dup())
...
where dup = return result from last expression.

...
================

And I don't totally understand how erlang expressions are evaluated so
it may not make sense at all in Erlang.  For example, with Erlang's
pattern matching, internally expressions may not be evaluated in such
a routine by routine fashion as they may be in imperative languages.

Thanks and this look like a good discussion.



More information about the erlang-questions mailing list