2006-07-21 19:42:18 -04:00
|
|
|
! Rewritten by Matthew Willis, July 2006
|
2004-08-15 19:23:47 -04:00
|
|
|
! Copyright (C) 2004 Chris Double.
|
2006-11-04 12:07:17 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-07-22 17:50:36 -04:00
|
|
|
|
|
|
|
USING: lazy-lists math kernel sequences test ;
|
2004-08-15 19:23:47 -04:00
|
|
|
IN: lazy-examples
|
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: naturals 0 lfrom ;
|
2006-09-13 09:09:59 -04:00
|
|
|
: positives 1 lfrom ;
|
2006-07-22 06:52:22 -04:00
|
|
|
: evens 0 [ 2 + ] lfrom-by ;
|
|
|
|
: odds 1 lfrom [ 2 mod 1 = ] lsubset ;
|
|
|
|
: powers-of-2 1 [ 2 * ] lfrom-by ;
|
|
|
|
: ones 1 [ ] lfrom-by ;
|
2006-07-22 16:48:42 -04:00
|
|
|
: squares naturals [ dup * ] lmap ;
|
2006-07-22 17:50:36 -04:00
|
|
|
: first-five-squares 5 squares ltake list>array ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
|
|
|
: divisible-by? ( a b -- bool )
|
2006-07-22 06:52:22 -04:00
|
|
|
#! Return true if a is divisible by b
|
|
|
|
mod 0 = ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-11-04 12:07:17 -05:00
|
|
|
: filter-multiples ( n list -- list )
|
2006-07-22 06:52:22 -04:00
|
|
|
#! Given a lazy list of numbers, filter multiples of n
|
2006-09-13 09:09:59 -04:00
|
|
|
swap [ divisible-by? not ] curry lsubset ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-13 21:40:13 -04:00
|
|
|
! : primes ( -- list )
|
|
|
|
! 2 lfrom [ filter-multiples ] lapply ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-11-04 12:07:17 -05:00
|
|
|
! : first-ten-primes 10 primes ltake list>array ;
|