unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Comparison operators for strings /and/ numbers?
@ 2017-08-24  6:05 Christopher Howard
  2017-08-24  8:05 ` David Kastrup
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Christopher Howard @ 2017-08-24  6:05 UTC (permalink / raw)
  To: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 557 bytes --]

Hi, in another lisp I have been working with, it has <, >, and ==
(structure equality) operators which can take string arguments, number
arguments, or a mixture of both. But it seems in guile that there are
separate comparison operators for strings and for numbers. This makes
sense but is not very convenient for my present purpose. Is there some
other guile operators or extension operators that will handle both? I
could make some I'm sure, but I don't want to reinvent the wheel.

-- 
https://qlfiles.net
https://emailselfdefense.fsf.org/en/

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24  6:05 Comparison operators for strings /and/ numbers? Christopher Howard
@ 2017-08-24  8:05 ` David Kastrup
  2017-09-19  4:27   ` Christopher Howard
  2017-08-24  8:29 ` Arne Babenhauserheide
  2017-08-24 13:31 ` Chris Vine
  2 siblings, 1 reply; 8+ messages in thread
From: David Kastrup @ 2017-08-24  8:05 UTC (permalink / raw)
  To: guile-user

Christopher Howard <christopher.howard@qlfiles.net> writes:

> Hi, in another lisp I have been working with, it has <, >, and ==
> (structure equality) operators which can take string arguments, number
> arguments, or a mixture of both. But it seems in guile that there are
> separate comparison operators for strings and for numbers. This makes
> sense but is not very convenient for my present purpose. Is there some
> other guile operators or extension operators that will handle both? I
> could make some I'm sure, but I don't want to reinvent the wheel.

(use-modules (oop goops))

(define-method (< (a <string>) . rest)
  (apply string<? a rest))

(< "g" "b") ;; => #f

-- 
David Kastrup




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24  6:05 Comparison operators for strings /and/ numbers? Christopher Howard
  2017-08-24  8:05 ` David Kastrup
@ 2017-08-24  8:29 ` Arne Babenhauserheide
  2017-08-24 10:17   ` Ralf Mattes
  2017-08-24 13:31 ` Chris Vine
  2 siblings, 1 reply; 8+ messages in thread
From: Arne Babenhauserheide @ 2017-08-24  8:29 UTC (permalink / raw)
  To: Christopher Howard; +Cc: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 1173 bytes --]

Hi Christopher,

You can use GOOPS to make them generic:

> (import (oop goops))
> (< "a" "b")
<unnamed port>:3:0: <unnamed port>:3:0: In procedure <: Wrong type argument in position 1: "a"

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
[1]> (define-method (< (a <string>) (b <string>)) (string<? a b))
[1]> (< "a" "b")
$1 = #t

For some functions you might need to call (define-generic <function>)
before this (you’ll notice because define-method will signal an error).

Best wishes,
Arne

Christopher Howard <christopher.howard@qlfiles.net> writes:

> Hi, in another lisp I have been working with, it has <, >, and ==
> (structure equality) operators which can take string arguments, number
> arguments, or a mixture of both. But it seems in guile that there are
> separate comparison operators for strings and for numbers. This makes
> sense but is not very convenient for my present purpose. Is there some
> other guile operators or extension operators that will handle both? I
> could make some I'm sure, but I don't want to reinvent the wheel.

-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24  8:29 ` Arne Babenhauserheide
@ 2017-08-24 10:17   ` Ralf Mattes
  2017-08-24 17:58     ` Arne Babenhauserheide
  0 siblings, 1 reply; 8+ messages in thread
From: Ralf Mattes @ 2017-08-24 10:17 UTC (permalink / raw)
  To: Arne Babenhauserheide; +Cc: Guile User Mailing List

On Thu, Aug 24, 2017 at 10:29:41AM +0200, Arne Babenhauserheide wrote:
> Hi Christopher,
> 
> You can use GOOPS to make them generic:
> 
> > (import (oop goops))
> > (< "a" "b")

