;; require LazyList ;;(define list-ls-from (ocaml" ;;fun n l -> lazy (if n = l then LazyList.Nil else (LazyList.Cons(n, (from (n + 1) l)))) ;;")) (define-macro list-lc (lambda expr (letrec ( (ifbodycore (lambda (src tmp) (let ((hd (car src)) (tl (cdr src))) (if (eq? (car hd) 'if) (ifbodycore (cdr src) (cons (cadr hd) tmp)) (if (> (length tmp) 1) (cons 'and (reverse tmp)) (car tmp)) ) )) ) (ifbody (lambda (src sym tmp) (let ((hd (car src)) (tl (cdr src))) (if (eq? (car hd) 'if) `(LazyList.filter (lambda (,sym) ,(ifbodycore src '())) ,tmp) tmp ) ) ) ) (count-for (lambda (src) (letrec ((_loop (lambda (ls n) (if (null? ls) n (let ((hd (car ls)) (tl (cdr ls))) (_loop tl (if (and (pair? hd) (or (eq? (car hd) 'for) (eq? (car hd) 'for-down) (eq? (car hd) 'map) ) ) (+ n 1) n) ))) ))) (_loop src 0)))) (genlast (lambda (src) (if (null? (cdr src)) (car src) (genlast (cdr src)) ) )) (fcount (count-for expr)) (genbody (lambda (src sym count) (let ( (hd (car src)) (tl (cdr src)) ) (if (null? tl) hd (cond ( (or (eq? (car hd) 'for) (eq? (car hd) 'for-down) (eq? (car hd) 'map) ) (let ((code `(LazyList.map (lambda (,(cadr hd)) ;;,(if (= count 1) ;; (genlast tl) ,(genbody tl (cadr hd) (- count 1)) ;; ) ) ,(ifbody tl (cadr hd) (if (eq? (car hd) 'map) (caddr hd) (cond ((= (length hd) 3) `(,(if (eq? (car hd) 'for) 'LazyListEtc.list_lc_from 'LazyListEtc.list_lc_from_down) ,(caddr hd) 1) ) ((= (length hd) 4) `(,(if (eq? (car hd) 'for) 'LazyListEtc.list_lc_range 'LazyListEtc.list_lc_range_down) ,(caddr hd) ,(cadddr hd) 1) ) ((= (length hd) 5) `(,(if (eq? (car hd) 'for) 'LazyListEtc.list_lc_range 'LazyListEtc.list_lc_range_down) ,(caddr hd) ,(cadddr hd) ,(cadddr (cdr hd))) ) (#t (error (string-append "list-lc syntax error: " (write-to-string hd))) ) ) ) ) ))) (if (= count 1) code `(LazyList.concat ,code) )) ) ((eq? (car hd) 'if) (genbody tl sym count) ) (#t (error hd) ))) ) )) (body (genbody expr #f fcount)) ) body ) )) (define-syntax lazy-list-int (syntax-rules (..) ((_ v1 v2 ..) (if (< v1 v2) (LazyListEtc.list_lc_from v1 (- v2 v1)) (LazyListEtc.list_lc_from_down v1 (- v1 v2)) ) ) ((_ v1 ..) (LazyListEtc.list_lc_from v1 1) ) ((_ v1 v2 .. v3) (if (< v1 v2) (LazyListEtc.list_lc_range v1 v3 (- v2 v1)) (LazyListEtc.list_lc_range_down v1 v3 (- v1 v2)) ) ) ((_ v1 .. v2) (if (< v1 v2) (LazyListEtc.list_lc_range v1 v2 1) (LazyListEtc.list_lc_range_down v1 v2 1) ) ) ((_) (lazy LazyList.Nil) ) ((_ v1) (lazy (LazyList.Cons (tuple v1 (lazy LazyList.Nil)))) ) ((_ v1 v2 ...) (lazy (LazyList.Cons (tuple v1 (lazy-list-int v2 ...)))) ) )) (define-syntax lazy-list-float (syntax-rules (..) ((_ v1 v2 ..) (if (< v1 v2) (LazyListEtc.list_lc_from_float v1 (-. v2 v1)) (LazyListEtc.list_lc_from_down_float v1 (-. v1 v2)) ) ) ((_ v1 ..) (LazyListEtc.list_lc_from_float v1 1.0) ) ((_ v1 v2 .. v3) (if (< v1 v2) (LazyListEtc.list_lc_range_float v1 v3 (-. v2 v1)) (LazyListEtc.list_lc_range_down_float v1 v3 (-. v1 v2)) ) ) ((_ v1 .. v2) (if (< v1 v2) (LazyListEtc.list_lc_range_float v1 v2 1.0) (LazyListEtc.list_lc_range_down_float v1 v2 1.0) ) ) ((_) (lazy LazyList.Nil) ) ((_ v1) (lazy (LazyList.Cons (tuple v1 (lazy LazyList.Nil)))) ) ((_ v1 v2 ...) (lazy (LazyList.Cons (tuple v1 (lazy-list-float v2 ...)))) ) )) (define-syntax lazy-list (syntax-rules () ((_) (lazy LazyList.Nil) ) ((_ v1) (lazy (LazyList.Cons (tuple v1 (lazy LazyList.Nil)))) ) ((_ v1 v2 ...) (lazy (LazyList.Cons (tuple v1 (lazy-list v2 ...)))) ) )) (define-syntax lazy-list-cons (syntax-rules () ((_ v1 v2) (lazy (LazyList.Cons (tuple v1 v2))) ) ;;((_ v1 v2 ...) ;; (lazy (LazyList.Cons (tuple v1 (lazy-list v2 ...)))) ;; ) )) (module LazyListEtc (struct (define list_lc_take_list (ocaml " fun begin_num end_num l -> (let rec _take = (fun i src tmp -> (if i >= end_num then (List.rev tmp) else (_take (i + 1) (LazyList.tl src) (if i >= begin_num then ((LazyList.hd src) :: tmp) else tmp) ) ) ) in (_take 0 l []) ) ")) (define list_lc_range (ocaml " fun begin_num end_num step -> (let rec _from = (fun i -> lazy (if i > end_num then LazyList.Nil else (LazyList.Cons(i, (_from (i + step))))) ) in (_from begin_num) ) ")) (define list_lc_from (ocaml " fun begin_num step -> (let rec _from = (fun i -> lazy (LazyList.Cons(i, (_from (i + step)))) ) in (_from begin_num) ) ")) (define list_lc_range_down (ocaml " fun begin_num end_num step -> (let rec _from = (fun i -> lazy (if i < end_num then LazyList.Nil else (LazyList.Cons(i, (_from (i - step))))) ) in (_from begin_num) ) ")) (define list_lc_from_down (ocaml " fun begin_num step -> (let rec _from = (fun i -> lazy (LazyList.Cons(i, (_from (i - step)))) ) in (_from begin_num) ) ")) (define list_lc_range_float (ocaml " fun begin_num end_num step -> (let rec _from = (fun i -> lazy (if i > end_num then LazyList.Nil else (LazyList.Cons(i, (_from (i +. step))))) ) in (_from begin_num) ) ")) (define list_lc_from_float (ocaml " fun begin_num step -> (let rec _from = (fun i -> lazy (LazyList.Cons(i, (_from (i +. step)))) ) in (_from begin_num) ) ")) (define list_lc_range_down_float (ocaml " fun begin_num end_num step -> (let rec _from = (fun i -> lazy (if i < end_num then LazyList.Nil else (LazyList.Cons(i, (_from (i -. step))))) ) in (_from begin_num) ) ")) (define list_lc_from_down_float (ocaml " fun begin_num step -> (let rec _from = (fun i -> lazy (LazyList.Cons(i, (_from (i -. step)))) ) in (_from begin_num) ) ")) ))