No value returned by Common Lisp function
Asked Answered
L

2

6

I've read that every form in Common Lisp returns something when evaluated. However, recently I've been playing with ASDF API and found a function that returns nothing:

CL-USER> (asdf:clear-output-translations)
; No value

How is this possible and why doesn't it return something like NIL?

Lepsy answered 15/8, 2014 at 6:54 Comment(0)
L
10

Common Lisp allows functions to return from 0 upto MULTIPLE-VALUES-LIMIT values. The constant MULTIPLE-VALUES-LIMIT is 20 or larger.

The function VALUES allows one to return multiple values, including zero values.

Thus a common idiom is to use the form (values) when a function has no useful return value and is just called for side effects. Also this usually causes the Lisp listener (aka REPL) to not print anything as return value, which can be useful for aesthetic reasons.

Note that variables only have a single value and that one can bind only exactly one value to a variable.

Lapith answered 15/8, 2014 at 9:4 Comment(3)
I prefer to return nil in such cases... Interestingly, expression (eq nil (values)) gets evaluated to T on CLISP and SBCL.Lepsy
@Mark: You can't pass zero values an argument to a function parameter in Common Lisp. Thus it gets replaced with NIL. Thus that EQ is returning T is because NIL is eq to NIL.Lapith
multiple-value-call can work for functions that allows arbitrary arguments. eg. (multiple-value-call #'= 0 (values)) ; ==> t but (multiple-value-call #'eq nil (values)) won't work since eq requires exactly two arguments.Ay
O
7

Function can return no values by using (values) form.

For example:

(defun foo ()) ;; returns nil
(defun bar () (values)) ;; returns nothing
Oringa answered 15/8, 2014 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.