badperson
November 30th, 2010, 10:28 PM
Hi,
I'm trying to write the following routine that will return a list of prime factors, e.g., 24 = 2 * 2 * 2 * 3.
I have the basic method where it should work but I'm getting an error with the line that tries to append to the list:
(define (getPrimeFactors num)
;; 1) start with 2, if x is divisible by 2, add 2 to the list. x becomes x/2, and
(define (getPrimeFactorsIteration num divisor primes)
(cond
((= 1 num) primes)
((not (isPrime? divisor)) (getPrimeFactorsIteration num (+ 1 divisor) primes))
((and (isPrime? divisor) (= (remainder num divisor) 0))
;; add divisor to primes, then new iteration
(getPrimeFactorsIteration (/ num divisor) 2 (append primes divisor)))))
(getPrimeFactorsIteration num 2 '())
)
when running in emacs I get this error:
;The object 2, passed as an argument to append, is not a list.
;To continue, call RESTART with an option number:
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.
I tried adding a number to a list with the same basic syntax....
efine testlist '(1 2 3))
(define num 4)
(append testlist 4)
;Value 13: (1 2 3 . 4)
any ideas?
bp
I'm trying to write the following routine that will return a list of prime factors, e.g., 24 = 2 * 2 * 2 * 3.
I have the basic method where it should work but I'm getting an error with the line that tries to append to the list:
(define (getPrimeFactors num)
;; 1) start with 2, if x is divisible by 2, add 2 to the list. x becomes x/2, and
(define (getPrimeFactorsIteration num divisor primes)
(cond
((= 1 num) primes)
((not (isPrime? divisor)) (getPrimeFactorsIteration num (+ 1 divisor) primes))
((and (isPrime? divisor) (= (remainder num divisor) 0))
;; add divisor to primes, then new iteration
(getPrimeFactorsIteration (/ num divisor) 2 (append primes divisor)))))
(getPrimeFactorsIteration num 2 '())
)
when running in emacs I get this error:
;The object 2, passed as an argument to append, is not a list.
;To continue, call RESTART with an option number:
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.
I tried adding a number to a list with the same basic syntax....
efine testlist '(1 2 3))
(define num 4)
(append testlist 4)
;Value 13: (1 2 3 . 4)
any ideas?
bp