[Haskell-beginners] Haskell triangular loop (correct use of (++))
Silent Leaf
silent.leaf0 at gmail.com
Sun Apr 10 18:12:56 UTC 2016
Dunno if that's what you're interested in, or if it's best in terms of
efficiency, but there's syntax inside the language made just for this kind
of thing, called list comprehension. It comes from math's definition of
sets by comprehension, and since it's part of the language I'd have a
tendency to trust its efficiency, but I might be entirely wrong on this
aspect.
Anyways, for your problem, say I want to create the set of pairs of your
example:
let result = [(x,y) | let xs = [1,2,3,0], (x,ix) <- zip xs [1,2..], y <-
drop ix xs, x /= y]
in result == [(1,2),(1,3),(1,0),(2,3),(2,0),(3,0)]
Basically the syntax is: [ parameterized result element | conditions on the
parameters]
the conditions being a sequence of comma-separated items that are either:
local variable declarations without the 'in', example being (let input =
[1,2,3,0]), pattern-accepting generation of values from a list, or
conditions on the parameters (here x and y).
In order to build y's list I decided to zip xs with a list of indexes
starting to 1, thereby ensuring no pair is twice in, considering the order
doesn't matter.
I'd bet the syntax is monad/do related, with all those right-to left
arrows. Plus it fits the bill of what's actually happening here.
Of course if you want a function, you can still write thereafter
mkpairs :: Integral a => a -> [(a,a)]
mkpairs n = [(x,y) | let xs = [1..n] ++ [0], (x,ix) <- zip xs [1,2..], y <-
drop ix xs, x /= y]
If you don't care about the order, I guess xs = [0..n] will be much more
efficient, relatively speaking.
Pretty sure the function even works for n == 0, since y <- drop 1 [0] won't
have a thing to yield, hence, result = [].
If that interests you:
https://wiki.haskell.org/List_comprehension
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160410/e60594a9/attachment.html>
More information about the Beginners
mailing list