Jump to content

Recommended Posts

Posted

It worked!!!! :shock:

Just had to set L to nil before the repeat cycle. Don't know why, but now it works. I didn't change anything else...

Thanks, guys!

  • Replies 34
  • Created
  • Last Reply

Top Posters In This Topic

  • Se7en

    10

  • Lee Mac

    9

  • alanjt

    5

  • firavolla

    4

Top Posters In This Topic

Posted

'(h) a quoted list will not evaluate what's inside. You must use (list h)

Posted

> Secondly, while is indeed a conditional loop.

 

The problem i have with that label is that its so generic...you might as well just say that its a function. Yes, it is a loop based upon a condition; I know what it does. Im speaking and thinking more academically.

Posted
Im speaking and thinking more academically.

 

You've obviously got some knowledge beyond us mere mortals - what would you call it?

Posted
You've obviously got some knowledge beyond us mere mortals - what would you call it?

what? no. what makes you say that?

 

If i knew what it was called i would call it that now wouldn't i? :P I want to try and to spend more time re-creating it. I was thinking about it last night on my way home and the only way i could re-create WHILE was with a simple linear recursive process (very simplistic). So ...I'm stumped (but them I am just a hack I want to talk to someone with a CS degree about it).

Posted

The way I see it, is as just a repeated IF statement (minus the 'else' expression I suppose).

Posted

yeah (well the else is the case but you get the idea). I imagined it a lot like MAPCAR

-i.e.

(defun map (proc items)
(if (null items)
   nil
   (cons (proc (car items))
         (map proc (cdr items)))))

but passing an exit condition as well as checking for the end of the list.

 

...that cant be it though. It has to be different; WHILE is fast! MAPCAR is fast, but not that fast.

 

*humm*

Posted

I'm not sure MAPCAR is on the right lines, there is no test expression to terminate the recursion, all the 'items' will be processed.

Posted

Thats why i said: "... but passing an exit condition as well".

Posted

I'm no CS major and I know as much about recursive loops as I do about string theory, but...

 

Would this be a while loop represented as a recursive function?

(defun whilerec(tst proc / )
 (eval proc)
 (if (eval tst) (whilerec tst proc))
 )

(defun c:tst( / )
 (setq a 1)
 (whilerec '(/= a 10) '(progn (princ (strcat (itoa a) "\n")) (setq a (1+ a))))
 )

Posted

That very well could.

 

> ...I know as much about recursive loops as I do about string theory...

 

A procedure can be recursive but the process a procedure uses can be recursive as well so you can have several different `recursive loop' scenario's: a recursive procedure using a recursive process, a recursive procedure using an iterative process.

Have i posted my substitution model doc here?

Posted

I don't know if you have or haven't. I just googled "recursion" and got a stack error.

Posted

Yay for more of my useless drivel! Three loops re-written recursively!

 

(defun whilerec(tst proc / ) (if (eval tst) (progn (eval proc) (whilerec tst proc))))
(defun repeatrec(tst proc / ) (if (> tst 0) (progn (eval proc) (repeatrec (1- tst) proc))))
(defun foreachrec(v lst proc / ) (if (set v (car lst)) (progn (eval proc) (foreachrec v (cdr lst) proc))))
(defun mapcarrec(fun lst / x) (if (setq x (car lst)) (cons (apply fun (list x)) (mapcarrec fun (cdr lst)))))

(defun c:tstforc( / )
 ;(foreach fv (list 1 2 3 4 5) (princ fv))
 (foreachrec 'fv (list 1 2 3 4 5) '(princ fv))
 (princ)
 )

(defun c:tstrepc( / a)
 (setq a 0)
 ;(repeatrec 5 '(princ (setq a (1+ a))))
 (repeat 5 (princ (setq a (1+ a))))
 (princ (list "*" a "*"))
 (princ)
 )

(defun c:tstwhilec( / b)
 (setq b 1)
 (whilerec '(/= b 10) '(progn (princ (itoa b)) (setq b (1+ b))))
 ;(while (/= b 10) (princ (itoa b)) (setq b (1+ b)))
 (princ (list "*" b "*"))
 (princ)
 )

(defun c:tstmapc( / )
 (princ (mapcar '(lambda (x) (1+ x)) '(0 1 2 3 4)))
 ;(princ (mapcarrec '(lambda (x) (1+ x)) '(0 1 2 3 4)))
 (princ)
 )

 

EDIT: And one more for mapcar.

Posted

How about a recursive conditional call:unsure:

 

[b][color=BLACK]([/color][/b]defun nw_while [b][color=FUCHSIA]([/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]cond [b][color=NAVY]([/color][/b][b][color=MAROON]([/color][/b]setq input [b][color=GREEN]([/color][/b]getint [color=#2f4f4f]"\nNumber To Add:   "[/color][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]setq tmp [b][color=GREEN]([/color][/b]cons input tmp[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
        [b][color=MAROON]([/color][/b]nw_while[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 tmp[b][color=BLACK])[/color][/b]

[b][color=BLACK]([/color][/b]setq tmp nil[b][color=BLACK])[/color][/b]
[b][color=BLACK]([/color][/b]prin1 [b][color=FUCHSIA]([/color][/b]nw_while[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]


 

I always like the way a recursive call looks when you (trace) the call

 


  Entering NW_WHILE:

Number To Add:   5
    Entering NW_WHILE:

Number To Add:   6
      Entering NW_WHILE:

Number To Add:   7
        Entering NW_WHILE:

Number To Add:   8
          Entering NW_WHILE:

Number To Add:
          Result: (8 7 6 5)
        Result: (8 7 6 5)
      Result: (8 7 6 5)
    Result: (8 7 6 5)
  Result: (8 7 6 5)
(8 7 6 5)


 

-David

Posted

I suppose it could be slightly shortened :)

 

(defun nw_while ( / input )
 (cond ((setq input (getint "\nNumber To Add:   "))
        (cons input (nw_while)))
 )
)

(setq tmp nil)
(prin1 (nw_while))

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...