That's not what David suggested. Of course you still need to define the
method specialized on strings.

> <unnamed port>:3:0: <unnamed port>:3:0: In procedure <: Wrong type argument in position 1: "a"
> 
> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> [1]> (define-method (< (a <string>) (b <string>)) (string<? a b))
> [1]> (< "a" "b")
> $1 = #t
> 
> For some functions you might need to call (define-generic <function>)
> before this (you’ll notice because define-method will signal an error).

Is that still the case? IIRC this was true for Guile 1 where you needed
to "redeclare" build-in functions as generic, but from what I see Guile2
automatically generates the appropriate generic functions. This is
pretty much how Common Lisp handles defmethod without a matching
defgeneric (but in CL you aren't allowed to transform a function from CL 
into a generic).

Cheers, Ralf Mattes

> Best wishes,
> Arne
> 
> Christopher Howard <christopher.howard@qlfiles.net> writes:
> 
> > Hi, in another lisp I have been working with, it has <, >, and ==
> > (structure equality) operators which can take string arguments, number
> > arguments, or a mixture of both. But it seems in guile that there are
> > separate comparison operators for strings and for numbers. This makes
> > sense but is not very convenient for my present purpose. Is there some
> > other guile operators or extension operators that will handle both? I
> > could make some I'm sure, but I don't want to reinvent the wheel.
> 
> -- 
> Unpolitisch sein
> heißt politisch sein
> ohne es zu merken





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24  6:05 Comparison operators for strings /and/ numbers? Christopher Howard
  2017-08-24  8:05 ` David Kastrup
  2017-08-24  8:29 ` Arne Babenhauserheide
@ 2017-08-24 13:31 ` Chris Vine
  2017-08-24 13:41   ` Chris Vine
  2 siblings, 1 reply; 8+ messages in thread
From: Chris Vine @ 2017-08-24 13:31 UTC (permalink / raw)
  To: guile-user

On Wed, 23 Aug 2017 22:05:37 -0800
Christopher Howard <christopher.howard@qlfiles.net> wrote:
> Hi, in another lisp I have been working with, it has <, >, and ==
> (structure equality) operators which can take string arguments, number
> arguments, or a mixture of both. But it seems in guile that there are
> separate comparison operators for strings and for numbers. This makes
> sense but is not very convenient for my present purpose. Is there some
> other guile operators or extension operators that will handle both? I
> could make some I'm sure, but I don't want to reinvent the wheel.

The predicate equal? will compare numbers, strings and records.

https://www.gnu.org/software/guile/docs/master/guile.html/Equality.html#index-equal_003f

Chris



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24 13:31 ` Chris Vine
@ 2017-08-24 13:41   ` Chris Vine
  0 siblings, 0 replies; 8+ messages in thread
From: Chris Vine @ 2017-08-24 13:41 UTC (permalink / raw)
  To: guile-user

On Thu, 24 Aug 2017 14:31:41 +0100
Chris Vine <vine35792468@gmail.com> wrote:
> On Wed, 23 Aug 2017 22:05:37 -0800
> Christopher Howard <christopher.howard@qlfiles.net> wrote:
> > Hi, in another lisp I have been working with, it has <, >, and ==
> > (structure equality) operators which can take string arguments,
> > number arguments, or a mixture of both. But it seems in guile that
> > there are separate comparison operators for strings and for
> > numbers. This makes sense but is not very convenient for my present
> > purpose. Is there some other guile operators or extension operators
> > that will handle both? I could make some I'm sure, but I don't want
> > to reinvent the wheel.  
> 
> The predicate equal? will compare numbers, strings and records.
> 
> https://www.gnu.org/software/guile/docs/master/guile.html/Equality.html#index-equal_003f

