Page 1 of 1

Identity function

Posted: Thu Mar 20, 2008 8:02 pm
by hsmyers

Code: Select all

(define (return x) x)

Gives me a self-documenting way of avoiding both catch, throw and eval as well. Please criticize?

Posted: Fri Mar 21, 2008 12:20 am
by Jeff
How would that be used for that?

Posted: Fri Mar 21, 2008 2:54 am
by hsmyers
Like this

Code: Select all

(define (parse-pawn-move s n)
  (filter on-board?
    (if (black-to-move? n)
      (let (lst (list (next-diagl s)(next-diagr s)(next-row s)))
        (if (= "6" (row s))
          (append lst (list (next-row (next-row s))))
          (return lst)))
      (let (lst (list (prev-diagl s)(prev-diagr s)(prev-row s)))
        (if (= "4" (row s))
          (append lst (list (prev-row (prev-row s))))
          (return lst))))))

Posted: Fri Mar 21, 2008 11:44 am
by Jeff
This does the same thing:

Code: Select all

(define (parse-pawn-move s n)
  (filter on-board?
    (if (black-to-move? n)
      (let (lst (list (next-diagl s)(next-diagr s)(next-row s)))
        (if (= "6" (row s))
          (append lst (list (next-row (next-row s))))
          lst))
      (let (lst (list (prev-diagl s)(prev-diagr s)(prev-row s)))
        (if (= "4" (row s))
          (append lst (list (prev-row (prev-row s))))
          lst)))))

Posted: Fri Mar 21, 2008 12:00 pm
by hsmyers
Why yes it does. Too much of non-functional approach. It at least worked; but yours requires less typing and one less function call.

Posted: Fri Mar 21, 2008 1:54 pm
by Jeff
Return doesn't make sense in newlisp. The value of a call is the value of the last expression evaluated.

Posted: Fri Mar 21, 2008 4:26 pm
by hsmyers
Hmmm, pretty sweeping statement there. That would imply that the idomatic use of catch and throw is never needed as well. I also remember seeing return in Common Lisp (granted as a macro), so while your statement is true for newLISP at least someone else found the need. I vaguely remember that MuLisp had one but I wouldn't swear to it. It was my first lisp back in the 80's (for an 8-bit machine) and I wrote several math packages for it--- seems like a 'return' would have been handy, but memory fades.
The value of a call is the value of the last expression evaluated.
Speaking of memory, thank you for reminding me of this little detail. And least I be accused of being original, here is a quote from Guy L. Steele Jr.
This function is occasionally useful as an argument to other functions that require functions as arguments (Got that?)
Kinda weak I'll admit--- but hey, what can I say!

Posted: Fri Mar 21, 2008 7:26 pm
by Jeff
That would imply that the idomatic use of catch and throw is never needed as well.
That's not the same thing. If I want to break a loop:

Code: Select all

(set 'foo (catch (dotimes (i 10) (if (= i 5) (throw i)))))
That will give me 5. Catch also let's you work around error-prone routines (although many built-ins return nil and an error function in these cases).