Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

dictionary - How can write a program in scheme to find factors of a list of numbers

This is the code for a single integer, how can it extends to list of function?

(define (factors n)
      (define (*factors d)
        (cond ((> d n) (list))
              ((= (modulo n d) 0) (cons d (*factors (+ d 1))))
              (else (*factors (+ d 1)))))
      (*factors 1))
    
    (display (factors 1111111))
    (newline)
question from:https://stackoverflow.com/questions/65829166/how-can-write-a-program-in-scheme-to-find-factors-of-a-list-of-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use for-each to iterate over a list.

(define (factors n)
  (define (*factors d)
    (cond ((> d n) (list))
          ((= (modulo n d) 0) (cons d (*factors (+ d 1))))
          (else (*factors (+ d 1)))))
  (*factors 1))

(define arbitarily-large-input (list 10 11 12))

(for-each (lambda (x)
            (display x)
            (newline))
          (map factors arbitarily-large-input))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...