Author Topic: REPEAT problem  (Read 1895 times)

0 Members and 1 Guest are viewing this topic.

Jeremy Dunn

  • Newt
  • Posts: 31
REPEAT problem
« on: May 17, 2018, 03:13:17 PM »
Here is an interesting question that I have yet to solve. I want to write REPEAT in such a way that it will take its arguments as a list. For instance

(defun MYREPEAT ( lst )
  (apply 'repeat lst)
)

Nope

(defun MYREPEAT ( lst )
  (apply 'repeat (apply 'progn lst))
)

Nope

I've tried all kinds of approaches but haven't created anything that will do more than one cycle. Any gurus out there to enlighten?


roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: REPEAT problem
« Reply #1 on: May 17, 2018, 04:25:57 PM »
Can't think of a good reason for this. But anyway:
Code - Auto/Visual Lisp: [Select]
  1. ; (myrepeat '(10 (if j (setq j (1+ j)) (setq j 1))))
  2. (defun myrepeat (lst)
  3.   (repeat (car lst) (eval (cadr lst)))
  4. )

ChrisCarlson

  • Guest
Re: REPEAT problem
« Reply #2 on: May 17, 2018, 04:52:23 PM »
What would your list look like that it would be able to limit the repeat function?

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: REPEAT problem
« Reply #3 on: May 17, 2018, 05:28:14 PM »
One way:

Code: [Select]
(defun MyRepeat ( L )
  (foreach x L
    (eval (cons 'repeat x))
  )
)

Code: [Select]
_$ (MyRepeat
  '(
    (2 (print "A"))
    (1 (print "B"))
    (3 (print "C"))
  )
)

"A"
"A"
"B"
"C"
"C"
"C" "C"


(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Jeremy Dunn

  • Newt
  • Posts: 31
Re: REPEAT problem
« Reply #4 on: May 18, 2018, 10:28:51 AM »
Grrrr, your way works but I had to modify it to

Code: [Select]
(defun MyRepeat ( L )
  (foreach x (list L)
    (eval (cons 'repeat x))
  )
)


I just wanted a single list of arguments to process, not several. It is a little mysterious to me as to why this works and why I have to put a list within a list. Ahh the subtleties of LISP.

Lee Mac

  • Seagull
  • Posts: 12917
  • London, England
Re: REPEAT problem
« Reply #5 on: May 18, 2018, 12:24:15 PM »
Why not reduce it to -

Code - Auto/Visual Lisp: [Select]
  1. (defun _repeat ( l ) (eval (cons 'repeat l)))
Code - Auto/Visual Lisp: [Select]
  1. _$ (progn (_repeat '(3 (print "A"))) (princ))
  2.  
  3. "A"
  4. "A"
  5. "A"

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: REPEAT problem
« Reply #6 on: May 18, 2018, 02:25:09 PM »
Here is an interesting question that I have yet to solve. I want to write REPEAT in such a way that it will take its arguments as a list. For instance

(defun MYREPEAT ( lst )
  (apply 'repeat lst)
)

Nope

(defun MYREPEAT ( lst )
  (apply 'repeat (apply 'progn lst))
)

Nope

I've tried all kinds of approaches but haven't created anything that will do more than one cycle. Any gurus out there to enlighten?

Jeremy,
You did not provide an essential part of your question.
What will be the contents of your list that you propose passing to the new function. ?
Please provide an example.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Jeremy Dunn

  • Newt
  • Posts: 31
Re: REPEAT problem
« Reply #7 on: May 18, 2018, 03:22:21 PM »
Kdub, the contents of the list are simply the lines of code that you would normally put in a REPEAT statement except that they are all in a list. For example,

(repeat 5
   (setq cnt (+ cnt 2))
)


would be


(MyRepeat (list 5 (setq cnt (+ cnt 2))))


Lee, I tried yours and it worked fine so long as I quoted the list but if I used (list instead it failed, why?

Lee Mac

  • Seagull
  • Posts: 12917
  • London, England
Re: REPEAT problem
« Reply #8 on: May 18, 2018, 06:38:05 PM »
Lee, I tried yours and it worked fine so long as I quoted the list but if I used (list instead it failed, why?

The list function evaluates the arguments it is supplied before returning a list - you may wish to refer to my tutorial here.