newLISP Fan Club

Forum => newLISP newS => Topic started by: hsmyers on March 20, 2008, 01:02:26 PM

Title: Identity function
Post by: hsmyers on March 20, 2008, 01:02:26 PM
(define (return x) x)

Gives me a self-documenting way of avoiding both catch, throw and eval as well. Please criticize?
Title:
Post by: Jeff on March 20, 2008, 05:20:57 PM
How would that be used for that?
Title:
Post by: hsmyers on March 20, 2008, 07:54:41 PM
Like this
(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))))))
Title:
Post by: Jeff on March 21, 2008, 04:44:20 AM
This does the same thing:


(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)))))
Title:
Post by: hsmyers on March 21, 2008, 05:00:52 AM
Why yes it does. Too much of non-functional approach. It at least worked; but yours requires less typing and one less function call.
Title:
Post by: Jeff on March 21, 2008, 06:54:20 AM
Return doesn't make sense in newlisp.  The value of a call is the value of the last expression evaluated.
Title:
Post by: hsmyers on March 21, 2008, 09:26:04 AM
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.
QuoteThe 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.
QuoteThis 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!
Title:
Post by: Jeff on March 21, 2008, 12:26:08 PM
QuoteThat 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:


(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).