Comprehending List Comprehensions

Ulf Wiger etxuwig@REDACTED
Mon Nov 26 13:28:39 CET 2001


The following comprehension is probably what you're after:

[{string:substr("1+1-2", Start, Length}) ||
  {Start, Length} <- [{1,1}, {2,2}, {4,2}]].

In your example below, Token <- string:substr(...) generates
Token from the string output by string:substr/2, essentially
setting Token = <char>, for each character in the string.

/Uffe

On Mon, 26 Nov 2001, Peter Caven wrote:

>Hello,
>I am at the early stages of learning Erlang and I've encountered what seems
>to be strange behaviour in list comprehensions. The expression:
>
>[{Token} || {Start, Length} <- [{1,1},{2,2},{4,2}], Token <-
>string:substr("1+1-2", Start, Length)]
>
>returns:
>
>[{49},{43},{49},{45},{50}]
>
>when I expected it to return:
>
>[{"1"},{"+1"},{"-2"}]
>
>Can someone please point out where my mistake is?
>
>Thanks,
>Peter.
>
>

-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson Telecom AB, ATM Multiservice Networks




More information about the erlang-questions mailing list