Question

In Arc there's a macro called in

> (let x 1
    (in x 4 5 6))
nil
> (let x 1
    (in x 1 5 6))
t

that checks if its first parameter is equal to any of the rest. I want a version of this that takes a parameter plus a list (semantically identical to Python's in), so I wrote:

(assign weak-tens* '(11 12))

(mac in? (elt lst)
  (cons 'in (cons elt lst)))

(def transform-deck (deck)
     (map [if (in? _ weak-tens*) #\T _] deck))

output:

arc> (load "main.arc")
*** redefining in?
map: contract violation
  expected: list?
  given: '(_ . weak-tens*)
  argument position: 2nd
  other arguments...:
   #<procedure:ac-niltree>

No correct solution

OTHER TIPS

To answer your immediate question, you can use mem, as follows:

arc> (let x 3
    (mem x '(1 2 3 4)))
(3 4)

Also, there's no reason this should be a macro. It doesn't do anything that requires a macro.

But let's look at why the macro doesn't work:

arc> (macex1 '(in? 1 '(1 2 3)))
(in 1 quote (1 2 3))

Ah, we're putting the value "quote".

Here's how we want the code to expand:

(in? 1 '(1 2 3))

should expand to:

(in 1 1 2 3)

But as mentioned, we don't even want this to be a macro in the first place. Ignoring mem, it could be written as follows:

(def in? (elt lst)
     (if (no lst)
         nil
       (is elt ;;if we've found the element
           (car lst))
       t
       (in? elt (cdr lst)))) ;;otherwise recurse
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top