curry.scm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;; Idea:
  2. ;; Write (thunk (+ a b)) instead of (lambda () (+ a b)).
  3. (import
  4. (except (rnrs base) let-values)
  5. (only (guile)
  6. lambda* λ))
  7. ;; (define-curried blub
  8. ;; (lambda (arg1 arg2 arg3)
  9. ;; expr1
  10. ;; expr2))
  11. ;; -->
  12. ;; (define blub
  13. ;; (lambda (arg1)
  14. ;; (lambda (arg2)
  15. ;; (lambda (arg3)
  16. ;; expr1
  17. ;; expr2))))
  18. (define-syntax curried-lambda
  19. (syntax-rules ()
  20. [(_ (arg args* ...) expr* ...)
  21. ;; Take one argument and wrap in a lambda.
  22. (lambda (arg)
  23. ;; Do the macro for the other arguments.
  24. (curried-lambda (args* ...) expr* ...))]
  25. ;; If there are no more arguments, output the
  26. ;; expressions of the body in the previous inner-most
  27. ;; lambda.
  28. [(_ () expr* ...)
  29. ((lambda () expr* ...))]))
  30. (define-syntax define-curried
  31. (syntax-rules ()
  32. ;; Case of 1 or more arguments.
  33. [(_ function-name
  34. (lambda (arg args* ...)
  35. expr* ...))
  36. (define function-name
  37. (lambda (arg)
  38. ;; Create nested lambdas.
  39. (curried-lambda (args* ...) expr* ...)))]
  40. ;; Case of no function arguments.
  41. [(_ function-name (lambda () expr* ...))
  42. (define function-name
  43. (lambda ()
  44. expr* ...))]))
  45. ;; But for convenient calling of the function, one might
  46. ;; need a syntax as well.
  47. ;; (curry-call (foobar arg1 arg2 arg3))
  48. ;; OR perhaps:
  49. ;; (curry-call foobar arg1 arg2 arg3)
  50. (define-syntax curry-call
  51. (syntax-rules ()
  52. [(_ function-name ???)
  53. #|TODO|#]))