lazy (prime?/fast)

32 views
Skip to first unread message

Scott Heidbrink

unread,
Dec 5, 2013, 5:31:56 PM12/5/13
to byu-cs-330...@googlegroups.com
I don't understand (prime?/fast n) so it tests if n is prime "but tests only prime factors from primes/fast," but primes/fast is all primes constructed with prime?/fast......I'm having trouble visualizing how this would work, can you explain it a different way or show me an example?

Jay McCarthy

unread,
Dec 5, 2013, 5:39:35 PM12/5/13
to Scott Heidbrink, BYU CS 330 Fall 2013
I gave you many examples in class. nats was based on nats and fibs was
based on fibs. Like that.
> --
> You received this message because you are subscribed to the Google Groups
> "byu-cs-330-fall-2013" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to byu-cs-330-fall-...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Jay McCarthy <j...@cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Scott Heidbrink

unread,
Dec 5, 2013, 5:43:02 PM12/5/13
to byu-cs-330...@googlegroups.com, Scott Heidbrink
But those both started with a base case and from the way i understand this is neither of these has a base case?


On Thursday, December 5, 2013 3:39:35 PM UTC-7, Jay McCarthy wrote:
I gave you many examples in class. nats was based on nats and fibs was
based on fibs. Like that.

On Thu, Dec 5, 2013 at 3:31 PM, Scott Heidbrink
<scott.h...@gmail.com> wrote:
> I don't understand (prime?/fast n) so it tests if n is prime "but tests only
> prime factors from primes/fast," but primes/fast is all primes constructed
> with prime?/fast......I'm having trouble visualizing how this would work,
> can you explain it a different way or show me an example?
>
> --
> You received this message because you are subscribed to the Google Groups
> "byu-cs-330-fall-2013" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Dan Burton

unread,
Dec 5, 2013, 5:49:42 PM12/5/13
to Scott Heidbrink, byu-cs-330...@googlegroups.com
(prime?/fast n) so it tests if n is prime "but tests only prime factors from primes/fast," but primes/fast is all primes constructed with prime?/fast

The base case is the first prime number: 2. Once you've bootstrapped the computation by hardcoding 2 as the first prime number, then the mutual recursion should work just fine. Where you put the base case is up to you.

Jay McCarthy

unread,
Dec 5, 2013, 6:33:54 PM12/5/13
to Dan Burton, Scott Heidbrink, byu-cs-330...@googlegroups.com
Yup, Dan's right, there's nothing special about the base cases that we
used for nats and fibs. You just need to pick something.
> --
> You received this message because you are subscribed to the Google Groups
> "byu-cs-330-fall-2013" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to byu-cs-330-fall-...@googlegroups.com.

Ben Draut

unread,
Dec 9, 2013, 10:20:28 AM12/9/13
to Jay McCarthy, BYU CS 330 Fall 2013
How do we avoid cyclic dependency issues in Racket when doing mutual recursion like this?

When both prime?/fast and primes/fast call each other, Racket tells me that I can't use one of them before it's definition. (Whichever is defined second)

Jay McCarthy

unread,
Dec 9, 2013, 11:07:46 AM12/9/13
to Ben Draut, BYU CS 330 Fall 2013
On Mon, Dec 9, 2013 at 8:20 AM, Ben Draut <dra...@gmail.com> wrote:
> How do we avoid cyclic dependency issues in Racket when doing mutual
> recursion like this?
>
> When both prime?/fast and primes/fast call each other, Racket tells me that
> I can't use one of them before it's definition. (Whichever is defined
> second)

Put the test for both after each. For example

CODE 1
TEST 1
CODE 2
TEST 2

is what you'd normally do, but you should do

CODE 1
CODE 2
TEST 1
TEST 2
Reply all
Reply to author
Forward
0 new messages