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

倖福魔咒の 提交于 2021-02-05 12:22:35

问题


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)

回答1:


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


来源:https://stackoverflow.com/questions/65829166/how-can-write-a-program-in-scheme-to-find-factors-of-a-list-of-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!