Need help wrapping my head around lazy substituion errors

14 views
Skip to first unread message

Spencer Weight

unread,
Dec 9, 2013, 12:33:18 PM12/9/13
to byu-cs-330...@googlegroups.com
This is some code I'm toying with...

(define (extend-primes n)
  (begin
    (define p (last primes))
    (for ([i (in-range p n)])
      (cond
        [(andmap (lambda (x)
                   (not (zero? (modulo i x))))
                 primes)
         (append primes (list i))]))))


I'm trying to get (last primes) to evaluate and store the result in p so that I can iterate up to n

(last) is a function I defined to get the last element in a list

When I try to do something like the code above, because of the lazy language substitution I get this error
in-range: contract violation
  expected: real?
  given: #<promise:p>


How can I get (last primes) to evaluate so that I can use the result in the next part of the procedure?


Spencer Weight

unread,
Dec 9, 2013, 12:34:09 PM12/9/13
to byu-cs-330...@googlegroups.com
I'm calling the code with (extend-primes 13)
and primes is defined as (list 2)

Andrew Kent

unread,
Dec 9, 2013, 12:42:01 PM12/9/13
to Spencer Weight, BYU CS 330 Fall 2013
You need a strictness point that *forces* the promise to give you what you want.


--
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.



--
-Andrew

Jay McCarthy

unread,
Dec 9, 2013, 12:38:45 PM12/9/13
to Spencer Weight, byu-cs-330...@googlegroups.com
This code is in the totally wrong direction. 

When I get back to my desk I'll give you more low level details. 

Sent from my iPhone

Dan Burton

unread,
Dec 9, 2013, 1:44:40 PM12/9/13
to Jay McCarthy, Spencer Weight, byu-cs-330...@googlegroups.com
It's the wrong direction because it is an imperative approach to the problem: build a list, fetch the last prime, append to that list, repeat.

Don't think of it that way. Laziness works best with a declarative approach.

-- Dan Burton

Jay McCarthy

unread,
Dec 9, 2013, 5:02:35 PM12/9/13
to Dan Burton, Spencer Weight, byu-cs-330...@googlegroups.com
Yes... meaning try to define what the primes ARE /not/ "how to get them".

Jay
--
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

Spencer Weight

unread,
Dec 11, 2013, 2:44:58 PM12/11/13
to byu-cs-330...@googlegroups.com, Dan Burton, Spencer Weight
Okay, so I know what primes are now, but I'm struggling to figure out how to program prime? and prime in a declarative manner.

I've been looking at the examples for nats and fibs and I'm still a little shaky on how to accomplish what I need.

I guess the thing I am hung up on is to test if a number is prime, then I need to see if it is divisible by any the primes before it.  I'm having trouble trying to understand how I would figure out all the primes before the prime I'm testing so that I can get an accurate answer.

For example if I called (prime? 13), then the procedure should try dividing 13 by 11, 7, 5, 3, and 2 and if they don't divide evenly, then the number is prime.  How do I calculate for 11, 7, 5, 3, and 2

If I have this all wrong, please let me know.

Andrew Kent

unread,
Dec 11, 2013, 3:20:55 PM12/11/13
to Spencer Weight, BYU CS 330 Fall 2013, Dan Burton
A number is prime if it is not divisible by primes less than it - this is because of the definition of prime and the fundamental theorem of arithmetic.

A stronger requirement which implies the above is a number is prime if it is not divisible by *any* number less than it.


--
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.



--
-Andrew

Jay McCarthy

unread,
Dec 11, 2013, 2:54:25 PM12/11/13
to Spencer Weight, byu-cs-330...@googlegroups.com, Dan Burton
Maybe a function like take-while would be useful to pull the prefix of a list before a certain number. :)

Sent from my iPhone
--

Willard Hagen

unread,
Dec 11, 2013, 3:32:13 PM12/11/13
to BYU CS 330 Fall 2013


---------- Forwarded message ----------
From: Willard Hagen <willar...@gmail.com>
Date: Wed, Dec 11, 2013 at 1:32 PM
Subject: Re: Need help wrapping my head around lazy substituion errors
To: Jay McCarthy <jay.mc...@gmail.com>


I used a for-each in my take-while, wouldn't this take away from the non-imperative programming model?
--
-Willard



--
-Willard

Jay McCarthy

unread,
Dec 11, 2013, 6:14:18 PM12/11/13
to Willard Hagen, BYU CS 330 Fall 2013
Yes, that's a sign your take-while is wrong
Reply all
Reply to author
Forward
0 new messages