<html><head><meta http-equiv="content-type" content="text/html; charset=us-ascii"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Hi Richard,<div><br></div><div>All sorted thanks, I already had `string` imported. I think working with Python has dulled my brain sometimes.</div><div>The Mercury `way` is a much better fit for how I intend to work it going forward, I am writing a strongly typed FORTH with Mercury as a rapid application tool for another project.</div><div><br></div><div>I have this code now, so far, but eventually I plan to support all the Mercury numerical types, different bases etc as FORTH allows, it won't be a strict standards compliant FORTH but rather, my interpretation of it guided by Mercury...</div><div><br></div><div><div><font face="Andale Mono">%</font></div><div><font face="Andale Mono">% Comparison operators.</font></div><div><font face="Andale Mono">%</font></div><div><font face="Andale Mono">core("<", stack('<')).</font></div><div><font face="Andale Mono"><br></font></div><div><font face="Andale Mono">:- pred '<'(opstate::in, opstate::out, dstk::in, dstk::out) is det.</font></div><div><font face="Andale Mono"> % [ V2, V1 ] <=> ( n1 n2 -- flag ) ie TOS is n2, is V1, global rule!</font></div><div><font face="Andale Mono">'<'(!Mode, !Stk) :-</font></div><div><font face="Andale Mono"> ( if [ V2, V1 | Rest ] = !.Stk then</font></div><div><font face="Andale Mono"> !:Stk = Rest,</font></div><div><font face="Andale Mono"> ( if V1 = int(N1), V2 = int(N2) then</font></div><div><font face="Andale Mono"> ( if compare(<, N1, N2)</font></div><div><font face="Andale Mono"> then push_true(!Mode, !Stk)</font></div><div><font face="Andale Mono"> else push_false(!Mode, !Stk)</font></div><div><font face="Andale Mono"> )</font></div><div><font face="Andale Mono"> else if V1 = str(S1), V2 = str(S2) then</font></div><div><font face="Andale Mono"> trace [io(!IO)] (</font></div><div><font face="Andale Mono"> io.format("> %s %s\n", [s(string(V1)),s(string(V2))],!IO)</font></div><div><font face="Andale Mono"> ),</font></div><div><font face="Andale Mono"> ( if compare(<, S1, S2)</font></div><div><font face="Andale Mono"> then push_true(!Mode, !Stk)</font></div><div><font face="Andale Mono"> else push_false(!Mode, !Stk)</font></div><div><font face="Andale Mono"> )</font></div><div><font face="Andale Mono"> else</font></div><div><font face="Andale Mono"> seterr("> requires two objects of the same type.", !Mode)</font></div><div><font face="Andale Mono"> )</font></div><div><font face="Andale Mono"> else stkerr(">", 2, !.Stk, !Mode)).</font></div><div><br></div><div>Once all the types I want are in the above code, then I will find a way to reduce the code, `compare/3` is great in that respect as it is a uniform interface across types.</div><div><br></div><div>I hope to find a way to generalise the call to `compare/3`, I will experiment!!! As much as Mercury sometimes makes my head want to explode at times, I can't help thinking that if the worldf at large used it, the world might be a better place, well, at least less global tech issues, e.g. Crowdstrike.</div><div><br></div><div>It's a bunch of fun so far at times (sarcasm emoji) but what else would one be doing?</div><div><br></div><div>Thanks again,</div><div>Sean.</div><div><br></div><div><br><blockquote type="cite"><div>On 24 Aug 2024, at 06:56, Richard O'Keefe <raoknz@gmail.com> wrote:</div><br class="Apple-interchange-newline"><div><div>Does<br> :- import_module string.<br>...<br> compare(>, S1, S2)<br><br>work for you?<br><br>On Sat, 24 Aug 2024 at 02:53, Sean Charles (emacstheviking)<br><objitsu@gmail.com> wrote:<br><blockquote type="cite"><br><br>% The builtin comparison operation on strings is also implementation<br>% dependent. The current implementation performs string comparison using<br>%<br>% - C's strcmp() function, when compiling to C;<br><br>Guided by the text, "The builtin comparison operation on strings "... I assumed that >, <, >= etc would work fork string types but my code:<br><br> 615 op_greater_than(!Mode, !Stk) :-<br> 616 ( if [ V1, V2 | Rest ] = !.Stk then<br> 617 !:Stk = Rest,<br> 618<br> 619 ( if V1 = int(N1), V2 = int(N2) then<br> 620 trace [io(!IO)] (<br> 621 io.format("> %s %s\n", [s(string(V1)),s(string(V2))],!IO)<br> 622 ),<br> 623 ( if N1 > N2<br> 624 then push_true(!Mode, !Stk)<br> 625 else push_false(!Mode, !Stk)<br> 626 )<br> 627<br> 628 else if V1 = str(S1), V2 = str(S2) then<br> 629 trace [io(!IO)] (<br> 630 io.format("> %s %s\n", [s(string(V1)),s(string(V2))],!IO)<br> 631 ),<br> 632 ( if S1 > S2<br> 633 then push_true(!Mode, !Stk)<br> 634 else push_false(!Mode, !Stk)<br> 635 )<br> 636 else<br> 637 seterr("> requires two objects of the same type.", !Mode)<br><br>gets these errors:<br><br>dstack.m:632: In clause for predicate `op_greater_than'/4:<br>dstack.m:632: in call to predicate `int.>'/2:<br>dstack.m:632: in argument 1:<br>dstack.m:632: variable `S1' has type<br>dstack.m:632: string,<br>dstack.m:632: expected type was<br>dstack.m:632: int;<br>dstack.m:632: in argument 2:<br>dstack.m:632: variable `S2' has type<br>dstack.m:632: string,<br>dstack.m:632: expected type was<br>dstack.m:632: int.<br><br><br>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!<br><br>Thanks,<br>Sean.<br><br>_______________________________________________<br>users mailing list<br>users@lists.mercurylang.org<br>https://lists.mercurylang.org/listinfo/users<br></blockquote></div></div></blockquote></div><br></div></body></html>