I was able to run your code successfully, so I can't help with the specific
error message you're seeing, but there were a number of issues. In
DrRacket, with `(step 500000)`, expansion reaches the memory limit I've set
before it's able to compile the program. I could run it at the command-line
(and presumable DrRacket would, too, if I gave it more RAM), but even there
it took an extraordinarily long time.

There are two other suggestions I wanted to make:

1. Other than as an experiment, there's no reason for this to be a macro
rather than a plain function. Given a far-from-optimized function step*:
(define (step* num-steps)
  (for/sum ([x (in-range num-steps)])
    (expt 2 x)))
DrRacket is happy to compute `(step* 50000)` even with my restrictive
memory limit in place and does so in around 800 ms, whereas running the
macro version at the command-line (which generally has better performance)
took, I think, tens of minutes—long enough that I forgot about checking on
it.

2. I noticed that in your macro implementation you were making heavy use of
`datum->syntax` rather than syntax templates and pattern variables. This
has issues for the correctness of your macro. In particular, a program like:
(let ([+ string-append])
  (step 50))
will raise an exception. This is because, when you write `(datum->syntax
stx `(+ ,@works))`, you are giving `+` the lexical context of the input
syntax, so it will be bound to whatever `+` is bound to where your macro is
used—or perhaps not bound to anything, if there isn't a binding for `+` in
scope there.

Sometimes one wants to do that sort of thing, so Racket gives you the tools
to express it, but most of the time you want the use of `+` in a macro to
mean what `+` means where the macro is defined. You can achieve that result
with low-level operations like `datum->syntax`, but doing it manually is
difficult and error-prone, so Racket provides enormously sophisticated
tools to make doing the write thing easy and natural. Even without changing
the style of your macro at all, simply using `quasisyntax` and `unsyntax`
(spelled "#`" and "#,") instead of `quasiquote` and `unquote` will fix your
problem:

#lang racket/base

(require (for-syntax racket/base))

(define-syntax (hygienic-step stx)
  (define numList (build-list (cadr (syntax->datum stx)) values))
  (define works (map (lambda (v) #`(expt 2 #,v)) numList))
  #`(+ #,@works))

(let ([+ string-append])
  (hygienic-step 50))





-Philip

On Mon, May 28, 2018 at 8:57 AM, <pavelrodzev...@gmail.com> wrote:

> Hello, my name is Pavel and I'm new to Racket :)
>
> I'm playing with macros right now and get a strange error: "unmap failed".
>
> What I'm trying to achieve in this program: expand (step x) into (+ (expt
> 2 0) (expt 2 1) ...)
>
> For small numbers everything is OK, but I get an error for (step 1000000)
>
> What does this error mean and how can I fix it?
>
> Code is in attachements.
>
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to