Author Topic: More subroutine functions  (Read 9752 times)

0 Members and 1 Guest are viewing this topic.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #15 on: March 20, 2009, 09:41:51 AM »

> don't be so rigid with the language that creative applications of the language are rejected.

CAB, thats the obstacle im trying to overcome. [my] solution isnt creative, its simple logic. I would consider OR the creative use; in fact why not use IF (why use OR at all). (if (null (set var ... (set var)...?

> As long as the intent of the code is not obfuscated to the average programmer I don't see the harm.

So you DONT see "(or (set var) (set var)" as obscured instead of "(set var (condition)" That Boolean statement is masking the underlying objective: bind a variable to one of two conditions which is true.

Maybe im reading or thinking about this too much academically. When I was documenting [my] use of cond for my notes, I was following a few links and discovered this is a bona fide issue touched on by people WAY smarter then any of us. I learned that Paul Graham (I didnt know who he was before I read his Bio -- he's fricken uber smart) described this issue in a book called On-Lisp and he called it the "anaphoric-if"

,----[ anaphoric-if ]-
|
| Var is bound to the value of expr in iftrue if the value of expr satisfies the
| conditional; otherwise iffalse is evaluated.
|
|  (aif var expr
|       iftrue
|       iffalse)
|
`----

This quote was in one web page that tried to discuss his theory and I thought it was funny, and would bring some joviality to this discussion so here it is.

,----[ Quote ]-
|
|     '"...and even Stigand, the patriotic archbishop of Canterbury, found it advisable--"'
|
|     'Found what?' said the Duck.
|
|     'Found it,' the Mouse replied rather crossly: 'of course you know what "it" means.'
|
|     'I know what "it" means well enough, when I find a thing,' said the Duck: 'it's generally a frog, or a worm. The question is, what did the archbishop find?'
|
|     [Lewis Carroll, Alice's Adventures in Wonderland, Chapter III]
|
`----
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More subroutine functions
« Reply #16 on: March 20, 2009, 10:10:21 AM »
MP, is (if numberp default) checking to make sure that the default variable a number?  very "pure," but possibly superfluous for a hard-coded value?

Agreed. When I penned it I was thinking it would be convenient for the caller to pass nil if they didn't have a default value in mind. The upshot being that an integer (0) would still be returned. Perhaps over thought.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: More subroutine functions
« Reply #17 on: March 20, 2009, 10:16:54 AM »
John,
As I said I prefer  "(set var (condition)" over "(or (set var) (set var)"
but i have no problem with "(or var (setq var value))" when testing for nil.

I don't care for
Code: [Select]
(setq var (cond ((null var) value)))I would use
Code: [Select]
(setq var (if (not var) value))but I prefer
Code: [Select]
(or var (setq var value))
If testing for other than nil I usually use
Code: [Select]
(if (= var constant)
  (setq var something_else)
)

That's all I have time for this morning.
See Ya.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More subroutine functions
« Reply #18 on: March 20, 2009, 10:27:30 AM »
So you DONT see "(or (set var) (set var)" as obscured instead of "(set var (condition)" That Boolean statement is masking the underlying objective: bind a variable to one of two conditions which is true.

It does mask the intent, and I too am not particularly fond of it, but to each his own. Having said that, it's a challenge to post one's opinions without sounding, ummm, curt or abrasive, especially when one is passionate about programming like many of us here are. I know, because [in red faced hindsight] I've been an opinionated ass much of my online life because I've felt strong about issues but not similarly blessed with the gift of eloquently airing said opinion without pissing off / offending others or sounding arrogant, and for that I apologize and am in the debt of those that have been forgiving. To summarize, maybe take a little time to take the edges off so that one's thoughts, rather than one's attitudes are what's left in the mind of the reader.

Fully acknowledge pot calling out kettle, it's part of my therapy: can't fix what you don't acknowledge (honestly, I have been trying to mend my ways).

:)
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #19 on: March 20, 2009, 11:15:45 AM »
So that quote didn't work? I thought it was hilarious myself. *

Okay. But i mean only to have discussions where `feelings' are not involved I harbor ZERO bad or ill wishes towards anyone in the coding forums. I guess i don't understand or just plain forget the whole `forum face' complex; i understand the basic psychology behind it -i.e. `big man on campus' kinda thing but i do not share that problem. I do not go to my RL best friend and tell him that i `smoked' or `got smoked' by my "online friend"; I'm almost positive that if i did he would try and punch me in the skull.  *

I will try to not post.

I am aware that i can sound gruff, but i try and warn people, and I am trying too. Ive tried several methods:
Respond with questions (this just annoying in RL so i stopped),
Responded with only code (all i got was blank stares),
...
I feel like a fricken' robot. `DANGER Will Robinson, DANGER!'  *

FWIW, here is my interpretation of a `default' get* expression for anyone who wants it.   **
Code: [Select]
(defun aif ( var expr iffalse )
  ;; anaphoric-if
  ;;
  ;; ported to AutoLisp from ?comonLisp?
  ;; I dont know the whole extents of the CL`aif' function, im only
  ;; working from one small definition.
  ;;
  ;; EX:
  ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
  ;;
  (set var (cond (expr) (iffalse))) )


Sorry,
7


*  Attempt: humor.
**   Attempt: `olive branch'.  Bad but attempted non the less.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More subroutine functions
« Reply #20 on: March 20, 2009, 11:23:52 AM »
I will try to not post.

WTF? Why?

I am aware that i can sound gruff, but i try and warn people, and I am trying too.

birds of a feather ...

Ive tried several methods: Respond with questions (this just annoying in RL so i stopped),
Responded with only code (all i got was blank stares)

What is RL?

Code: [Select]
(defun aif ( var expr iffalse )
  ;; anaphoric-if
  ;;
  ;; ported to AutoLisp from ?comonLisp?
  ;; I dont know the whole extents of the CL`aif' function, im only
  ;; working from one small definition.
  ;;
  ;; EX:
  ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
  ;;
  (set var (cond (expr) (iffalse))) )

Interesting, but more cryptic than the or technique which spawned this branch of the discussion -- IMO. :)
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #21 on: March 20, 2009, 12:04:50 PM »
Sorry should have expanded that out more. try not to post for: awhile, right away, when i feel..., when its raining. At the least I will go grab a cup 'o java first.

