Noob question about list comprehensions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Noob question about list comprehensions

Tako Schotanus
Hello,

I was going through some of the tuturials and trying out different (syntactic) alternatives to the given solutions and I I got to this line:

    length [chain x | x <- [1..100] , length (chain x) > 15]

Now, there's nothing wrong with it, it works of course. But the application of chain x is repeated twice and I wondered if there was a way for a guard in a list comprehension to refer to the item being produced?

Like this for example (invented syntax):

    length [@c(chain x) | x <- [1..100] , length c > 15]

NB: Just to make clear, I'm not asking if there is an alternative way of preventing the repetition, of course there is, I'm just wondering about this very specific case within list comprehensions.

-Tako


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Noob question about list comprehensions

MigMit
  length [c | x <- [1..100], let c = chain x, length c > 15]

16.02.2011 12:19, Tako Schotanus пишет:

> Hello,
>
> I was going through some of the tuturials and trying out different (syntactic) alternatives to the given solutions and I I got to this line:
>
> *length [chain x | x <- [1..100] , length (chain x) > 15]*
>
> Now, there's nothing wrong with it, it works of course. But the application of chain x is repeated twice and I wondered if there was a way for a guard in a list comprehension to refer to the item
> being produced?
>
> Like this for example (invented syntax):
>
> *length [@c(chain x) | x <- [1..100] , length c > 15]*
>
> NB: Just to make clear, I'm not asking if there is an alternative way of preventing the repetition, of course there is, I'm just wondering about this very specific case within list comprehensions.
>
> -Tako
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Noob question about list comprehensions

Stephen Lavelle-2
In reply to this post by Tako Schotanus
Might better ways, but the following work:

length [c | x <- [1..100], let c = chain x , length c > 15]
length [c | x <- [1..100], c <- [chain x] , length c > 15]


On Wed, Feb 16, 2011 at 9:19 AM, Tako Schotanus <[hidden email]> wrote:
Hello,

I was going through some of the tuturials and trying out different (syntactic) alternatives to the given solutions and I I got to this line:

    length [chain x | x <- [1..100] , length (chain x) > 15]

Now, there's nothing wrong with it, it works of course. But the application of chain x is repeated twice and I wondered if there was a way for a guard in a list comprehension to refer to the item being produced?

Like this for example (invented syntax):

    length [@c(chain x) | x <- [1..100] , length c > 15]

NB: Just to make clear, I'm not asking if there is an alternative way of preventing the repetition, of course there is, I'm just wondering about this very specific case within list comprehensions.

-Tako


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Noob question about list comprehensions

Ozgur Akgun
In reply to this post by Tako Schotanus
On 16 February 2011 09:19, Tako Schotanus <[hidden email]> wrote:
I wondered if there was a way for a guard in a list comprehension to refer to the item being produced?
 
I'm just wondering about this very specific case

Then, the answer is no.

As others have noted, let binding is the way to go. 

Ozgur

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Noob question about list comprehensions

Tako Schotanus
Ok, thanks all, that was what I was looking for :)

-Tako


On Wed, Feb 16, 2011 at 10:46, Ozgur Akgun <[hidden email]> wrote:
On 16 February 2011 09:19, Tako Schotanus <[hidden email]> wrote:
I wondered if there was a way for a guard in a list comprehension to refer to the item being produced?
 
I'm just wondering about this very specific case

Then, the answer is no.

As others have noted, let binding is the way to go. 

Ozgur


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe