[m-users.] String comparison when compiling to C

Richard O'Keefe raoknz at gmail.com
Sat Aug 24 15:56:14 AEST 2024


Does
 :- import_module string.
...
    compare(>, S1, S2)

work for you?

On Sat, 24 Aug 2024 at 02:53, Sean Charles (emacstheviking)
<objitsu at gmail.com> wrote:
>
>
> % The builtin comparison operation on strings is also implementation
> % dependent. The current implementation performs string comparison using
> %
> % - C's strcmp() function, when compiling to C;
>
> Guided by the text, "The builtin comparison operation on strings "... I assumed that >, <, >= etc would work fork string types but my code:
>
>     615 op_greater_than(!Mode, !Stk) :-
>     616     ( if [ V1, V2 | Rest ] = !.Stk then
>     617         !:Stk = Rest,
>     618
>     619         ( if V1 = int(N1), V2 = int(N2) then
>     620             trace [io(!IO)] (
>     621                 io.format("> %s %s\n", [s(string(V1)),s(string(V2))],!IO)
>     622             ),
>     623             ( if N1 > N2
>     624             then push_true(!Mode, !Stk)
>     625             else push_false(!Mode, !Stk)
>     626             )
>     627
>     628         else if V1 = str(S1), V2 = str(S2) then
>     629             trace [io(!IO)] (
>     630                 io.format("> %s %s\n", [s(string(V1)),s(string(V2))],!IO)
>     631             ),
>     632             ( if S1 > S2
>     633             then push_true(!Mode, !Stk)
>     634             else push_false(!Mode, !Stk)
>     635             )
>     636         else
>     637             seterr("> requires two objects of the same type.", !Mode)
>
> gets these errors:
>
> dstack.m:632: In clause for predicate `op_greater_than'/4:
> dstack.m:632:   in call to predicate `int.>'/2:
> dstack.m:632:   in argument 1:
> dstack.m:632:     variable `S1' has type
> dstack.m:632:       string,
> dstack.m:632:     expected type was
> dstack.m:632:       int;
> dstack.m:632:   in argument 2:
> dstack.m:632:     variable `S2' has type
> dstack.m:632:       string,
> dstack.m:632:     expected type was
> dstack.m:632:       int.
>
>
> So what is the 'way' to do string comparisons? I find it odd in all the code I've written, I have never needed to do this before!
>
> Thanks,
> Sean.
>
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users


More information about the users mailing list