ketos icon indicating copy to clipboard operation
ketos copied to clipboard

[feature request] function define in a function define

Open Inc0n opened this issue 6 years ago • 1 comments

ketos=> (define (x)
            (define (x-1) "hello")
            (x-1))
Traceback:

  In main, define x

execution error: type error: expected string; found list: (define (x-1) "hello")

Would something like this be planned to be added to ketos? #60 is related in the sense that let-rec, and define in this way would achieve lambda recursion as well.

Inc0n avatar Jun 01 '19 12:06 Inc0n

This is currently supported, provided that you use the do operator as the body of the outer function. This comes with the caveat that, perhaps counter-inuitively, x-1 is not defined as a local binding within x, but rather a global definition that is bound every time x is called.

(define (x)
  (do
    (define (x-1) "hello")
    (x-1)))

(x)   ; Yields "hello"
(x-1) ; Also yields "hello", but only after `x` is called at least once.

To avoid putting the inner function into the global scope, one can use let and lambda instead.

(define (x)
  (let ((x-1 (lambda () "hello")))
    (x-1)))

murarth avatar Jun 01 '19 15:06 murarth