Arc Forumnew | comments | leaders | submitlogin
having a parenthesis in a list?
1 point by globalrev 6034 days ago | 6 comments
im playing around with a small rather pointless function. insetad of:

  (defop welc req
       (w/link (w/link (w/link (w/link (pr "!")
       	       (pr "Sweden"))
	       (pr "to"))
	       (pr "cooommee))
	(pr "Weeelll")))  
i want to write

  (defop welc req
      (xw "Weeell" "ccoommee" "too" "Sweden" "!"))

  (def xw(n)
       (let l '()
       	  (for x 1 n
     	       (= l (join l '(w/link))))pr l)) 
xw produces a list (w/link w/link w/link) if i enter 3 as nbr. i want a list ((w/link (w/link (w/link)) but how can i have a ( in a list? i should use something else than a list? liek a string? but it should be transformed to code at somepoint... and obv i will add to xw since at the moment it wont do what welc expects it to.


2 points by almkglor 6034 days ago | link

I think you want to learn about macros.

-----

1 point by schtog 6034 days ago | link

i dont want to display really the display is there so i can see what is happening. i am actually writing a macro yes but just testing now to figure out how stuff works.

-----

1 point by cooldude127 6034 days ago | link

the parenthesis means you have a list nested in a list.

-----

1 point by globalrev 6034 days ago | link

yes and i dont want to, is there a way to get around this.

(car '(( 1 2)) and have ( returned. u cant do some trick here?

(car'("(" + 1)) returns "(", how do i get rid of the ""?

-----

4 points by kens 6034 days ago | link

If you want to display a string without the quotes, use "disp" instead of "write".

If you want to convert a string into an S-expr, use "read", and then if you want to evaluate that expression, use "eval".

  (eval (read "(cons (cons 1 2) 3)"))
But I'm pretty sure you don't want to do either of those in this case. If what you want is nested lists, generate the nested lists - Lisp is good at that. Don't try to piece together your code out of parentheses. The following code will more-or-less generate the nested lists you asked about:

  (def xw (n)
    (let l '()
      (for x 1 n
        (= l (join 'w/link (list l))))
      l))
However, the other problem with your approach is that even if you get xv working, it will pass a quoted expression to defop instead of an expression, and defop won't work right. To generate code, you need a macro, not a function. Here's a macro that will solve your original problem:

  (mac xw args
    (if (is (len args) 1)
          `(pr ,@args)
          `(w/link (xw ,@(cdr args)) (pr ,(car args)))))

  (defop wc req (xw "Wel" "come" "to" "Sweden" "!"))

-----

2 points by conanite 6034 days ago | link

are you looking for something like

  (prn (list #\( 'foo #\)))

?

-----