"Nameless" recursion in other programming languages?

A somewhat obscure feature in SC is that a function can refer to itself without a name binding in an outer frame, e.g. not just

f = { |n| if(n < 1) { 1 } { n * f.(n - 1) } } // factorial CS101 impl
f.(5)

but also

{ |n| if(n < 1) { 1 } { n * thisFunction.(n - 1) } }.(5)

(Can’t do mutual-recursion like that, I think, though.)

So, I wonder if this feature–of an anonymous function being able to refer to itself by a predefined (keyword) name–exists in other (more popular) languages? (I suspect it might exist in Smalltalk.)

I see there’s a proposal to allow lambdas to have a name for themselves in C++.


In languages like Scheme that lack that kind of keyword, one can use a self-application wrapper.

(((lambda (x) (x x))
  (lambda (fact-gen)
    (lambda (n)
      (if (zero? n)
          1
          (* n ((fact-gen fact-gen) (sub1 n)))))))
 5)

Translated into SC:

{|x| x.(x)}.({|fg| {|n| if(n < 1) {1} {n * fg.(fg).(n-1)}}}).(5)

:grin:

2 Likes

Don’t think it’s a “more popular” language(!), but I happened to be reading about Gravity (embedded language) today and noticed this functionality for functions and closures, via a _func keyword referring to the current function, like SC’s thisFunction.

1 Like