0

How can I redefine the procedure and only when it is called as an argument to the procedure fetch?

For example:

; this `and` returns #f
(and #t #f)

; this `and` returns "and a b" 
(fetch (foo (bar (and "a" "b"))))

I would like to write a macro to do this, but I can't work out how to write a pattern that matches and anywhere in an arbitrary tree of arguments passed to fetch.

I am using Chicken and am happy to use as much of R7RS as Chicken supports.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
nickloewen
  • 15
  • 4
  • 1
    You should provide more information about the Scheme version (e.g. R5RS, R6RS, R7RS) and implementation (e.g. MIT, Guile, Racket) you are using. Scheme is less a language and more a family of languages; programs that do useful things are rarely portable. – Alexis King Sep 01 '18 at 00:45
  • Good point, I have updated the question. – nickloewen Sep 01 '18 at 04:50

1 Answers1

1

One nitpick: and is not a procedure, it is syntax (think about it: evaluation is stopped as soon as the first #f is encountered).

But regardless of that, I don't think what you're trying to do is possible by overriding and. You'll need to convert fetch to be a macro. Instead of trying to scan the input and replacing and, I'd use an unhygienic let to override the meaning of and locally. A bit like this:

(define my-local-and ...)
(define the-real-fetch ...)

(define-syntax fetch
  (ir-macro-transformer
    (lambda (e i c)
      `(let ((,(i 'and) my-local-and))
         (the-real-fetch ,@(cdr e))))))

I'd really argue against this, though, because this will really mess with the user's expectations of what is happening. Perhaps you can explain a bit more about why you want to do this?

sjamaan
  • 2,282
  • 10
  • 19
  • `ir-macro-transformer` is what I was looking for, thanks! I understand that this is a bad design; I just wanted to know how to do it for the sake of the knowledge itself. Indeed, I think my whole project (a little interface to manage SQL databases with a certain schema) is a Bad Idea, just one that I hope to learn from. I have a number of other procedures, eg, `has-attribute`, which will be assembled by `fetch` into a full query. So `and` will at least still be performing a logical AND, but in SQL rather than Scheme. – nickloewen Sep 01 '18 at 18:44
  • Sounds like you're having a lot of fun experimenting and learning. Excellent! If you have more questions, also feel free to join `#chicken` on freenode IRC or check out the mailing lists. You're more likely to get your questions answered there. – sjamaan Sep 03 '18 at 08:26