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

Haskell list comprehensions

Name: Anonymous 2009-05-11 0:44

Is there something deeper to it, or is it just a shitty excuse for not writing, e.g. three for loops in [(a, b, c) | a <- [1..10], b <- [1..10], c <- [1..10], a^2 + b^2 == c^2]
and it still being O(n3)?

Name: Anonymous 2009-05-11 1:02

It's a nice syntax.
You can compile it into C and see that it folds into three loops.

Name: Anonymous 2009-05-11 1:06

That's what I expected, haskell can't even optimize loops *grabs dick*

Name: Anonymous 2009-05-11 1:33

>>1
For Loops
Is there something deeper to it, or is it just a shitty way to write e.g. [(a, b, c) | a <- [1..10], b <- [1..10], c <- [1..10], a^2 + b^2 == c^2]?

Also I don't think you know how big O notation works.

Name: Anonymous 2009-05-11 1:58

>>4
Also I don't think you know how big O notation works.
You're clueless.

Name: Anonymous 2009-05-11 2:22

>>5
And you're an sagefaulter.

Name: Anonymous 2009-05-11 2:35

haskell
optimize

1/10

Name: Anonymous 2009-05-11 2:35

>>5
Then what makes that snippet of code exponential? Looks like linear time to me.

Name: Anonymous 2009-05-11 2:38

>>8
Except it's three loops.

Name: Anonymous 2009-05-11 2:45

>>9
YHBT

Name: Anonymous 2009-05-11 2:47

>>10
YHBT2

Name: Anonymous 2009-05-11 3:13

(\n -> [(a, b, c) | a <- [1..n], b <- [1..n], c <- [1..n], a^2 + b^2 == c^2])

Name: Anonymous 2009-05-11 3:27

>>9
Nested loops? A Cartesian product sort of thing? What a maze of syntax.

Name: Anonymous 2009-05-11 5:23

>>11
Actually, it's
YHBT3

Name: Anonymous 2009-05-11 9:50

Name: Anonymous 2009-05-11 10:26

Please do not summon DONS here.  He will ruin /prog/ by proving us all wrong.

Name: Anonymous 2009-05-11 13:42

>>16
dons doesn't prove anyone wrong he just thinks he does.

Name: Anonymous 2009-05-11 14:33

primes = 2 : 3 : 5 : 7 : [k + r | k <- [0, 30..], r <- [11, 13, 17, 19, 23, 29, 31, 37], primeTest (k + r)]
    where primeTest n = all ((0 /=) . mod n) . takeWhile ((n >=) . join (*)) $ drop 3 primes

Name: drg 2009-05-11 22:39

drgesrg(***()

Name: Anonymous 2009-05-12 2:07

>>1
it's just a stupid way of writing
catMaybes $ liftM3 (\a b c -> if a^2 + b^2 == c^2 then Just (a,b,c) else Nothing) [1..10] [1..10] [1..10]

Name: Anonymous 2009-05-12 2:09

>>20
catMaybes $ liftM3 (flip flip Nothing . (flip .) . ap (ap . (liftM2 if' .) . flip flip (^2) . (((.) . (==)) .) . (. (^2)) . (+) . (^2)) (((Just .) .) . (,,))) [1..10] [1..10] [1..10]

Name: dons 2009-05-12 3:57

>>21
Hahaha, this is actually very funny!
But you forgot zipWith3 and two (***)'s.

Name: Anonymous 2009-05-12 7:48

filter (liftA2 (==) (uncurry ((+) `on` (^2)).fst) ((^2).snd)) $ (join $ join $ liftA3 (((,).).(,))) [1..10]

alternatively

filter (uncurry (==) . (uncurry ((+) `on` (^2)) *** (^2))) $ (join $ join $ liftA3 (((,).).(,))) [1..10]

Hand-crafted pointless style is superior. Please, do suggest improvements. (Maybe some parens can be removed... I don't have a compiler handy to test, though.)

Name: Anonymous 2009-05-12 12:24

This is based on >>20's version:
catMaybes . (join.join) (liftM3 ((liftA2.liftA2.liftA2) (>>) ((((. (^2)) . (guard .)) .) . ((==) .) . ((+) `on` (^2))) (((return .) .) . (,,)))) . enumFromTo 1

pl'ing the actual guard (\a b c -> guard (a^2 + b^2 == c^2)) turns out to be inelegant. This a bit better:
catMaybes . (join.join) (liftM3 ((liftA2.liftA2.liftA2) (>>) (\a b c -> guard (a^2 + b^2 == c^2)) (((return .) .) . (,,)))) . enumFromTo 1

But, not pointfree.

Name: Anonymous 2009-05-12 15:15

please die ? go to #haskell for this bullshit you scum.

Name: Anonymous 2009-05-12 16:06

>>25
YHBT

Name: Anonymous 2009-05-12 16:43

>>25
This is /prog/, the official text board of #haskell

Name: Anonymous 2009-05-12 17:32

>>23
((a,b),c)(a,b,c)
also,
filter (uncurry (==) . (uncurry (+) . join (***) (^2) *** (^2))) $ (join $ join $ liftA3 (((,).).(,))) [1..10]
doesn't use that `on` bullshit.

and then there's this:
filter (\(a,b,c) -> a^2 + b^2 == c^2) $ (join $ join $ liftA3 (,,)) [1..10]

Name: Anonymous 2009-05-12 18:04

>>28
And this:
filter (\[a,b,c] -> a^2 + b^2 == c^2) $ sequence $ replicate 3 [1..10]

Name: Anonymous 2009-05-12 18:31

>>29
And this:
filter (\[a,b,c] -> a^2 + b^2 == c^2) $ replicateM 3 [1..10]

Name: Anonymous 2009-05-12 18:49

>>29-30
[a](a,b,c)

Name: Anonymous 2009-05-12 19:06

>>31
It doesn't matter.

Name: Anonymous 2009-05-12 19:15

>>31
    Couldn't match expected type `(t, t, t)'
           against inferred type `[a]'

Name: Anonymous 2009-05-12 19:19

>>32
What doesn't matter? Nevermind...

Name: Anonymous 2010-11-26 2:43

Name: Anonymous 2010-12-22 16:06

Name: Anonymous 2011-02-04 17:50

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