Which languages support *recursive* function literals / anonymous functions?

后端 未结 16 2137
一整个雨季
一整个雨季 2021-02-04 05:09

It seems quite a few mainstream languages support function literals these days. They are also called anonymous functions, but I don\'t care if they have a name. The important th

相关标签:
16条回答
  • 2021-02-04 06:03

    I think this may not be exactly what you're looking for, but in Lisp 'labels' can be used to dynamically declare functions that can be called recursively.

    (labels ((factorial (x) ;define name and params
        ; body of function addrec
        (if (= x 1)
          (return 1)
          (+ (factorial (- x 1))))) ;should not close out labels
      ;call factorial inside labels function
      (factorial 5)) ;this would return 15 from labels
    
    0 讨论(0)
  • 2021-02-04 06:06

    'Tseems you've got the idea of anonymous functions wrong, it's not just about runtime creation, it's also about scope. Consider this Scheme macro:

    (define-syntax lambdarec
      (syntax-rules ()
        ((lambdarec (tag . params) . body)
         ((lambda ()
           (define (tag . params) . body)
            tag)))))
    

    Such that:

    (lambdarec (f n) (if (<= n 0) 1 (* n (f (- n 1)))))
    

    Evaluates to a true anonymous recursive factorial function that can for instance be used like:

    (let ;no letrec used
        ((factorial (lambdarec (f n) (if (<= n 0) 1 (* n (f (- n 1)))))))
      (factorial 4)) ; ===> 24
    

    However, the true reason that makes it anonymous is that if I do:

    ((lambdarec (f n) (if (<= n 0) 1 (* n (f (- n 1))))) 4)
    

    The function is afterwards cleared from memory and has no scope, thus after this:

    (f 4)
    

    Will either signal an error, or will be bound to whatever f was bound to before.

    In Haskell, an ad hoc way to achieve same would be:

    \n -> let fac x = if x<2 then 1 else fac (x-1) * x
        in fac n
    

    The difference again being that this function has no scope, if I don't use it, with Haskell being Lazy the effect is the same as an empty line of code, it is truly literal as it has the same effect as the C code:

    3;
    

    A literal number. And even if I use it immediately afterwards it will go away. This is what literal functions are about, not creation at runtime per se.

    0 讨论(0)
  • 2021-02-04 06:07

    Delphi includes the anonymous functions with version 2009.

    Example from http://blogs.codegear.com/davidi/2008/07/23/38915/

    type
      // method reference
      TProc = reference to procedure(x: Integer);               
    
    procedure Call(const proc: TProc);
    begin
      proc(42);
    end;
    

    Use:

    var
      proc: TProc;
    begin
      // anonymous method
      proc := procedure(a: Integer)
      begin
        Writeln(a);
      end;               
    
      Call(proc);
      readln
    end.
    
    0 讨论(0)
  • 2021-02-04 06:07

    Anonymous functions exist in C++0x with lambda, and they may be recursive, although I'm not sure about anonymously.

    auto kek = [](){kek();}
    
    0 讨论(0)
提交回复
热议问题