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

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