Help us understand the problem. What is going on with this article?

Guile SchemeでClojure的なマクロと遅延シーケンス

More than 3 years have passed since last update.

ほぼ初めて書いたSchemeのコード
https://gist.github.com/emanon-was/062e65268f91847aecd2

シーケンスでもなんでもないし細々違うところはあるものの
そこそこ似たような動作出来てるはず?

パフォーマンスは考えていない

(define-module (clojure core)
  #:use-module (ice-9 format)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-45)
  #:use-module (oop goops)
  #:replace    (map filter)
  #:export     (-> ->> doto loop str println time slot lazy-seq seq first rest nth take drop dorun doall doseq range fib-seq))

(define-syntax ->
  (syntax-rules ()
    ((_ x) x)
    ((_ x (form more ...)) (form (-> x) more ...))
    ((_ x form) (form (-> x)))
    ((_ x form more ...) (-> (-> x form) more ...))))

(define-syntax ->>
  (syntax-rules ()
    ((_ x) x)
    ((_ x (form more ...)) (form more ... (->> x)))
    ((_ x form) (form (->> x)))
    ((_ x form more ...) (->> (->> x form) more ...))))

(define-syntax doto
  (syntax-rules ()
    ((_ x) x)
    ((_ x form) (begin (-> x form) x))
    ((_ x form more ...) (doto (doto x form) more ...))))

(define-macro (loop binds . forms)
  `(let recur ,binds . ,forms))

(define-syntax str
  (syntax-rules ()
    ((_ form ...)
     (format #f "~{~a~}" (list form ...)))))

(define (println _)
  (display _)
  (newline))

(define (current-micro-time)
  (let ((datetime (gettimeofday)))
    (+ (* 1000000 (car datetime)) (cdr datetime))))

(define-syntax time
  (syntax-rules ()
    ((_ exp)
     (let* ((start (current-micro-time))
            (result exp)
            (end (current-micro-time)))
       (println (str "Elapsed time: " (/ (- end start) 1000.0) " msecs"))
       result))))

(define-syntax slot
  (syntax-rules ()
    ((_ x (key value)) (begin (slot-set! x 'key value) x))
    ((_ x (key value) more ...) (slot (slot x (key value)) more ...))
    ((_ x key) (slot-ref x 'key))
    ((_ x key more ...) (cons (slot x key) (slot x more ...)))))

(define-class <lazy-seq> (<pair>)
  (ref #:init-value '()
       #:init-keyword #:ref))

(define-syntax lazy-seq
  (syntax-rules ()
    ((_ exp) (make <lazy-seq> #:ref (delay exp)))))

(define (force-seq _)
  (let ((data (slot _ ref)))
    (if (promise? data)
        (let ((result (force data)))
          (begin (slot _ (ref result))
                 result))
        data)))

(define-method (seq (_ <null>)) _)
(define-method (seq (_ <pair>)) _)
(define-method (seq (_ <lazy-seq>)) (force-seq _))

(define-method (first (_ <null>)) _)
(define-method (first (_ <pair>)) (car _))
(define-method (first (_ <lazy-seq>)) (car (seq _)))

(define-method (rest (_ <null>)) _)
(define-method (rest (_ <pair>)) (cdr _))
(define-method (rest (_ <lazy-seq>)) (cdr (seq _)))

(define (nth n coll)
  (if (= n 0)
      (first coll)
      (nth (- n 1) (rest coll))))

(define (map f coll)
  (lazy-seq
   (if (null? (seq coll))
       '()
       (cons (f (first coll))
             (map f (rest coll))))))

(define (filter pred coll)
  (lazy-seq
   (loop ((pred pred) (coll coll))
         (if (null? (seq coll))
             '()
             (let ((head (first coll)))
               (if (pred head)
                   (cons head (filter pred (rest coll)))
                   (recur pred (rest coll))))))))

(define (take n coll)
  (lazy-seq
   (if (or (= n 0) (null? (seq coll)))
       '()
       (cons (first coll) (take (- n 1) (rest coll))))))

(define (drop n coll)
  (if (= n 0)
      coll
      (drop (- n 1) (rest coll))))

(define (dorun:all coll)
  (if (null? (seq coll))
      '()
      (dorun:all (rest coll))))

(define (dorun:opt n coll)
  (if (or (= n 0) (null? (seq coll)))
      '()
      (dorun:opt (- n 1) (rest coll))))

(define-syntax dorun
  (syntax-rules ()
    ((_ coll) (dorun:all coll))
    ((_ n coll) (dorun:opt n coll))))

(define-syntax doseq
  (syntax-rules ()
    ((_ ((bind seq)) forms ...)
     (dorun (map (lambda (bind) forms ...) seq)))
    ((_ ((bind seq) more ...) forms ...)
     (doseq ((bind seq))
            (doseq (more ...) forms ...)))))

(define (doall coll)
  (loop ((coll coll) (acc '()))
        (if (null? (seq coll))
            (reverse! acc)
            (recur (rest coll) (cons (first coll) acc)))))

(define (range:all)
  (loop ((start 0))
        (lazy-seq (cons start (recur (+ 1 start))))))

(define (range:opt start end skip)
  (lazy-seq
   (loop ((start start) (end end) (skip skip))
         (if (or (and (< 0 skip) (> start end))
                 (and (> 0 skip) (< start end)))
             '()
             (cons start (range:opt (+ start skip) end skip))))))

(define-syntax range
  (syntax-rules ()
    ((_)
     (range:all))
    ((_ num)
     (range 0 num 1))
    ((_ start end)
     (if (< start end)
         (range start end 1)
         (range start end -1)))
    ((_ start end skip)
     (range:opt start end skip))))

(define (fib)
  (loop ((x 0) (y 1))
        (lazy-seq (cons x (recur y (+ x y))))))

;;
;; (time (->> (fib) (nth 100)))
;;
;; Elapsed time: 0.605 msecs
;; => 354224848179261915075
;;

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした