During the execution of my code I get the following errors in the different Scheme implementations:
Racket:
application: not a procedure;
expected a procedure that can be applied to arguments
given: '(1 2 3)
arguments...:
Ikarus:
Unhandled exception
Condition components:
1. &assertion
2. &who: apply
3. &message: "not a procedure"
4. &irritants: ((1 2 3))
Chicken:
Error: call of non-procedure: (1 2 3)
Gambit:
*** ERROR IN (console)@2.1 -- Operator is not a PROCEDURE
((1 2 3) 4)
MIT Scheme:
;The object (1 2 3) is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
Chez Scheme:
Exception: attempt to apply non-procedure (1 2 3)
Type (debug) to enter the debugger.
Guile:
ERROR: In procedure (1 2 3):
ERROR: Wrong type to apply: (1 2 3)
Chibi:
ERROR in final-resumer: non procedure application: (1 2 3)
Why is it happening
Scheme procedure/function calls look like this:
Both operator and operands can be variables like
test
, and+
that evaluates to different values. For a procedure call to work it has to be a procedure. From the error message it seems likely thattest
is not a procedure but the list(1 2 3)
.All parts of a form can also be expressions so something like
((proc1 4) 5)
is valid syntax and it is expected that the call(proc1 4)
returns a procedure that is then called with5
as it's sole argument.Common mistakes that produces these errors.
Trying to group expressions or create a block
When the predicate/test is true Scheme assumes will try to evaluate both
(proc1)
and(proc2)
then it will call the result of(proc1)
because of the parentheses. To create a block in Scheme you usebegin
:In this
(proc1)
is called just for effect and the result of teh form will be the result of the last expression(proc2)
.Shadowing procedures
Here the parameter is called
list
which makes the procedurelist
unavailable for the duration of the call. One variable can only be either a procedure or a different value in Scheme and the closest binding is the one that you get in both operator and operand position. This would be a typical mistake made by common-lispers since in CL they can uselist
as an argument without messing with the functionlist
.wrapping variables in
cond
While besides the predicate expression
(< 5 4)
(test)
looks correct since it is a value that is checked for thurthness it has more in common with theelse
term and whould be written like this:A procedure that should return a procedure doesn't always
Since Scheme doesn't enforce return type your procedure can return a procedure in one situation and a non procedure value in another.
Undefined values like
#<void>
,#!void
,#<undef>
, and#<unspecified>
These are usually values returned by mutating forms like
set!
,set-car!
,set-cdr!
,define
.The result of this code is undetermined since
set!
can return any value and I know some scheme implementations like MIT Scheme actually return the bound value or the original value and the result would be25
or10
, but in many implementations you get a constant value like#<void>
and since it is not a procedure you get the same error. Relying on one implementations method of using under specification makes gives you non portable code.Passing arguments in wrong order
Imagine you have a fucntion like this:
If you by error swapped the arguments:
In higher order functions such as
fold
andmap
not passing the arguments in the correct order will produce a similar error.Trying to apply as in Algol derived languages
In algol languages, like JavaScript and C++, when trying to apply
fun
with argumentarg
it looks like:This gets interpreted as two separate expressions in Scheme:
The correct way to apply
fun
witharg
as argument is:Superfluous parentheses
This is the general "catch all" other errors. Code like
((+ 4 5))
will not work in Scheme since each set of parentheses in this expression is a procedure call. You simply cannot add as many as you like and thus you need to keep it(+ 4 5)
.Why allow these errors to happen?
Expressions in operator position and allow to call variables as library functions gives expressive powers to the language. These are features you will love having when you have become used to it.
Here is an example of
abs
:This switched between doing
(- x)
and(values x)
(identity that returns its argument) and as you can see it calls the result of an expression. Here is an example ofcopy-list
using cps:Notice that
k
is a variable that we pass a function and that it is called as a function. If we passed anything else than a fucntion there you would get the same error.Is this unique to Scheme?
Not at all. All languages with one namespace that can pass functions as arguments will have similar challenges. Below is some JavaScript code with similar issues: