wpaisb-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wpaisb-devel] Persistenza ortogonale e checkpointing: il codice


From: Luca Saiu
Subject: [Wpaisb-devel] Persistenza ortogonale e checkpointing: il codice
Date: Tue, 03 Oct 2006 10:08:25 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060915)

Salute. Non ho tempo per commentare adesso, ma nell'attesa vi mando un
po' di codice divertente che ho scritto stanotte.

#!/usr/bin/guile -s
!#
;; This file is part of "Web programming as it should be", a distributed
;; programming and web programming framework.
;;
;; Copyright (C) 2006 Luca Saiu
;;
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 2, or (at your
;; option) any later version.
;;
;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this software; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301  USA


;; The call/cc alias is nearly universal:
(define call/cc
  call-with-current-continuation)

;; This will be set to a continuation as soon as a resumable computation
;; saves its state:
(define resume
  #f)

(define-macro (check-point!)
  "Save the current continuation into the global resume"
  `(call/cc (lambda (k) (set! resume k))))

(define (for-all? predicate? list)
  "Return true iff the given predicate is true for all the elements of the
given list. The predicate is called on the elements of the list in order,
and evaluation is stopped at the first element not satisfying the predicate"
  (cond ((null? list)            #t)
        ((predicate? (car list)) (for-all? predicate? (cdr list)))
        (else                    #f)))

(define (prime-with-respect-to? candidate known-primes)
  "Return true iff the given number is not divisible for any number in the
given list"
  (for-all? (lambda (prime)
              (not (zero? (modulo candidate prime))))
            known-primes))

(define (write-primes)
  "Write the list of prime numbers. This procedure can be interrupted at
any time with a SIGINT, and its execution can be resumed later by calling
resume with no parameters"
  (define (write-primes next-candidate known-primes)
    (if (prime-with-respect-to? next-candidate known-primes)
        (begin
          (display next-candidate) (display #\space) ;;(newline)
          (check-point!) ; it seems slow because we call it too often...
          (write-primes (+ next-candidate 2)
                        (cons next-candidate known-primes)))
        (write-primes (+ next-candidate 2)
                      known-primes)))
  (write-primes 3 '(2)))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]