Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ternary operator idiom?

605 views
Skip to first unread message

mss

unread,
Feb 7, 2011, 11:05:11 AM2/7/11
to
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.

--
later on,
Mike

http://www.topcat.hypermart.net/index.html

Tim Bradshaw

unread,
Feb 7, 2011, 11:11:33 AM2/7/11
to
On 2011-02-07 16:05:11 +0000, mss said:

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

mss

unread,
Feb 7, 2011, 11:13:50 AM2/7/11
to
mss wrote:

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

vanekl

unread,
Feb 7, 2011, 11:15:25 AM2/7/11
to
(setq y (or (and (search foo (string-downcase line)) some-val) other-val))

jos...@lisp.de

unread,
Feb 7, 2011, 11:17:01 AM2/7/11
to
On 7 Feb., 17:05, mss <m...@dev.null> 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))
>
> 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.

You write everything in one line. Lisp does not care much about lines.

(setf y (if (null (search foo (string-downcase line))) 0 y))

or

(unless (search foo (string-downcase line))
(setf y 0))

mss

unread,
Feb 7, 2011, 11:23:02 AM2/7/11
to
Tim Bradshaw wrote:

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

Pascal J. Bourguignon

unread,
Feb 7, 2011, 11:37:57 AM2/7/11
to
mss <m...@dev.null> writes:

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

mss

unread,
Feb 7, 2011, 11:48:08 AM2/7/11
to
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 =)

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

unread,
Feb 7, 2011, 11:53:34 AM2/7/11
to

mss <m...@dev.null> writes:

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

vanekl

unread,
Feb 7, 2011, 12:00:39 PM2/7/11
to
It would probably be easier to write a grammar for an existing parser. This thread discusses some of the CL parser choices that already exist.
http://common-lisp.net/pipermail/pro/2011-February/000377.html

mss

unread,
Feb 7, 2011, 12:00:51 PM2/7/11
to
vanekl wrote:

> (setq y (or (and (search foo (string-downcase line)) some-val) other-val))

Thank you vanekl, I'll study this.

Antony

unread,
Feb 7, 2011, 12:35:02 PM2/7/11
to
On 2/7/2011 8:05 AM, mss wrote:
> Is there an ternary operator equivalent (for functions) in common lisp?
in CL if is the ternary operator

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

Matt

unread,
Feb 7, 2011, 4:15:10 PM2/7/11
to

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

mss

unread,
Feb 7, 2011, 10:07:31 PM2/7/11
to
Antony wrote:

Thanks for the example Antony, I've added this to my notes.

mss

unread,
Feb 7, 2011, 10:09:31 PM2/7/11
to
Matt wrote:

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

mss

unread,
Feb 9, 2011, 1:43:47 PM2/9/11
to
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.

(ext:shell (if (null (ext:getenv "OS")) "clear" "cls"))

Pascal J. Bourguignon

unread,
Feb 9, 2011, 1:55:03 PM2/9/11
to
mss <m...@dev.null> writes:

> 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?"))

mss

unread,
Feb 9, 2011, 2:28:09 PM2/9/11
to
Pascal J. Bourguignon wrote:

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

Tim Bradshaw

unread,
Feb 10, 2011, 4:57:04 AM2/10/11
to
On 2011-02-09 18:43:47 +0000, mss said:

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

mss

unread,
Feb 10, 2011, 8:58:04 AM2/10/11
to
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).

mss

unread,
Feb 10, 2011, 9:00:23 AM2/10/11
to
Tim Bradshaw wrote:

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

mss

unread,
Feb 10, 2011, 9:12:14 AM2/10/11
to
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?')

Tim Bradshaw

unread,
Feb 10, 2011, 9:55:42 AM2/10/11
to
On 2011-02-10 14:00:23 +0000, mss said:

> What did you have in mind Tim?

Anything that doesn't use an environment variable, really.

Pascal J. Bourguignon

unread,
Feb 10, 2011, 2:56:31 PM2/10/11
to
mss <m...@dev.null> writes:

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

Pascal J. Bourguignon

unread,
Feb 10, 2011, 2:57:20 PM2/10/11
to
mss <m...@dev.null> writes:

> 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

mss

unread,
Feb 11, 2011, 7:47:49 AM2/11/11
to
Pascal J. Bourguignon wrote:

> Also, have a look at: http://www.cliki.net/features

Yes, will do. Thanks Pascal.

0 new messages