Live data from Hacker News

Anonymous recursive functions in Racket

github.com

41–50 of 65 posts

Re: Anonymous recursive functions in Racket

#41
post #18

Tangential, but I've been wanting to dive back into FP for quite sometime; for context I used Haskell at a payments corp ~10 years back, working mostly with Typescript, Zig and Nim for the past couple of years, realizing I am basically trying to do FP in most of these languages. Is Racket a good language to pick up and re-learn my concepts + implement some tools? Or are there some other languages that would be better…

You could try Purescript, with the book¹ written by Charles Scalfani.

It focuses exclusively on FP and does not deviate from it.

¹ https://leanpub.com/fp-made-easier/

Re: Anonymous recursive functions in Racket

#42
Probably worth noting that there's already an SRFI for this. [0] And that macro will work on any Scheme implementing the standard since about '98.

    (define-syntax rec
      (syntax-rules ()
        ((rec (NAME . VARIABLES) . BODY)
         (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME))
        ((rec NAME EXPRESSION)
         (letrec ( (NAME EXPRESSION) ) NAME))))
[0] https://srfi.schemers.org/srfi-31/srfi-31.html

Re: Anonymous recursive functions in Racket

#43
post #31

Earlier quoted context omitted.

Wow — scheme-rs is such a neat project! Hadn't heard of it before!

Thanks! I haven’t really publicized it, my goal is to get it finished first, but I will be presenting on it at the scheme workshop at ICFP/SPLASH

Neat! Will see if I can make it (though I'll probably have to be dealing with OOPSLA stuff at the same time )-:).

Re: Anonymous recursive functions in Racket

#44
post #42

Probably worth noting that there's already an SRFI for this. [0] And that macro will work on any Scheme implementing the standard since about '98. (define-syntax rec (syntax-rules () ((rec (NAME . VARIABLES) . BODY) (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME)) ((rec NAME EXPRESSION) (letrec ( (NAME EXPRESSION) ) NAME)))) [0] https://srfi.schemers.org/srfi-31/srfi-31.html

1. This isn't the same. `rec` names the function. This does not name the function. The point is to illustrate how the name-capture works.

2. The README literally says "Don't Use This Macro!" and references `rec` to use instead:

https://github.com/shriram/anonymous-recursive-function?tab=...

Re: Anonymous recursive functions in Racket

#45
post #26

can we implement this in Python?

The Y combinator in Python: https://eli.thegreenplace.net/2016/some-notes-on-the-y-combi... (scroll down, after the concept is explained using Clojure) A bit crazier, in Go with generics: https://eli.thegreenplace.net/2022/the-y-combinator-in-go-wi...

This isn't the Y-combinator.

Re: Anonymous recursive functions in Racket

#46
post #33

Earlier quoted context omitted.

This isn't meant to be a good programming mechanism, it's meant to be an illustration of how to use the macro system. But also, if you're processing non-linear data, you're going to want to do with a recursive function anyway. E.g., when dealing with a tree. Code below; can't seem to get multi-line code-formatting so it looks hideous: #lang racket (require "anon-rec.rkt") (require rackunit) (struct mt ()) (struct nod…

For formatting code blocks on HN, prefixing each line with 4+ leading spaces works: (define sum-tree (lam/anon (t) (cond ((mt? t) 0) ((node? t) (+ (node-v t) ($MyInvocation (node-l t)) ($MyInvocation (node-r t)))))))

Aaah, thanks Neil!

Re: Anonymous recursive functions in Racket

#47

All the languages I like have niche ecosystems which have a lot of drawbacks

The correlation is likely causal in both directions.

They're niche because they're doing weird, interesting things. Like creating their own VMs to support funky features. So nobody wants to depend on them: low bus-factor.

They can do weird, interesting things because they don't have a large user-base that will yell at them about how they're breaking prod.

Re: Anonymous recursive functions in Racket

#48
post #28

The Racket Discourse thread on this: https://racket.discourse.group/t/illustrate-anonymous-recurs... (Just me suggesting other alternatives right now)

Thanks for the suggestion to replace the reference to MzLib with SRFI-31. I've done that now.

https://github.com/shriram/anonymous-recursive-function/comm...

Re: Anonymous recursive functions in Racket

#49

Earlier quoted context omitted.

Do the clojure folks still insist this is a feature, as opposed to an incomplete compiler leaking limitations into their world?

Without the explicit recur it's far too easy to misidentify a tail call and use recursion where it's not safe. Recur has zero inconvenience. It's four letters, it verifies that you are in a tail position, and it's portable if you take code to a new function or rename a function. What's not to love?

That doesn't work for mutual recursion, what is quite common in Scheme programs. Besides, tail call optimization is not only useful in recursion.
Post reply on HN