an example:
foo (> x y) ? assign_true : assign_false
via wikipedia:
<http://en.wikipedia.org/wiki/Ternary_operation#Common_Lisp>
the example has:
(setf result (if (> a b) x y))
But since I'd need to call the function in question 1st,
(and then if the return is nil which is what I want to discern...)
it does not look like I've managed to pull if off:
(x (search foo (string-downcase line)))
(setf y (if (null x) 0 y))
Trying to do it with a single line for a minimum of fuss.
--
later on,
Mike
> Is there an ternary operator equivalent (for functions) in common lisp?
> [...]
> the example has:
>
> (setf result (if (> a b) x y))
It is precisely that. Did you try it?
> (x (search foo (string-downcase line)))
More to the point...
If 'foo' is not found in 'line' an error will
be raised & that is the chief problem.
You write everything in one line. Lisp does not care much about lines.
>
> --
> later on,
> Mike
>
> http://www.topcat.hypermart.net/index.html
(setf y (if (null (search foo (string-downcase line))) 0 y))
or
(unless (search foo (string-downcase line))
(setf y 0))
Well, I'm not sure I can in this case...
I'd have to call it twice, ie:
(setf x (if (null (search foo (string-downcase line))) 0
(search foo (string-downcase line))))
(those parens may be off), but my point is, the search
function chokes on nil so I dont see how it would work...
> Is there an ternary operator equivalent (for functions) in common lisp?
>
> an example:
>
> foo (> x y) ? assign_true : assign_false
>
> via wikipedia:
>
> <http://en.wikipedia.org/wiki/Ternary_operation#Common_Lisp>
>
> the example has:
>
> (setf result (if (> a b) x y))
>
> But since I'd need to call the function in question 1st,
> (and then if the return is nil which is what I want to discern...)
> it does not look like I've managed to pull if off:
>
> (x (search foo (string-downcase line)))
>
> (setf y (if (null x) 0 y))
>
> Trying to do it with a single line for a minimum of fuss.
What you've not managed to do, is to explain clearly what you want.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
> What you've not managed to do, is to explain clearly what you want.
I know... (such are the articulation skills of a man
of the prairies, its very frustrating to only grasp at
words) shrugs... But I wont give up =)
I'll post the latest iteration of my project later today.
Its a parser based on these specs:
<http://www.topcat.hypermart.net/papers/topic-spec.txt>
Examples in other languages (source/binaries) here:
<http://www.topcat.hypermart.net/topic.html>
> Pascal J. Bourguignon wrote:
>
>> What you've not managed to do, is to explain clearly what you want.
>
> I know... (such are the articulation skills of a man
> of the prairies, its very frustrating to only grasp at
> words) shrugs... But I wont give up =)
From your followup messages, I guess vaneck's answer is what you want.
You may also have a look at anaphoric macros.
eg. http://common-lisp.net/project/anaphora/
> (setq y (or (and (search foo (string-downcase line)) some-val) other-val))
Thank you vanekl, I'll study this.
there is no distinct non ternary if
basic point to understand is in CL there is no statement versus
expression distinction
try
(defun coin-flip ()
(= 0 (random 2)))
(print (if (coin-flip)
"head"
"tail"))
-Antony
I believe this gets the behavior you are looking for without calling
it twice.
(setf x (or (search foo (string-downcase line)) 0))
CL-USER> (setf x (or (search "example" '()) 0))
0
CL-USER> x
0
CL-USER> (setf x (or (search "example" "the line includes example")
0))
18
CL-USER> x
18
Thanks for the example Antony, I've added this to my notes.
> I believe this gets the behavior you are looking for without calling
> it twice.
>
> (setf x (or (search foo (string-downcase line)) 0))
>
> CL-USER> (setf x (or (search "example" '()) 0))
> 0
> CL-USER> x
> 0
> CL-USER> (setf x (or (search "example" "the line includes example")
> 0))
> 18
> CL-USER> x
> 18
Hmmm, I must study this (very new to CL).
Thank you kindly for your input Matt.
> Is there an ternary operator equivalent (for functions) in common lisp?
>
> an example:
>
> foo (> x y) ? assign_true : assign_false
>
> via wikipedia:
>
> <http://en.wikipedia.org/wiki/Ternary_operation#Common_Lisp>
>
> the example has:
>
> (setf result (if (> a b) x y))
>
Okay I've got it now...
According to MSDN, under Windows, the environmental variable
named 'OS' has been available since 1995. With that info in
hand, here a quick common lisp snippet that will clear a text
mode screen under both nix/win by invokeing 'clear' on the unix
side, and 'cls' on the windows side.
(ext:shell (if (null (ext:getenv "OS")) "clear" "cls"))
> mss wrote:
>
>> Is there an ternary operator equivalent (for functions) in common lisp?
>>
>> an example:
>>
>> foo (> x y) ? assign_true : assign_false
>>
>> via wikipedia:
>>
>> <http://en.wikipedia.org/wiki/Ternary_operation#Common_Lisp>
>>
>> the example has:
>>
>> (setf result (if (> a b) x y))
>>
>
> Okay I've got it now...
>
> According to MSDN, under Windows, the environmental variable
> named 'OS' has been available since 1995. With that info in
> hand, here a quick common lisp snippet that will clear a text
> mode screen under both nix/win by invokeing 'clear' on the unix
> side, and 'cls' on the windows side.
export OS=Linux
> (ext:shell (if (null (ext:getenv "OS")) "clear" "cls"))
Have a look at clhs *features*
(ext:shell #+WIN32 "cls"
#+UNIX "clear"
#-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
> export OS=Linux
cringe... yes you're right.
>> (ext:shell (if (null (ext:getenv "OS")) "clear" "cls"))
>
>
> Have a look at clhs *features*
>
> (ext:shell #+WIN32 "cls"
> #+UNIX "clear"
> #-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
Will do, thanks Pascal.
> (ext:shell (if (null (ext:getenv "OS")) "clear" "cls"))
That's the right idiom, though probably not a very robust way of
detecting the platform.
> Have a look at clhs *features*
>
> (ext:shell #+WIN32 "cls"
> #+UNIX "clear"
> #-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
WIN32 call works on WIN32 too yes?
(I can only test still on 32bit).
> That's the right idiom, though probably not a very robust way of
> detecting the platform.
actually, it will work fine, as I doubt anyone
would ever make that type of change, but...
to be safe, I followed Pascal's advice.
What did you have in mind Tim?
> Pascal J. Bourguignon wrote:
>
>> Have a look at clhs *features*
>>
>> (ext:shell #+WIN32 "cls"
>> #+UNIX "clear"
>> #-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
>
> WIN32 call works on WIN32 too yes?
> (I can only test still on 32bit).
(should read: 'works on WIN64 too yes?')
> What did you have in mind Tim?
Anything that doesn't use an environment variable, really.
> mss wrote:
>
>> Pascal J. Bourguignon wrote:
>>
>>> Have a look at clhs *features*
>>>
>>> (ext:shell #+WIN32 "cls"
>>> #+UNIX "clear"
>>> #-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
>>
>> WIN32 call works on WIN32 too yes?
>> (I can only test still on 32bit).
>
> (should read: 'works on WIN64 too yes?')
The best way to know is to compile clisp on WIN64 and see what's on its
*features* list.
> mss wrote:
>
>> Pascal J. Bourguignon wrote:
>>
>>> Have a look at clhs *features*
>>>
>>> (ext:shell #+WIN32 "cls"
>>> #+UNIX "clear"
>>> #-(or WIN32 UNIX) (error "How do I clear a screen on this OS?"))
>>
>> WIN32 call works on WIN32 too yes?
>> (I can only test still on 32bit).
>
> (should read: 'works on WIN64 too yes?')
Also, have a look at: http://www.cliki.net/features
> Also, have a look at: http://www.cliki.net/features
Yes, will do. Thanks Pascal.