[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: scheme max/min in c++
From: |
David Kastrup |
Subject: |
Re: scheme max/min in c++ |
Date: |
Fri, 19 Aug 2016 16:23:09 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) |
Mark Knoop <address@hidden> writes:
> At 13:56 on 19 Aug 2016, David Kastrup wrote:
>>Mark Knoop <address@hidden> writes:
>>> Just trying out some more ideas for the keep-alive-together-engraver.
>>> How do I use the Guile max/min functions on a scheme list in C++? I
>>> have tried this:
>>
>>Well...
>>
>> 5.5.2.11 Arithmetic Functions
>> .............................
>>
>> The C arithmetic functions below always takes two arguments, while
>> the Scheme functions can take an arbitrary number. When you need to
>> invoke them with just one argument, for example to compute the
>> equivalent od `(- x)', pass `SCM_UNDEFINED' as the second one:
>> `scm_difference (x, SCM_UNDEFINED)'.
>>
>>[...]
>>
>> -- Scheme Procedure: max x1 x2 ...
>> -- C Function: scm_max (x1, x2)
>> Return the maximum of all parameter values.
>>
>>Most notable, scm_max is a C function, not a Scheme function (which
>>would be of type SCM itself). You cannot pass it to scm_apply_0.
>
> Thanks David for the pointers.
>
>>If you really, really want to use the Scheme function, however, ...
>
> I have no particular desire to use the Scheme function, but was just
> looking for the most elegant way to do this. Would it perhaps be better
> to convert to C types?
The usual thing is to open-code the min/max for arbitrary numbers of
arguments. Like
SCM max;
if (scm_is_pair (x))
{
max = scm_car (x);
for (SCM n = scm_cdr (x); scm_is_pair (n); n = scm_cdr (n))
max = scm_max (max, scm_car (n));
}
else
max = default_value;
Maybe one should write a C version of `reduce' for automating this. But
then it's not all that complicated wither.
--
David Kastrup