[Haskell-beginners] code critique
Alexander Batischev
eual.jp at gmail.com
Wed Jul 27 02:34:23 CEST 2011
Hi!
On Wed, Jul 27, 2011 at 09:47:14AM +1000, Jeff Lasslett wrote:
> Also, if I understand correctly, solutions 1 & 2 will only produce one
> pair, whereas solution 3 (based on list comprehensions) will produce
> all pairs that sum to the total.
>
> On 27 July 2011 07:33, Gary Klindt <gary.klindt at uni-konstanz.de> wrote:
> > sumCheck3 i as bs = not $ null [(x,y) | x <- as, y <- bs, x+y==i ]
Maybe I misunderstood you, but you aren't quite right. In third
solution, *list comprehension* will produce all the pairs. But you're
applying 'null' to it, and since everything in Haskell is lazy by
default, only *one* pair will be produced.
Just for your information: that's why it's always advised to use 'null'
to check if list is empty. Newbies often try to do that in more obvious
way:
length list == 0
but that have very nasty consequence: to calculate length of the list,
you should compute the value of each element. If you use null, only
first (if any) element gets computed, and then null immediately returns.
Ah, yes, one more note: first two solutions return pair that satisfies
your task, while third solution will only return some Bool value
indicating if such pair exists.
--
Regards,
Alexander Batischev
1024D/69093C81
F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110727/a94f3366/attachment.pgp>
More information about the Beginners
mailing list