Why does this code fails to load?

17 views
Skip to first unread message

Boring Coder

unread,
Oct 9, 2025, 2:46:22 PM (21 hours ago) Oct 9
to Shen

(define stringify
    []      -> "()"
    [skip]  -> ")"
    X       -> (str X) where (not (cons? X))
    [skip X | Y] -> (@s  (stringify X) " " (stringify [skip | Y]) )
    [X | Y] -> (@s "("  (stringify  X) " " (stringify [skip | Y]) ))

(defmacro quote
    [test X Y Z] -> [test X Y (stringify Z)])

(define test
    X Y Z -> [X Y Z])


This code fails to load
(0-) (load "simulator.shen")
fn: stringify is undefined

when I define stringify in the repl and then load

Heap exhausted during allocation: 119865344 bytes available, 260046856 requested.
        Immobile Object Counts
 Gen layout fdefn symbol   code  Boxed   Cons    Raw   Code  SmMix  Mixed  LgRaw LgCode  LgMix Waste%       Alloc        Trig   Dirty GCs Mem-age

dr.mt...@gmail.com

unread,
Oct 9, 2025, 2:59:25 PM (21 hours ago) Oct 9
to Shen
(defmacro quote
    [test X Y Z] -> [test X Y (stringify Z)])

goes into an infinite loop?

M.

Boring Coder

unread,
Oct 9, 2025, 4:36:50 PM (19 hours ago) Oct 9
to Shen
In my head it is  supposed to turn the third argument into a string before evaluating it. so    that :
(test (+2 2) (+3 3) (+ 5 5)) will give [4 6 "(+ 5 5)"]

but I think I didn't understand the order of macro-expansion and evaluation  correctly.
I will work on that.

Bruno Deferrari

unread,
Oct 9, 2025, 4:38:23 PM (19 hours ago) Oct 9
to qil...@googlegroups.com
It gives you [test 4 6 "(+ 5 5)"], which again matches the pattern [test X Y Z], which will be macro-expanded again, and again, and again.

--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+un...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/qilang/c4c57551-a7c8-4a51-a79d-aa0e600faca9n%40googlegroups.com.


--
BD

Bruno Deferrari

unread,
Oct 9, 2025, 4:40:08 PM (19 hours ago) Oct 9
to qil...@googlegroups.com
My guess is that you wanted this instead?

(defmacro quote
    [test X Y Z] -> (test X Y (stringify Z)))
--
BD

Boring Coder

unread,
9:02 AM (2 hours ago) 9:02 AM
to Shen
This is what I get with that when I enter everything in the repl :

(0-) (define stringify
    []      -> "()"
    [skip]  -> ")"
    X       -> (str X) where (not (cons? X))
    [skip X | Y] -> (@s  (stringify X) " " (stringify [skip | Y]) )
    [X | Y] -> (@s "("  (stringify  X) " " (stringify [skip | Y]) ))
(fn stringify)

(1-) (define test
    X Y Z -> [X Y Z])
(fn test)

(2-) (defmacro quote
    [test X Y Z] -> (test X Y (stringify Z)))
quote

(3-) (test (+ 3 3) (+ 2 2) (+ 5 5))
The value
  6
is not of type
  (OR FUNCTION SYMBOL)
when binding FUNCTION


And when I load the file it says this :
fn: test is undefined

Bruno Deferrari

unread,
9:16 AM (2 hours ago) 9:16 AM
to qil...@googlegroups.com
Sorry, I misinterpreted what you were trying to do. You can use the original code you had, you just need to make a small change to the macro:

\\ Only expand if Z is not already a string
\\ avoids infinite recursive macro expansion
(defmacro quote
  [test X Y Z] -> [test X Y (stringify Z)]
     where (not (string? Z)))

or

\\ name the function differently, so that the quote macro
\\ doesn't match it again, avoiding infinite recursive expansion.
(define test-internal

  X Y Z -> [X Y Z])

(defmacro quote
  [test X Y Z] -> [test-internal X Y (stringify Z)]

--
BD
Reply all
Reply to author
Forward
0 new messages