Multiple Return Values (Lisp solution)

  • 0

    I'm not trying to start a war over preferred languages, I just enjoy working through these problems in a variety of languages on my own time to keep my wit sharp, and this problem raised an interesting use case for multiple return values. Here is a solution in common lisp (it's O(sqrt(N)) time complexity and is more or less the algorithm most people seem to have produced here):

    (defun sum-of-squares-p (i)
      (loop with top = (1- (floor (sqrt i)))
            for s from 1 upto top do
            (multiple-value-bind (div remain)
                (floor (sqrt (- i (* s s))))
                (when (= remain 0)
                  (return-from sum-of-squares-p
                               (values t s div)))))
      (values nil 0 0))

    The interesting thing about this solution is not the algorithm, which is much the same as other solutions here, but rather the return value - in a boolean or single-value context, it's either t (true) or nil (false), but because in Lisp a function can return multiple values, the integers one would sum and square are also returned, and can be used later. An example REPL session with the above defined:

    CL-USER> (sum-of-squares-p 5)
    CL-USER> (if (sum-of-squares-p 5) "Yep!" "Nah")
    CL-USER> (loop for i from 0 to 10 do
                   (multiple-values-bind (p a b) (sum-of-squares-p i)
                       (when p (format t "~A = ~A^2 + ~A^2~%" i a b))))
    5 = 1^2 + 2^2
    10 = 1^2 + 3^2

    I wanted to point it out as an interesting language feature. I wish more languages had this feature because it helps maintain abstraction boundaries in cases where subsequently useful values are a byproduct of whatever a function nominally computes.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.