Identity function

Started by hsmyers, March 20, 2008, 01:02:26 PM

Previous topic - Next topic

hsmyers

(define (return x) x)

Gives me a self-documenting way of avoiding both catch, throw and eval as well. Please criticize?
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

Jeff

#1
How would that be used for that?
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

hsmyers

#2
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))))))
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

Jeff

#3
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)))))
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

hsmyers

#4
Why yes it does. Too much of non-functional approach. It at least worked; but yours requires less typing and one less function call.
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

Jeff

#5
Return doesn't make sense in newlisp.  The value of a call is the value of the last expression evaluated.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

hsmyers

#6
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!
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

Jeff

#7
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).
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code