Author Topic: logic doubt  (Read 1168 times)

0 Members and 1 Guest are viewing this topic.

VovKa

  • Swamp Rat
  • Posts: 1118
  • Ukraine
Re: logic doubt
« Reply #15 on: March 08, 2019, 05:04:53 PM »
Lisp checks ALL conditionals.
Code: [Select]
(or (setq a 1) (setq a 2))

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1363
  • class keyThumper<T>:ILazy<T>
Re: logic doubt
« Reply #16 on: March 08, 2019, 05:21:21 PM »
Lisp checks ALL conditionals.
Code: [Select]
(or (setq a 1) (setq a 2))

My bad ... you are correct.
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

Grrr1337

  • Swamp Rat
  • Posts: 693
Re: logic doubt
« Reply #17 on: March 08, 2019, 07:10:31 PM »
... because I can discern the logic easier than the second. To explain why would require me to reveal how my brain works...

Cheers.

I'm on the same boat as MP - hence I'd use the most readable evaluation for me.



Coding Conventions == null

Time ago somewhere here I was discussing about how the perception about the syntax of the code changes for one
which is based on the time he spent writing in a certain language, by constantly changing or modifying his style of writing.

For example I'd dare to assume that MP's lisp coding syntax wasn't figured out in a single night by him, but rather constantly re-styled through the years.
One could trace Lee's syntax style from 2008 and how it changed till now..
I've also changed my style of writing many times, so I couldn't figure out what I was writing like 2 months ago (but in the end I ended-up with one that works for me).


BTW would be more easier to point out the different evaluations if we wrap them in a defuns, just like Marc'Antonio Alessi suggested -

Code: [Select]
(defun f1 (x y)
  (not (and x y))
)

(defun f2 (x y)
  (or (not x) (not y))
)