Real life. I thought i would spew I.R.C. Terms today. It's on my Outlook public calendar, didn't you know? *

Yeah, i feel the same. I'm not going to use it. I imagine the life of this thread will determine the existence of that abstraction.



*  Attempt: humor.


EDIT: Added `key'.
*Se7en: slaps himself with a Trout.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2275
  • Marseille, France
Re: More subroutine functions
« Reply #22 on: March 20, 2009, 12:13:46 PM »
Still about OR (and AND).

In its AutoLISP FAQ (subject 7), Reni Urban said :

"The following are not real bugs, that make AutoLISP crash or return
  false results. They are just bad language implementations.

  * AND and OR should return the value of the not-NIL argument
    instead of T
"

This can make sense since the AutoLISP functions using a test expression (as IF, COND, WHILE) don't need the test expression result to be strictly a Boolean (NIL or T) but Nil or anything else (as LISP is a dynamicaly typed language).
Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More subroutine functions
« Reply #23 on: March 20, 2009, 12:19:43 PM »
@Se7en: Ok, peace, HAGWEETTYL. :P
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #24 on: March 20, 2009, 01:50:25 PM »
Still about OR (and AND).

In its AutoLISP FAQ (subject 7), Reni Urban said :

"The following are not real bugs, that make AutoLISP crash or return
  false results. They are just bad language implementations.

  * AND and OR should return the value of the not-NIL argument
    instead of T
"

This can make sense since the AutoLISP functions using a test expression (as IF, COND, WHILE) don't need the test expression result to be strictly a Boolean (NIL or T) but Nil or anything else (as LISP is a dynamicaly typed language).

I believe he stated that because in the Scheme Language, that is how it treats it's Boolean expressions (I thought i remembered he stated that XLisp was a Scheme cousin or something like that in a post. But anyways...).

For example In Scheme:
Code: [Select]
(and (= 2 2) (> 2 1))                   =>  #t
(and (= 2 2) (< 2 1))                   =>  #f
(and 1 2 'c '(f g))                     =>  (f g)
(and) 

(or (= 2 2) (> 2 1))                    =>  #t
(or (= 2 2) (< 2 1))                    =>  #t
(or #f #f #f)                           =>  #f
(or (memq 'b '(a b c)) (/ 3 0))         =>  (b c)
Therefore in Scheme: (set var (or <condition> <condition> ... would be legal.

But i could be wrong.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: More subroutine functions
« Reply #25 on: March 20, 2009, 02:28:18 PM »
Here is my olive branch but watch out for the thorns. :-D

Sorry, bad joke.

How about an attempt at our own OR and AND that does return a value?

Problem is that you need to use a list because of the undetermined number of arguments.
Try this out.
Code: [Select]
(defun _or (lst / result)
  (vl-some (function (lambda(x) (setq result (eval x)))) lst)
  result
)

(defun _and (lst / result)
  (if (vl-every (function (lambda(x) (setq result (eval x)))) lst)
    result
  )
)


(defun c:test()
  (print (_or '((= 2 3)(> 2 5)(+ 2 3))))
  (print (_and '((= 2 3)(> 2 5)(+ 2 3))))
  (print (_and '((= 2 2)(> 8 5)(+ 2 3))))
  (princ)
)
« Last Edit: March 20, 2009, 03:55:26 PM by CAB »
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Water Moccasin
  • Posts: 2275
  • Marseille, France
Re: More subroutine functions
« Reply #26 on: March 20, 2009, 03:42:46 PM »
Quote
How about an attempt at our own OR and AND that does return a value?

Code: [Select]
(defun _or (lst / r)
  (if lst
    (if (setq r (eval (car lst)))
      r
      (_or (cdr lst))
    )
  )
)


(defun _and (lst)
  (if (cdr lst)
    (if (eval (car lst))
      (_and (cdr lst))
    )
    (eval (car lst))
  )
)

OR with COND

Code: [Select]
(defun _or (lst)
  (eval (cons 'cond (mapcar 'list lst)))
)
« Last Edit: March 20, 2009, 04:10:37 PM by gile »
Speaking English as a French Frog

Daniel J. Ellis

  • Swamp Rat
  • Posts: 809
Re: More subroutine functions
« Reply #27 on: March 20, 2009, 03:44:13 PM »
MP, is (if numberp default) checking to make sure that the default variable a number?  very "pure," but possibly superfluous for a hard-coded value?

Agreed. When I penned it I was thinking it would be convenient for the caller to pass nil if they didn't have a default value in mind. The upshot being that an integer (0) would still be returned. Perhaps over thought.

Gotya.  I can see how it could have its uses, but think it would overkill in this situation (the whole purpose is to set a default, after all)  It's a trick I'll try to remember for future use though.

dJE
===
dJE

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: More subroutine functions
« Reply #28 on: March 20, 2009, 08:53:04 PM »
I've ordered a T Shirt ..
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #29 on: March 20, 2009, 08:54:53 PM »
Ok, that was funny.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org