Partial application and closures

前端 未结 5 1142
独厮守ぢ
独厮守ぢ 2021-02-14 06:36

I was asked what\'s the relationship between partial function application and closures. I would say there isn\'t any, unless I\'m missing the point. Let\'s say I\'m writing in p

5条回答
  •  失恋的感觉
    2021-02-14 07:16

    Closures are not a required functionality in a language. I'm experimenting a homemade language, lambdatalk, in which lambdas don't create closures but accept partial application. For instance this is how the set [cons, car, cdr] could be defined in SCHEME:

    (def cons (lambda (x y) (lambda (m) (m x y))))
    (def car (lambda (z) (z (lambda (p q) p))))
    (def cdr (lambda (z) (z (lambda (p q) q))))
    
    (car (cons 12 34)) -> 12
    (cdr (cons 12 34)) -> 34
    

    and in lambdatalk:

    {def cons {lambda {:x :y :m} {:m :x :y}}} 
    {def car {lambda {:z} {:z {lambda {:x :y} :x}}}}
    {def cdr {lambda {:z} {:z {lambda {:x :y} :y}}}}
    
    {car {cons 12 34}} -> 12
    {cdr {cons 12 34}} -> 34
    

    In SCHEME the outer lambda saves x and y in a closure the inner lambda can access given m. In lambdatalk the lambda saves :x and :y and returns a new lambda waiting for :m. So, even if closure (and lexical scope) are usefull functionalities, there are not a necessity. Without any free variables, out of any lexical scope, functions are true black boxes without any side effect, in a total independance, following a true functional paradigm. Don't you think so?

提交回复
热议问题