(defun f3 ( x y )
  (vl-some 'not (list x y))
)

(defun f4 ( x y )
  (not (vl-every 'eval (list x y)))
)

(defun f5 ( x y )
  (not (if x y))
)

(defun f6 ( x y )
  (cond
    ( (not x) )
    ( (not y) )
  )
)

Sample test to check if our evaluations work as expected -

Code: [Select]
(mapcar
  '(lambda (x)
    (list
      (apply 'f1 x)
      (apply 'f2 x)
      (apply 'f3 x)
      (apply 'f4 x)
      (apply 'f5 x)
      (apply 'f6 x)
    )
  )
  '((t nil)(nil t)(t t)(nil nil))
)
>> ((T T T T T T) (T T T T T T) (nil nil nil nil nil nil) (T T T T T T))

If the speed doesn't matter then my reading preferences would be on the evaluations within the functions f3 and f6.

f6 brings me memories about a fragment of John's post which stated this:
Quote
Ultimatly all that was required was a simple COND statement to implement the concept

:)

Cheers!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 988
  • Marco
Re: logic doubt
« Reply #18 on: March 09, 2019, 04:22:57 AM »
More simple?
(or (and x y) (progn ...))

I think you meant to post something else.
Excuse my ignorance and incompetence, would you please be so kind to explain to me why what I wrote is completely wrong?
Thank you for your time.  :-)

Code: [Select]
OP:
(if (not (and x y))       ...)
(if (or (not x) (not y))  ...)

My Post:
(or (and x y)     (progn ...))

-----------------------------------

(defun TestA () (if (not (and x y))      (print "yes")) (princ))
(defun TestB () (if (or (not x) (not y)) (print "yes")) (princ))
(defun TestC () (or (and x y)            (print "yes")) (princ))

Code: [Select]
-----------------------------------
Comando: (setq x T y T) T

Comando: (TestA) =>
Comando: (TestB) =>
Comando: (TestC) =>

------------------------------------
Comando: (setq x T y nil) nil

Comando: (TestA) => "yes"
Comando: (TestB) => "yes"
Comando: (TestC) => "yes"

------------------------------------
Comando: (setq x nil y T) T

Comando: (TestA) => "yes"
Comando: (TestB) => "yes"
Comando: (TestC) => "yes"

------------------------------------
Comando: (setq x nil y nil) nil

Comando: (TestA) => "yes"
Comando: (TestB) => "yes"
Comando: (TestC) => "yes"

------------------------------------

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1363
  • class keyThumper<T>:ILazy<T>
Re: logic doubt
« Reply #19 on: March 09, 2019, 05:03:27 AM »
Settle down Marc' .. I didn't say or intimate you were ignorant and incompetent ... I just thought you posted something other than intended.

This is how I read the code you posted.
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (or
  3.      (and x y)      ; first conditional test
  4.      (progn ...)    ; second conditional test  
  5. )
  6.  
  7.  

Regards,
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #20 on: March 09, 2019, 12:38:03 PM »
For example I'd dare to assume that MP's lisp coding syntax wasn't figured out in a single night by him, but rather constantly re-styled through the years.

Pretty much 2 phases primary phases. When I first learned LISP I used an indent of 2 spaces. As work demanded the employ of additional languages I opted to use an indent of 4 spaces universally. The effect on my LISP coding was that it required a more vertical orientation if I wanted to honour a margin of 80 chars. Other changes if present are more subtle, reflecting continued knowledge and growth in my programming quest. Or the quantity of coffee on a given day.

BTW would be more easier to point out the different evaluations if we wrap them in a defuns ...

Code: [Select]
(defun f1 (x y)
  (not (and x y))
)

...

While I appreciate the simplification you aim to achieve for me this erodes the signal to noise ratio.

I can discern (if (not (and x y)) ...) far faster -- and in-situ -- than (if (f1 x y) ...) as f1 is meaningless until I search thru source code for the def. While a construct like (mapcar '(lambda (x) (list (apply 'f1 x) ...) may occupy less real estate in a working program for demonstration purposes I find it difficult to see benefit as reading clarity was fundamental to the original inquiry.

Beer.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #21 on: March 09, 2019, 12:40:23 PM »
Lisp checks ALL conditionals.
Code: [Select]
(or (setq a 1) (setq a 2))

My bad ... you are correct.

You had the malady correct (lack of short-circuit evaluation), wrong patient: VBA.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #22 on: March 09, 2019, 12:41:15 PM »
the gray stuff is difficult to understand , particularly on Saturdays and Wednesdays.

TRUTH
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #23 on: March 09, 2019, 01:10:23 PM »
More simple?
(or (and x y) (progn ...))

I see your intent Marco but for me it requires pause, whereas the easy digestion of (if (not (and x y)) ...) makes the latter preferable.

Cheers.
« Last Edit: March 09, 2019, 03:18:19 PM by MP »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Stefan

  • Bull Frog
  • Posts: 220
Re: logic doubt
« Reply #24 on: March 09, 2019, 02:36:39 PM »
Hi...

I have those 2 expressions which are working correctly, but I am in doubt which one should I actually use in routine :

Code: [Select]
(if (not (and x y))
  ...
)

Code: [Select]
(if (or (not x) (not y))
  ...
)

It is difficult to determine which is faster, so I am in doubt. Like I said they both are logically fine and work as desired...

Marco, both are ugly... Without seeing the big picture, I can only suggest what it seems to me more elegant:
Instead of:
Code: [Select]
(if
  (not (and x y))
  (do_the_stuff)
  (do_the_other_stuff)
)
use
Code: [Select]
(if
  (and x y)
  (do_the_other_stuff)
  (do_the_stuff)
)
I know, I know... you didn't mention anything about any ELSE statement, but then it looks even more... oh, well,  you know what I mean...

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #25 on: March 09, 2019, 03:21:10 PM »
Marco, both are ugly...

Disagree, logic is relatively concise, and the intention clear: do something if either x and y are nil.

<snip> ... seems to me more elegant: <snip>

use:

(if (and x y)
  (do_the_other_stuff)
  (do_the_stuff)
)

What if there is no need to perform something when x and y are non nil?
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Stefan

  • Bull Frog
  • Posts: 220
Re: logic doubt
« Reply #26 on: March 09, 2019, 03:45:51 PM »
Marco, both are ugly...

Disagree, logic is relatively concise, and the intention clear: do something if either x and y are nil.

<snip> ... seems to me more elegant: <snip>

use:

(if (and x y)
  (do_the_other_stuff)
  (do_the_stuff)
)

What if there is no need to perform something when x and y are non nil?

Yeah, I know, I already mentioned that possibility.
Still ugly to me

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #27 on: March 09, 2019, 03:51:46 PM »
Yeah, I know, I already mentioned that possibility.

Far more than a possibility given the original post so were back to "the ugly code".
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Stefan

  • Bull Frog
  • Posts: 220
Re: logic doubt
« Reply #28 on: March 09, 2019, 04:12:14 PM »
Yeah, I know, I already mentioned that possibility.

Far more than a possibility given the original post so were back to "the ugly code".

OK, no more"ugly code". Let me reformulate.
Marko, in case of an "else" statement, you can use
Code: [Select]
(if
  (and x y)
  (do_the_oter_stuff)
  (do_the_stuff)
)

MP

  • Seagull
  • Posts: 17369
Re: logic doubt
« Reply #29 on: March 09, 2019, 04:36:36 PM »
OK, no more"ugly code". Let me reformulate.
Marko, in case of an "else" statement, you can use
Code: [Select]
(if
  (and x y)
  (do_the_oter_stuff)
  (do_the_stuff)
)



Some day Ill grow up, but not today lol.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox