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

后端 未结 16 2108
一整个雨季
一整个雨季 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 05:55

    Because I was curious, I actually tried to come up with a way to do this in MATLAB. It can be done, but it looks a little Rube-Goldberg-esque:

    >> fact = @(val,branchFcns) val*branchFcns{(val <= 1)+1}(val-1,branchFcns);
    >> returnOne = @(val,branchFcns) 1;
    >> branchFcns = {fact returnOne};
    >> fact(4,branchFcns)
    
    ans =
    
        24
    
    >> fact(5,branchFcns)
    
    ans =
    
       120
    

提交回复
热议问题