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

前端 未结 1 1904
你的背包
你的背包 2021-01-29 08:56

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


        
相关标签:
1条回答
  • 2021-01-29 09:28

    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))
    
    0 讨论(0)
提交回复
热议问题