Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Haskell help

Name: Anonymous 2010-03-28 10:15

Show how the single comprehension [(x , y) | x ← [1, 2, 3], y ←
[4, 5, 6]] with two generators can be re-expressed using two comprehensions
with single generators. Hint: make use of the library function concat
and nest one comprehension within the other.

It's been easy up until now, but I can't figure out how to nest one comprehension within the other. Any ideas besides reading SICP?

Name: Anonymous 2010-03-28 10:20

Read JAVA TUTORIALS

Name: Anonymous 2010-03-28 10:29

Is there anything wrong with [(x,x+3)|x<-[1,2,3]]? Is it too obvious?

Name: Anonymous 2010-03-28 10:30

>>3
That just gives [(1,4),(2,5),(3,6)]. The original example gives [(1,4),(1,5)...(3,5),(3,6)]

Name: Anonymous 2010-03-28 11:06

>>1
IT tells you in the hint
[Anonymous@/prog/ ~]$ ghci
GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :t concat
concat :: [[a]] -> [a]
Prelude> [(x,y) | x <- [1,2,3], y <- [4,5,6]]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
Prelude> concat [ [(x,y)| y <- [4,5,6]] | x <- [1,2,3] ]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]

The problem is in that Haskell's (and python's) comprehension syntax the tightest loops are farthest away from the body, when IMO they should be together.
[Anonymous@/prog/ ~]$ rlwrap ikarus
Ikarus Scheme version 0.0.4-rc1+ (revision 1870, build 2010-03-23)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

(import (srfi :42)) ; Eager comprehensions
(list-ec (:list x (list 1 2 3))
           (:list y (list 4 5 6))
           (list x y))
((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))


[/smug lisp weenie]

Name: Anonymous 2010-03-28 11:12

>>5
Thanks, friend.

Name: Anonymous 2010-03-28 13:04

>>6
I'm not your friend, buddy.

Name: Anonymous 2010-03-28 13:22

>>7
I'm not your buddy, friend.

Name: Anonymous 2010-03-28 17:50

import itertools
itertools.product((1,2,3),(4,5,6))

Name: Anonymous 2010-03-28 17:51

>>9
is not an answer to his question.

Name: Anonymous 2010-03-29 7:36

>>10
well yeah bump

Name: Anonymous 2010-03-29 10:15

OP here.
>>5­-kun already answered my question, please stop bumping it you faggot.

Name: Anonymous 2010-03-29 16:20

>>12
Sorry

Name: Anonymous 2010-03-29 21:24



    import itertools
    itertools.product((1,2,3),(4,5,6))

Name: Anonymous 2010-03-29 21:36

>>14
( ≖‿≖)

Name: ​​​​​​​​​​ 2010-10-23 19:58

Name: Anonymous 2011-02-04 15:43

Name: Anonymous 2013-01-19 20:59

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List