In TXR Lisp, I called the function retf
. It produces a function which just returns the specified value. Higher order code reads well with that name.
I also use the f
suffix in some place to indicate a functional result; for instance iff
is like if
, but functional.
Here is an example with iff
and retf
:
1> (mapcar [iff oddp (retf 1) (retf 0)] 0..10)
(0 1 0 1 0 1 0 1 0 1)
Map the list of integers 0 to 9 through a function which returns 1 for odd integers, otherwise 0.
iff
is a function which has functional arguments. oddp
is a built-in function and (ret 1)
and (ret 0)
return functions. iff
passes its arguments to oddp
. If that function yields true, then it calls (retf 1)
, otherwise it calls (retf 2)
.
So it's like the (if cond then else)
operator except that rather than evaluating expressions, it calls functions [iff cond-function then-function else-function]
.
In this language, there is a ret
macro. Wnen you evaluate (retf expr)
, expr
is eagerly evaluated to an argument value, and retf
is called, return a function which returns that value. By contrast, when (ret expr)
is evaluated, it returns a function which, when called, will evaluate expr
at that time (and every time it is called) and return the value.
1> (let ((i 0)) (ret (inc i)))
#<interpreted fun: lambda #:arg-rest-0015>
2> [*1]
1
3> [*1]
2
4> [*1]
3
5> (let ((i 0)) (retf (inc i)))
#<intrinsic fun: 0 param + variadic>
6> [*5]
1
7> [*5]
1
8> [*5]
1
Here, the function produced by ret
is capable of incrementing i
each time it is called and returning the incremented value, whereas the retf
function has just captured the value 1 and returns that.
If you don't have these kinds of evaluation situations to worry about, using the name ret
for the function-returning function might make sense.
makeFoo
function in an arbitrary situation depends entirely on what the purpose of the function is in that specific situation; there is no generic, correct answer to your question.fooFor(.....)