2
\$\begingroup\$

This program calculates common factor for given numbers. The original use case is to find a possible resolution for pixel games that scale well with new screen resolutions (that's why I don't have any check and optimizations for big numbers and use simple brute force).

(define (fun1 l d)
    (if (apply = (map (lambda (x) (remainder x d)) l))
        (print d)))


(define (fun2 l)
    (do ((i 1 (+ i 1)))
        ((= i (apply min l)) i)
        (fun1 l i)))

(fun2 `(1080 720))

I'm learning lisp and scheme and want to know if there's any way to make this code more functional and lispy.

Also, I'm wondering if (apply = (1 2 3) is the right way to do it. I haven't found another way to force feed list of numbers to the = function or break numbers out of the list.

\$\endgroup\$
2
\$\begingroup\$

Scheme has a built-in function called gcd, which computes the greatest common divisor. You don't need to write your own version. (gcd 1080 720) returns 360.

One other thing I should mention is that, as a matter of style, you should prefer to use quote rather than backquote if you have nothing to unquote.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.