I meant to add, but failed to do so, that as far as I am aware there is
no built-in polymorphic < or > procedure that will take strings as well
as numbers.  As a simple work-around you could use type predicates in a
cond expression to dispatch the appropriate comparison procedure for
the type in question.

Chris



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24 10:17   ` Ralf Mattes
@ 2017-08-24 17:58     ` Arne Babenhauserheide
  0 siblings, 0 replies; 8+ messages in thread
From: Arne Babenhauserheide @ 2017-08-24 17:58 UTC (permalink / raw)
  To: Ralf Mattes; +Cc: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]


Ralf Mattes <rm@seid-online.de> writes:

> On Thu, Aug 24, 2017 at 10:29:41AM +0200, Arne Babenhauserheide wrote:
>> Hi Christopher,
>> 
>> You can use GOOPS to make them generic:
>> 
>> > (import (oop goops))
>> > (< "a" "b")
>
> That's not what David suggested.

No, it is not. I wrote my reply before I saw his.

>> <unnamed port>:3:0: <unnamed port>:3:0: In procedure <: Wrong type argument in position 1: "a"
>> 
>> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
>> [1]> (define-method (< (a <string>) (b <string>)) (string<? a b))
>> [1]> (< "a" "b")
>> $1 = #t
>> 
>> For some functions you might need to call (define-generic <function>)
>> before this (you’ll notice because define-method will signal an error).
>
> Is that still the case?

Yes. Try:

> (import (oop goops))
> (define-method (list (a <string>)))
ERROR: In procedure scm-error:
ERROR: #<procedure list _> is not a valid generic function

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Comparison operators for strings /and/ numbers?
  2017-08-24  8:05 ` David Kastrup
@ 2017-09-19  4:27   ` Christopher Howard
  0 siblings, 0 replies; 8+ messages in thread
From: Christopher Howard @ 2017-09-19  4:27 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1797 bytes --]

On Thu, 2017-08-24 at 10:05 +0200, David Kastrup wrote:
> Christopher Howard <christopher.howard@qlfiles.net> writes:
> 
> > Hi, in another lisp I have been working with, it has <, >, and ==
> > (structure equality) operators which can take string arguments,
> > number
> > arguments, or a mixture of both. But it seems in guile that there
> > are
> > separate comparison operators for strings and for numbers. This
> > makes
> > sense but is not very convenient for my present purpose. Is there
> > some
> > other guile operators or extension operators that will handle both?
> > I
> > could make some I'm sure, but I don't want to reinvent the wheel.
> 
> (use-modules (oop goops))
> 
> (define-method (< (a <string>) . rest)
>   (apply string<? a rest))
> 
> (< "g" "b") ;; => #f
> 

Forgive me for bring this thread back, but I just finished reading the
goops info manual...

My question, specifically: Suppose:

1) I use fn "<" in my module, as a goops generic, and then
2) somebody else "use"s my module in their module, and then
3) suppose they do a "define-method" to create another "<" for a new
datatype
4) they call a function in my module that uses "<"

Will that function call have access to the new method for that generic?

Background: This would obvious be very important in a module providing
a generic data structure (like a binary tree), where you would want to
compare keys using "<", but you wouldn't want to arbitrarily limit the
possible data types to what was defined in the module.

Or do I need to define an <ordered> class and expose that to my module
users? I'm thinking in terms like Data.Ord class from Haskell. (Java
folks would call it an "interface".)

-- 
https://qlfiles.net
https://emailselfdefense.fsf.org/en/

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-09-19  4:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-24  6:05 Comparison operators for strings /and/ numbers? Christopher Howard
2017-08-24  8:05 ` David Kastrup
2017-09-19  4:27   ` Christopher Howard
2017-08-24  8:29 ` Arne Babenhauserheide
2017-08-24 10:17   ` Ralf Mattes
2017-08-24 17:58     ` Arne Babenhauserheide
2017-08-24 13:31 ` Chris Vine
2017-08-24 13:41   ` Chris Vine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).