0

Why won't this compile? Why does it come up saying that ordered is an undefined function?

(defun ordered (numbers)
  (sort numbers #'<))

(defun printing-ordered (numbers)
  (princ (apply #'ordered numbers)))

I am using SBCL, and Emacs with SlIME. The error I get when compiling is:

; compilation unit finished
;   Undefined function:
;     ORDERED
;   caught 1 STYLE-WARNING condition
Floofk
  • 123
  • 1
  • 11
  • You might want to explain what you do. What you do with SLIME? The WARNING happens when you compile a function with uses an unknown function. This means that `ORDERED` is unknown for some reason. – Rainer Joswig Jan 10 '15 at 11:50
  • 2
    Unrelated to you question: `ordered` expect a list argument not lots of numeric arguments. `print-ordered` should probably just `(princ (ordered numbers))` – Sylwester Jan 10 '15 at 12:20
  • I have edited the question. And Sylwester, I do pass it a list. – Floofk Jan 10 '15 at 12:47
  • @Floofk Sylwester would seem to be correct. **sort** expects a list, so **ordered** expects a list. **(apply #'foo '(1 2 3 4 5))** is equivalent to **(foo 1 2 3 4 5)**, so if you call **(printing-ordered '(1 2 3 4))**, you end up doing **(apply #'ordered '(1 2 3 4))** which is equivalent to **(ordered 1 2 3 4)**, and *not* to **(ordered '(1 2 3 4))**. You may want to use **funcall** instead. – Joshua Taylor Jan 10 '15 at 14:10
  • @JoshuaTaylor Why use `funcall` when the application is static? – Sylwester Jan 10 '15 at 16:31
  • @Sylwester, there's no point. I was trying to point out the difference between funcall and apply, which seems to be OP's confusion. In this case, it sounds like funcall is wanted, even though (in this static case), there's no need for either. – Joshua Taylor Jan 10 '15 at 16:32
  • Would it be too much to ask for someone to post a remade version of mine, as no matter what I do to my code on Emacs, it won't correctly compile, and if it does with a style warning, I can't call the function with valid arguments without Emacs asking me how to handle the error. – Floofk Jan 10 '15 at 19:42
  • You're probably only compiling the second function with *C-c C-c* with the cursor within the second function. You need to either compile the other function first or to compile the whole file with *C-c C-k*. You can also select a region first, then *C-c C-c* will compile it. – acelent Jan 11 '15 at 02:27
  • So I guess that might have been a small problem there. Thanks for pointing that out. – Floofk Jan 12 '15 at 14:21

1 Answers1

0

Running the same bits of code in my Emacs-Slime-SBCL combo gives expected results - a condition with message "invalid numbers of arguments". Try evaluating the first defun first if it helps at all. A bit mysterious indeed.

As side commentary; just as @Sylwester did, I noticed that you may have mixed funcall and apply together. The latter "unpacks" a list argument into several separate arguments while the former just passes on the given arguments as-is.

mike3996
  • 17,047
  • 9
  • 64
  • 80