Understanding recursion in the function normalize-definition form the racket sources

38 views
Skip to first unread message

Dan Synek

unread,
Apr 17, 2021, 8:02:00 AM4/17/21
to Racket Users
I am trying to implement a variation of define. In order to do that I am trying to understand the source of the define macro in racket.
 It calls a function normalize-definition which looks like this:
  (define-values (normalize-definition)
    (case-lambda 
     [(stx lambda-stx check-context? allow-key+opt?)
      (let-values ([(id mk-rhs body)
                    (normalize-definition/mk-rhs stx lambda-stx check-context? allow-key+opt? #t)])
        (values id (mk-rhs body)))]
     [(stx lambda-stx check-context?) (normalize-definition stx lambda-stx check-context? #f)]
     [(stx lambda-stx) (normalize-definition stx lambda-stx #t #f)])))

In the two last cases of the case-lambda there is a call to normalize-definition. It is not a recursive call, since define-values cannot be used recursively. I cannot find any other normalize-definition in the modules required by the norm-define module.
 If I try to enter a copy of the definition (renamed to normalize-definition1) I get (as expected) that normalize-definition1 is undefined.
What is going on?
Thanks
Dan

John Clements

unread,
Apr 17, 2021, 5:25:38 PM4/17/21
to Dan Synek, Racket Users
I’m confused by your assertion that define-values can’t be used recursively. Here’s a program that does this:

#lang racket

(define-values (fact)
(λ (x) (if (= x 0) 1 (* x (fact (sub1 x))))))

(fact 14)

Am I misunderstanding your message?

John Clements
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/ce5ce36e-03ee-44b1-a5ae-4f5a6e7b9d1en%40googlegroups.com.

Dan Synek

unread,
Apr 18, 2021, 12:37:31 AM4/18/21
to Racket Users
(blush)
No you are not misunderstanding my question. I was wrong about define-values and confused by an error message related to normalize-definition not being known in another
part of the program due to phase problems. Thank you so much for taking the time to help me out!
Dan

Wayne Harris

unread,
Apr 20, 2021, 8:18:05 AM4/20/21
to racket...@googlegroups.com
Beginner here. I don't see the problem you seem to be referring to
here. In this example below, I recursively define arglen using
define-values.

#lang racket/base

(define-values (arglen)
(case-lambda
[() 'no-list]
[(ls) (if (null? ls)
0
(+ 1 (arglen (cdr ls))))]))

case-lambda-recursion.rkt> (arglen)
'no-list
case-lambda-recursion.rkt> (arglen (list))
0
case-lambda-recursion.rkt> (arglen (list 1))
1
case-lambda-recursion.rkt> (arglen (list 1 1))
2
case-lambda-recursion.rkt> (arglen (list 1 1 1))
3
case-lambda-recursion.rkt>


Reply all
Reply to author
Forward
0 new messages