Identity function

Notices and updates
Locked
hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Identity function

Post 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?
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

How would that be used for that?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post 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))))))
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post 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)))))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post 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.
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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

Artful code

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post 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!
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post 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).
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked