`
lobin
  • 浏览: 425818 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

JScheme

 

 

 
> (do ((i 1 (+ i 1))
     (j 1 (+ j 1))
     (k 1 (+ k 1)))
     ((and (> j 4) (> i 10)  (> i 20)))
    ((lambda
      (x)
      (display x)
      (newline)) j))
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$81 = unspecified
  在上面例子中,do循环内部也是一种调用方式:这里定义了一个匿名函数
(lambda 
      (x)
      (display x)
      (newline))
 接受1个参数,直接输出&换行。
直接将这个匿名函数作为参数,包括函数需要传入的参数传入,这样调用:
 
((lambda 
      (x)
      (display x)
      (newline)) j)
 
这种调用方式等价于:
> (define fx
  (lambda
    (x)
    (display x)
    (newline)))
$86 = (lambda ?? (x)...)

> (fx 100)
100
$87 = #t
 
 
 
> (do ((lst
      (list 1 2 3 4 5 6)
      ))
    ((<= (length lst) 0))
    (display (car lst))
    (newline)
    (set! lst (cdr lst)))
1
2
3
4
5
6
$5 = unspecified
 
 
> (do ((lst
      (list 1 2 3 4 5 6)
      (set! lst (cdr lst))
      ))
    ((<= (length lst) 0))
    (display (car lst))
    (newline))
1
2
3
4
5
6
$6 = unspecified
 
> (define lst (list 1 2 3 4 5 6))
$7 = (1 2 3 4 5 6)

> (do ()
    ((<= (length lst) 0))
    (display (car lst))
    (newline)
    (set! lst (cdr lst)))
1
2
3
4
5
6
$8 = unspecified
 

 

 

 

(define tree (list 0 (list (list #'a' 1 2 3)  ; 0
                    (list #'b' 4 5)    ; 1
                    (list #'c')        ; 2
                    (list #'d' 6)      ; 3
                    (list #'e')        ; 4
                    (list #'f')        ; 5
                    (list #'g' 7 8 9)  ; 6
                    (list #'h')        ; 7
                    (list #'i')        ; 8
                    (list #'j' 10 11 12 13 14 15 16)        ; 9
                    (list #'k')        ; 10
                    (list #'l')        ; 11
                    (list #'m')        ; 12
                    (list #'n')        ; 13
                    (list #'o')        ; 14
                    (list #'p' 17)        ; 15
                    (list #'q')        ; 16
                    (list #'r' 18)        ; 17
                    (list #'s' 19)        ; 18
                    (list #'t' 20 21 22 23 24 25)        ; 19
                    (list #'u')        ; 20
                    (list #'v')        ; 21
                    (list #'w')        ; 22
                    (list #'x')        ; 23
                    (list #'y')        ; 24
                    (list #'z'))))        ; 25

(define iterate 
  (lambda 
    (t)
    (let* 
      ((t0 (car (cdr t))) 
       (i (car t)) 
       (node (list-ref t0 i))) 
      
      
      ;(display (car node))
      ;; search the children of node, which the parent is node, with parent index is i
      (let 
        ((vn (list (list node i))) 
         (nvn (list))) 
        
        (do 
          () 
          ((<= (length vn) 0))
          (map 
            (lambda 
              (x) 
              ;(display "searched node:")
              ;(newline)
              (display (list-ref x 0)) 
              (newline)
              (let 
                ((children (cdr (list-ref x 0))))
                ;(display children)
                (if 
                  (> (length x) 1)
                  (for-each 
                    (lambda 
                      (z) 
                      ;(display z)
                      (set! nvn (cons (list (list-ref t0 z) z) nvn))) 
                    children)))
            ) vn)
            (set! vn (reverse nvn))
            (set! nvn (list))
            ;(display vn) (newline)
          )
        )
      )
    )
  )

 

> (iterate tree)
(a 1 2 3)
(b 4 5)
(c)
(d 6)
(e)
(f)
(g 7 8 9)
(h)
(i)
(j 10 11 12 13 14 15 16)
(k)
(l)
(m)
(n)
(o)
(p 17)
(q)
(r 18)
(s 19)
(t 20 21 22 23 24 25)
(u)
(v)
(w)
(x)
(y)
(z)
$7 = unspecified 

  

 

(define tree (list 0 
               (list (list #'a' -1) 
                 (list #'z' 0) 
                 (list #'c' 0) 
                 (list #'x' 0) 
                 (list #'e' 0) 
                 (list #'f' 0) 
                 (list #'g' 0) 
                 (list #'h' 1) 
                 (list #'i' 1) 
                 (list #'j' 1) 
                 (list #'k' 1) 
                 (list #'l' 2) 
                 (list #'m' 2) 
                 (list #'n' 3) 
                 (list #'o' 3) 
                 (list #'p' 3) 
                 (list #'q' 5) 
                 (list #'r' 6) 
                 (list #'s' 6) 
                 (list #'t' 6) 
                 (list #'u' 6) 
                 (list #'v' 6) 
                 (list #'w' 6) 
                 (list #'d' 6) 
                 (list #'y' 6) 
                 (list #'b' 6))))
                 
(define iterate 
  (lambda 
    (t)
    (let* ((t0 (car (cdr t))) 
           (i (car t)) 
           (node (list-ref t0 i))) 
      
      
      ;(display (car node))
      ;; search the children of node, which the parent is node, with parent index is i
      (let ((vn (list (list node i))) 
            (ii 0)
            (nvn (list))) 
        
        (do () 
          ((<= (length vn) 0))
          (map (lambda (x) 
                 ;(display "searched node:")
                 ;(newline)
                 (display (list-ref x 0)) 
                 (newline)
                 (map (lambda (z) 
                        (if (= (list-ref z 1) (list-ref x 1))
                          (set! nvn (cons (list z ii) nvn)))
                        (set! ii (+ ii 1))) 
                      t0)
                 (set! ii 0)
               ) vn)
          (set! vn (reverse nvn))
          (set! nvn (list))
          ;(display vn) (newline)
          ))
      )))
 
> (iterate tree)
(a -1)
(z 0)
(c 0)
(x 0)
(e 0)
(f 0)
(g 0)
(h 1)
(i 1)
(j 1)
(k 1)
(l 2)
(m 2)
(n 3)
(o 3)
(p 3)
(q 5)
(r 6)
(s 6)
(t 6)
(u 6)
(v 6)
(w 6)
(d 6)
(y 6)
(b 6)
$3 = unspecified
 
 
 
;;
;; (#'a', #'b', #'c', #'d', #'e', #'f', #'g')
;; 
;; (#'a', #'c', 10 )
;; (#'a', #'e', 30 )
;; (#'a', #'f', 100)
;; (#'b', #'c', 5  )
;; (#'c', #'d', 50 )
;; (#'d', #'f', 10 )
;; (#'e', #'d', 20 )
;; (#'e', #'f', 60 )
;; 
(define g 
  (list 
    (list 
      #'a' #'b' #'c' #'d' #'e' #'f' #'g') 
    (list 
      (list #'a' #'c' 10 )
      (list #'a' #'e' 30 )
      (list #'a' #'f' 100)
      (list #'b' #'c' 5  )
      (list #'c' #'d' 50 )
      (list #'d' #'f' 10 )
      (list #'e' #'d' 20 )
      (list #'e' #'f' 60 ))))

(define r
  (lambda
    ;; param map 
    ;; param start represents the start address/node 
    (map start)
    
    ;; handle invalid start address/node.
    (if 
      (not 
        (member 
          start (car map)))
      (begin
        (display 
          (string-append "start address/node: " start " is invalid."))
        (newline)
        
        (throw 
          (IllegalArgumentException. 
            (string-append "start address/node: " start " is invalid.")))))
    (let* 
      ((s (list start))        ;
       (a (car map))           ; addresses/nodes
       (p (list-ref map 1))    ; 
       (dis 
         (let 
           ((t (list)))        ; each of t is a tuple: (dis, s), dis represents the dis between the start address/node and the specific address/node.
           (for-each 
             (lambda 
               (x)
               (display (string-append "x is == " x))
               (newline)
               (if 
                 (.equals start x)
                 
                 ;; 
                 (begin 
                   (set! t 
                     (append t 
                       (list 
                         (list 0 0)))))
                 
                 ;; 
                 (set! t 
                   (append t 
                     (let 
                       ((t0 (list #null)))
                       (for-each 
                         (lambda 
                           (e)
                           (if 
                             (and 
                               (.equals 
                                 (list-ref 
                                   e 0) start) 
                               (.equals 
                                 (list-ref 
                                   e 1) x))
                             (set! t0 
                               (list 
                                 (list 
                                   (list-ref 
                                     e 2) 0)))))
                         p)
                         t0)))))
             a)
           t)))
      (display dis)
      (newline))
      ))
 
> (r g #'a')
x is == a
x is == b
x is == c
x is == d
x is == e
x is == f
x is == g
((0 0) #null (10 0) #null (30 0) (100 0) #null)
$18 = #t
 
 

 

0
0
分享到:
评论

相关推荐

    jscheme:Jscheme(方案(在Java中,由Peter Norvig编写))

    Jscheme(方案(在Java中,由Peter Norvig编写)) 这是在找到的代码。 在第一次提交期间,它已准备好在IntelliJ中执行,并略微格式化为更现代的编码标准。 提交为“首次提交”的所有代码均由Peter Norvig编写,并...

    jscheme:用于浏览器和节点的小型简单对象架构库

    基本用法这是一个最简单的示例,用于验证具有一个必须为字符串的属性的对象: // Add a schema.jScheme . add ( 'person' , { name : 'string'} ) ;// Validate a valid object against our first schema.jScheme . ...

    JSchemeMin是一个JVM平台上的Scheme语言实现

    JSchemeMin 是一个JVM平台上的Scheme语言实现。 作为R7RS的实现,JSchemeMin支持Scheme的所有标准特性,包括头等公民地位的过程、尾递归优化、继续、用户定义记录、库(包括R7RS附录A中全部语法和过程,不只base)、...

    The Jscheme Web Programming Project-开源

    在这个项目中,我们旨在开发用于开发各种Web应用程序(尤其是Servlet和基于xml的Web服务)的方案库。 我们的方法是使用jscheme(Java方案的开源实现)作为核心语言,

    EvolvingTheJavaPlatform-OlaBini.pdf

    Java 平台的影响,包括 Hecl、Jacl、Clojure、Ync/Javascript、Joy、Jv-language、CAL、Aardappel、Funnel、MiniPLAN、SixxBDC Scheme、ABCL、Lisp、PS3i、HotScheme、webLISP、Jaja、JScheme、Skij、Kawauts、...

    zClipse-开源

    zClipse是一个用于构建Eclipse(http://www.eclipse.org)插件的项目。 当前正在开发的插件包括:TabNavigator-编辑器增强; JScheme-方案整合; Insectivore-共享的TeamTasks,与Tasks视图集成。

Global site tag (gtag.js) - Google Analytics