unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* About srfi-58
@ 2012-09-21  8:32 nalaginrut
  2012-09-21 15:45 ` Mark H Weaver
  0 siblings, 1 reply; 3+ messages in thread
From: nalaginrut @ 2012-09-21  8:32 UTC (permalink / raw)
  To: guile-devel

hi guys!
I checked out the slib and realized most of the part of slib we do have
it in our core/modules. 
Unfortunately, "prime" is not in the feature list of slib when I run
slib:feature. But I need it, then I try to port it to Guile directly.

And I encountered something "A:fixN32b" can't be found. I think this
"prime" needs srfi-58. But I remember Guile has it's own array
notations, srfi-58 will introduce another one.

So let me raise a topic about the proper implementation of srfi-58. If
we have something useful in this thread, we may do it.
In my opinion, we have to add a global variable to check if the user
used srfi-58, and scm_read_array could call a brand new
scm_i_srfi_58_read_array if it's true. 
But I don't think introduce a global variable is a good way. Maybe add
new read-opts is better? And if we do it in the C level, it's not so
interesting to implement the array parser. 

Anyone provide better solution? Any other consideration about srfi-58 is
welcome. ;-)





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

* Re: About srfi-58
  2012-09-21  8:32 About srfi-58 nalaginrut
@ 2012-09-21 15:45 ` Mark H Weaver
  2012-09-24  2:18   ` nalaginrut
  0 siblings, 1 reply; 3+ messages in thread
From: Mark H Weaver @ 2012-09-21 15:45 UTC (permalink / raw)
  To: nalaginrut; +Cc: guile-devel

On 09/21/2012 04:32 AM, nalaginrut wrote:
> hi guys!
> I checked out the slib and realized most of the part of slib we do have
> it in our core/modules.
> Unfortunately, "prime" is not in the feature list of slib when I run
> slib:feature. But I need it, then I try to port it to Guile directly.

If all you need is a probabilistic primality test, here's a simple 
implementation of the Miller-Rabin test:

(set! *random-state* (random-state-from-platform))

(define* (prime? n #:key (trials 100))
   (define n-1 (- n 1))
   (define (composite-by-witness? a)
     (let loop ((b (/ n-1 2)))
       (and (not (= (modulo-expt a b n) n-1))
            (if (odd? b)
                (not (= (modulo-expt a b n) 1))
                (loop (/ b 2))))))
   (or (= n 2)
       (and (> n 2)
            (odd? n)
            (let loop ((trials trials))
              (or (zero? trials)
                  (and (not (composite-by-witness? (+ 1 (random n-1))))
                       (loop (- trials 1))))))))

      Mark



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

* Re: About srfi-58
  2012-09-21 15:45 ` Mark H Weaver
@ 2012-09-24  2:18   ` nalaginrut
  0 siblings, 0 replies; 3+ messages in thread
From: nalaginrut @ 2012-09-24  2:18 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-devel

hi Mark!
Thanks for reply!
The prime algorithm is good to me. :-)

But do you think we may take this chance to add srfi-58 incidentally?

On Fri, 2012-09-21 at 11:45 -0400, Mark H Weaver wrote: 
> On 09/21/2012 04:32 AM, nalaginrut wrote:
> > hi guys!
> > I checked out the slib and realized most of the part of slib we do have
> > it in our core/modules.
> > Unfortunately, "prime" is not in the feature list of slib when I run
> > slib:feature. But I need it, then I try to port it to Guile directly.
> 
> If all you need is a probabilistic primality test, here's a simple 
> implementation of the Miller-Rabin test:
> 
> (set! *random-state* (random-state-from-platform))
> 
> (define* (prime? n #:key (trials 100))
>    (define n-1 (- n 1))
>    (define (composite-by-witness? a)
>      (let loop ((b (/ n-1 2)))
>        (and (not (= (modulo-expt a b n) n-1))
>             (if (odd? b)
>                 (not (= (modulo-expt a b n) 1))
>                 (loop (/ b 2))))))
>    (or (= n 2)
>        (and (> n 2)
>             (odd? n)
>             (let loop ((trials trials))
>               (or (zero? trials)
>                   (and (not (composite-by-witness? (+ 1 (random n-1))))
>                        (loop (- trials 1))))))))
> 
>       Mark





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

end of thread, other threads:[~2012-09-24  2:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-21  8:32 About srfi-58 nalaginrut
2012-09-21 15:45 ` Mark H Weaver
2012-09-24  2:18   ` nalaginrut

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