Skip to content

Commit 1e1985c

Browse files
authored
Add rough update of list comprehensions
1 parent 5c85fdd commit 1e1985c

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

doc/Language/haskell-to-p6.pod6

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,41 @@ data shapes become quite intuitive, but it takes a bit of practice.
367367
368368
=head2 List Comprehensions
369369
370-
TODO compare haskell list comprehensions to Perl 6 gather/take
370+
There are no explicit list comprehensions in Perl6. But rather, you can achieve list comprehensions
371+
a couple of different ways.
372+
373+
Here is a trivial example in Haskell:
374+
375+
=begin code :skip-test
376+
evens = [ x | x <- [0..100], even x ]
377+
=end code
378+
379+
And now in Perl6 using `if` and `for`:
380+
381+
=begin code
382+
my @evens = ($_ if $_ %% 2 for ^100);
383+
=end code
384+
385+
Here is the creation of tuples in Haskell:
386+
387+
=begin code :skip-test
388+
tuples = [(i,j) | i <- [1,2],
389+
j <- [1..4] ]
390+
-- [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4)]
391+
=end code
392+
393+
And in Perl6:
394+
395+
=begin code
396+
my @tuples = (1,2) X 1..4;
397+
# [(1 1) (1 2) (1 3) (1 4) (2 1) (2 2) (2 3) (2 4)]
398+
=end code
399+
400+
See this design document for more information on what kinds of list comprehensions are possible
401+
in Perl6: http://design.perl6.org/S04.html#The_do-once_loop
402+
403+
As you can see, when you get into some more advanced Haskell list comprehensions, Perl6
404+
does not translate exactly the same, but it's possible to do the same things, nonetheless.
371405
372406
=head2 Fold
373407

0 commit comments

Comments
 (0)