Implied Conditional

Started by Jeremy Dunn, October 21, 2005, 07:50:50 PM

Previous topic - Next topic

Jeremy Dunn

I really enjoy the implied indexing of NewLISP. MuLISP I believe had something called the implied conditonal. You could write a statement like

(if (and x y) A B) as ((and x y) A B) where the "if" is implied. Could we have this as well?

Lutz

#1
Yes, implicit indexing has been well received by everybody.



Regarding an implicit 'if': I have been going back and forth on this in my mind, I am just not sure if I want it, if it leads to too much ambiguities from a users point of view? When the first expression evaluates to 'nil' things are clear, but what if the the xpression evauates to a list or number? Is it now impicit indexing or impixit 'if' etc.





Lutz

Fanda

#2
Interesting :)

You would probably need to have functions 'or' and 'and' always return 'nil' or 'true'. That way we might lose some of their functionality:



http://www.alh.net/newlisp/phpbb/viewtopic.php?p=4554">http://www.alh.net/newlisp/phpbb/viewtopic.php?p=4554



Dmi => (parse (or (lookup "key" some-list 1) "") "delimiter")



> (or nil "")

""

> (or "word" "")

"word"

>



Fanda

Fanda

#3
Actually.... function 'or' already does something like 'if'  :-))))

(x is nil) => ""

(x is not nil) => x



You can compare numbers (< = >) using:

> (setq x 3)

3

> (sgn x "<0" "0" ">0")

">0"

> (sgn (- x 5) "<5" "5" ">5")

"<5"



Fanda

PaipoJim

#4
Quote from: "Lutz"Regarding an implicit 'if': I have been going back and forth on this in my mind, I am just not sure if I want it, if it leads to too much ambiguities from a users point of view? When the first expression evaluates to 'nil' things are clear, but what if the the xpression evauates to a list or number? Is it now impicit indexing or impixit 'if' etc.


Indeed, with an implicit "if" it will take longer to comprehend what's going on when reading unfamiliar source code; perhaps even one's own after a lapse of a few months.  ;-) The implicit "else" in "if" constructs is already quite enough, thank you very much.



OTH, addition of this feature would help greatly in facilitating the holding of an annual "most obscure code" contest for newLISP, such as they do for C and PERL.

-

Lutz

#5
Yes, and here further clarifications of the whole issue:



Expression evaluation is done by evaluating the first member of an expression and than applying it to the parameters after it. Depending on the type of the first evaluated member an action is taken:



built-in function -> function application

lambda expression -> user defined function application



number -> implicit indexing for 'rest' and 'slice'

string -> implicit indexing for 'nth'

list and array -> implicit indexing for 'nth'



There are two problems with triggering 'if' on the boolean type:



(1) 'if' and 'unless' not only work on 'nil' and 'true', but also take '(not nil)' as 'true'. A common pattern used is:



(if myVar) (do-this) (do-that))



When 'myVar' is a number, string or list ambiguity arises.



(2) 'nil' in the first member of a list is a frequent error when coding through mispelling:



(define (fooo x y) (...))



(fuu 3 4)



Currently the previous code snippet would cause and "undefined function" error, becuase 'fuu' most likely rerturns 'nil'. With implicit 'if' the expression simply would return 4 and obscure the error.



Lutz