Comprehending List Comprehensions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Comprehending List Comprehensions

Peter Caven
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.



Reply | Threaded
Open this post in threaded view
|

Comprehending List Comprehensions

Ulf Wiger-4

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



Reply | Threaded
Open this post in threaded view
|

Comprehending List Comprehensions

Håkan Mattsson-6
In reply to this post by Peter Caven
On Mon, 26 Nov 2001, Peter Caven wrote:

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

The Token generator with contribute with a solution for each element
in the list that string:substr/2 returns. See this little example:

    5> [Token  || Token <- string:substr("1+1-2", 1, 1)].
    "1"
    6> [{Token}  || Token <- string:substr("1+1-2", 1, 1)].
    [{49}]
    7> [{Token}  || Token <- string:substr("1+1-2", 1, 2)].
    [{49},{43}]
    8> [{Token}  || Token <- string:substr("1+1-2", 1, 3)].
    [{49},{43},{49}]

You problably intended to do this:

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

/H?kan

---
H?kan Mattsson
Ericsson
Computer Science Laboratory
http://www.ericsson.com/cslab/~hakan/




Reply | Threaded
Open this post in threaded view
|

Comprehending List Comprehensions - Thanks!

Peter Caven
In reply to this post by Ulf Wiger-4
Many thanks to Uffe and H?kan!
I see now that I'll need to be careful about any strings returned from a
generator.

By the way - Erlang is a very nice language!
I get the same kind of liberated feeling with Erlang as I do when programing
in Python.

-- Peter.


----- Original Message -----
From: "Ulf Wiger" <etxuwig>
To: "Peter Caven" <p.caven>
Cc: <erlang-questions>
Sent: Monday, November 26, 2001 7:28 AM
Subject: Re: Comprehending List Comprehensions


>
> 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
>