|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Can I do var-assignment in a function?
_______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: Can I do var-assignment in a function?Circular Function wrote:
> extract(Str) -> > A = string:tokens(Str," "). > lists:sort(A). > > I could of course do this: > lists:sort(string:tokens(Str," ")). > > But lets say I want a bigger function that slo includes some deeper > function calls, lets say 4-5 > calls deep. > > That's the main thing I dislike about functional programming, it is very > expressive but sometimes it is hard afterwards to follow what a function > does and it is hard hwen you have to read what is happening from right > to the left. > It is easier to reason about when I can split into several calls. > > I guess it is doable but I can't figure out the syntax. Use comma. extract(Str) -> A = string:tokens(Str," "), lists:sort(A). That's it. -- vlm _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: Can I do var-assignment in a function?You are almost there: you need to replace the period on the second
line with a comma. extract(Str) -> A = string:tokens(Str, " "), lists:sort(A). Generally speaking, sequential statements in a block are separated by commas; blocks are separated by semi-colons; and functions are terminated by a period. Watch out for guards, though - commas and semi-colons are used differently there. Cheers, Philip 2008/7/23 Circular Function <circularfunc@...>: > extract(Str) -> > A = string:tokens(Str," "). > lists:sort(A). > > I could of course do this: > lists:sort(string:tokens(Str," ")). > > But lets say I want a bigger function that slo includes some deeper function > calls, lets say 4-5 > calls deep. > > That's the main thing I dislike about functional programming, it is very > expressive but sometimes it is hard afterwards to follow what a function > does and it is hard hwen you have to read what is happening from right to > the left. > It is easier to reason about when I can split into several calls. > > I guess it is doable but I can't figure out the syntax. > > ________________________________ > Sök efter kärleken! > Hitta din tvillingsjäl på Yahoo! Dejting: http://se.meetic.yahoo.net > _______________________________________________ > erlang-questions mailing list > erlang-questions@... > http://www.erlang.org/mailman/listinfo/erlang-questions > erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: Can I do var-assignment in a function?Circular Function wrote:
> extract(Str) -> > A = string:tokens(Str," "). > lists:sort(A). I think the comma was just a typo and not the actual question. Am I right? You want var-assignment because you want to write something like: extract(Str) -> A = string:tokens(Str," "), A = lists:sort(A), A = do_something_else(A), A = and_finalize(A). But that doesn't work. You have to use a new variable each time. extract(Str) -> A1 = string:tokens(Str," "), A2 = lists:sort(A1), A3 = do_something_else(A2), A4 = and_finalize(A3). Or this hard to read version extract(Str) -> A = and_finalize(do_something_else(lists:sort(string:tokens(Str," ")))). This approach of functional languages has at least two big advantages: 1. You don't accidentally overwrite variables 2. You can see all results in the debugger. And there should be no runtime penalty. Since the processor had to allocate and de-allocate the same amount of memory in each of the 3 cases -even if you don't see this in the code. mit freundlichen Grüßen / with kind regards Silvester Rößner This message is intended for a particular addressee only and may contain business or company secrets. If you have received this email in error, please contact the sender and delete the message immediately. Any use of this email, including saving, publishing, copying, replication or forwarding of the message or the contents is not permitted. _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: Can I do var-assignment in a function?On Wed, Jul 23, 2008 at 3:29 PM, Roessner, Silvester
<silvester.roessner@...> wrote: > Circular Function wrote: >> extract(Str) -> >> A = string:tokens(Str," "). >> lists:sort(A). > > I think the comma was just a typo and not the actual question. Am I right? > > You want var-assignment because you want to write something like: > > extract(Str) -> > A = string:tokens(Str," "), > A = lists:sort(A), > A = do_something_else(A), > A = and_finalize(A). extract(Str) -> Toks = string:tokens(Str, ""), SortedToks1 = lists:sort(Toks), ... :-) /J > > But that doesn't work. You have to use a new variable each time. > > extract(Str) -> > A1 = string:tokens(Str," "), > A2 = lists:sort(A1), > A3 = do_something_else(A2), > A4 = and_finalize(A3). > > Or this hard to read version > > extract(Str) -> > A = and_finalize(do_something_else(lists:sort(string:tokens(Str," ")))). > > This approach of functional languages has at least two big advantages: > > 1. You don't accidentally overwrite variables > 2. You can see all results in the debugger. > > And there should be no runtime penalty. Since the processor had to allocate and de-allocate the same amount of memory in each of the 3 cases -even if you don't see this in the code. > > mit freundlichen Grüßen / with kind regards > > Silvester Rößner > This message is intended for a particular addressee only and > may contain business or company secrets. If you have received > this email in error, please contact the sender and delete the > message immediately. Any use of this email, including saving, > publishing, copying, replication or forwarding of the message > or the contents is not permitted. > > _______________________________________________ > erlang-questions mailing list > erlang-questions@... > http://www.erlang.org/mailman/listinfo/erlang-questions > erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
| Free Forum Powered by Nabble | Forum Help |