Is it possible to implement auto-currying to the Lisp-family languages?

前端 未结 6 2010
夕颜
夕颜 2020-12-31 06:15

That is, when you call a function with >1 arity with only one argument, it should, instead of displaying an error, curry that argument and return the resulting function with

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 06:37

    Sure, you just have to decide exact semantics for your language, and then implement your own loader which will translate your source files into the implementation language.

    You could e.g. translate every user function call (f a b c ... z) into (...(((f a) b) c)... z), and every (define (f a b c ... z) ...) to (define f (lambda(a) (lambda(b) (lambda(c) (... (lambda(z) ...) ...))))) on top of a Scheme, to have an auto-currying Scheme (that would forbid varargs functions of course).

    You will also need to define your own primitives, turning the varargs functions like e.g. (+) to binary, and turning their applications to using fold e.g. (+ 1 2 3 4) ==> (fold (+) (list 1 2 3 4) 0) or something - or perhaps just making such calls as (+ 1 2 3 4) illegal in your new language, expecting of its user to write fold forms by themselves.

    That's what I meant by "deciding ... semantics for your language".

    The loader can be as simple as wrapping the file contents into a call to a macro - which you would then have to implement, as per your question.

提交回复
热议问题