title: Prolog: How can I implement the sum of squares of two largest numbers out of three?
body: <p>Exercise 1.3 of the book <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html" rel="nofollow"><em>Structure and Interpretation of Computer Programs</em></a> asks the following:</p>
  
  <blockquote>
    <p>Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.</p>
  </blockquote>
  
  <p>I'm learning Prolog. Here's the function I tried to implement:</p>
  
  <pre><code>square(X, Y) :- Y is X * X.
  squareTwoLargest(X, Y, Z, R) :- 
      R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z).
  </code></pre>
  
  <p>However, when I run it, it gives the following error: <code>ERROR: is/2: Arguments are not sufficiently instantiated</code>. I think I'm not only not getting Prolog's syntax, but I'm also not getting the logic programming paradigm yet. So, how could I implement this function in good logic programming style?</p>