Results 1 to 6 of 6

Thread: noob scheme question

  1. #1
    Join Date
    Oct 2007
    Beans
    411

    noob scheme question

    Hi,
    I'm starting to work a bit with scheme, and I'm trying to write a procedure that will determine if a number is prime. This is a very basic, brute force solution to that problem, but I'm having some trouble with the scheme syntax.

    Code:
    (define (isPrime? x)
    ;; private procedures
      (define (isComposite? i)
        (if (= (remainder x i) 0)
    	#t
    	#f))
    
    ;; this is not defined yet. What this method will for two conditions: 1) if i is less than x-1 and if the remainder of x and i is zero, then it's
    ;; not a prime number and the return value of isPrime is false. 2) If i = x-1 and isComposite is false, then the number is prime and the return
    ;; value of isPrime is true. 3) If the previous two conditions are false, then isPrimeIteration recursively calls itself with i+1 as an argument. 
    ;; what I don't know how to do is have a cond block and have the consequent be return value for the entire method
    (define (isPrimeIteration i)
    i)
    
    (isPrimeIteration 2)
    
    )
    bp

  2. #2
    Join Date
    Jul 2009
    Location
    UK
    Beans
    222
    Distro
    Ubuntu

    Re: noob scheme question

    this looks like school homework.. you should try doing it on your own..

  3. #3
    Join Date
    Oct 2007
    Beans
    411

    Re: noob scheme question

    it's not, I promise

    I'm working my way thru the SICP book on my own...no problem, I'll get to that eventually, the book just started talking about defining private procedures defined in a parent procedure...

    I just don't see the equivalent of defining a boolean variable (as in java) and being able to set the value and return the variable when given conditions are met.
    I'm sure I'll come across if eventually...

  4. #4
    Join Date
    May 2006
    Beans
    1,790

    Re: noob scheme question

    Quote Originally Posted by badperson View Post
    it's not, I promise

    I'm working my way thru the SICP book on my own...no problem, I'll get to that eventually, the book just started talking about defining private procedures defined in a parent procedure...

    I just don't see the equivalent of defining a boolean variable (as in java) and being able to set the value and return the variable when given conditions are met.
    I'm sure I'll come across if eventually...
    So what problem with the Scheme syntax are you having?

  5. #5
    Join Date
    Dec 2006
    Beans
    256

    Re: noob scheme question

    Code:
    (define (isPrime? x)
    ;; private procedures
      (define (isComposite? i)
        (if (= (remainder x i) 0)
    	#t
    	#f))
    
    ;; this is not defined yet. What this method will for two conditions: 
    ;; 1) if i is less than x-1 and if the remainder of x and i is zero, then it's
    ;; not a prime number and the return value of isPrime is false. 
    ;; 2) If i = x-1 and isComposite is false, then the number is prime and the return
    ;; value of isPrime is true. 
    ;; 3) If the previous two conditions are false, then isPrimeIteration
    ;; recursively calls itself with i+1 as an argument. 
    ;; what I don't know how to do is have a cond block and have the consequent 
    ;; be return value for the entire method
    Start out by implementing exactly what you described. The "return value" is merely whatever the last line executed evaluates to.

    Code:
      (define (isPrimeIteration i)
        (cond 
          ((< i (- x 1))
            (if (zero? (modulo x i))
              #f ) ) ; <== last line executed if these conditions are met
          ((= i (- x 1))
            (if (= (isComposite? i) #f)
              #t ) ) ; <== last line executed if these conditions are met
          (else
            (isPrimeIteration (+ i 1)) ) ) )
      (isPrimeIteration 2) )
    There is a serious problem though. If i=x-1 then isComposite? will never evaluate to true and your iteration will never end for prime x's.

    I would recommend forgetting about your isComposite? function (just test the modulo for zero). Focus on the three possibilities for i as it increments each time through the iteration:

    Code:
      (define (isPrimeIteration i)
        (cond 
          ((< i x)
            (if (zero? (modulo x i))
              ; ... what to do if remainder is zero
              ; ... what to do if remainder is non-zero 
              ) )
          ((= i x)
            (if (zero? (modulo x i))
              ; ... what to do if remainder is zero
              ; ... what to do if remainder is non-zero 
              ) )
          (else
            ; ... what to do if i is greater than x
            ) )
      (isPrimeIteration 2) )
    Last edited by saulgoode; November 20th, 2010 at 12:49 AM.
    "We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson

  6. #6
    Join Date
    Oct 2007
    Beans
    411

    Re: noob scheme question

    The "return value" is merely whatever the last line executed evaluates to.
    that's what was tripping me up. The procedure below seems to work:
    Code:
    (define (isPrime? x)
    ;; brute force isPrime procedure. 11/26/10
    (define (isPrimeIteration i)
      (cond
       ((= x 1) #t)
        ((= i x) #t)
       ((= (remainder x i) 0) #f)
       (else 
        (isPrimeIteration (+ i 1))))
    )
    (isPrimeIteration 2)
    
    )
    thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •