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.

mdfst13
22.4k6 gold badges34 silver badges70 bronze badges
asked Jul 4, 2016 at 16:28
\$\endgroup\$

1 Answer 1

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.

answered Jul 4, 2016 at 17:36
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.