• Re: tail recursion guidelines

    From B. Pym@21:1/5 to Juho Snellman on Thu Sep 12 10:59:41 2024
    XPost: comp.lang.scheme

    Juho Snellman wrote:

    One useful detail that I didn't see mentioned in that chapter is the
    loop keyword "it". I find that it pops up often in code like

    (let ((x ...))
    ...
    (loop for e in list when (foo-bar e x) collect it))

    , where foo-bar returns nil on failure and some sort of state object otherwise.

    Gauche Scheme

    (define (foo-bar x) (and (positive? x) (sqrt x)))

    (filter-map foo-bar '(-3 0 4 9))
    ===>
    (2 3)


    Here's an anaphoric "if" that provides "it".
    (Adapted from an example in the Gauche documentation.)

    (use util.match)

    (define-syntax if@
    (er-macro-transformer
    (lambda (form rename id=?)
    (match form
    [(if@ test expr1 expr2)
    (quasirename rename
    `(let ((,'it ,test))
    (if ,'it ,expr1 ,expr2)))]))))

    (if@ (find odd? '(2 3)) (print it) (print "false is " it))
    3

    (if@ (find odd? '(2 30)) (print it) (print "false is " it))
    false is #f

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)