Egison 3.5.1 釋出,模式匹配程式語言

edithfang發表於2014-12-30
示例程式碼:

;; Extract all twin primes from the infinite list of prime numbers with pattern-matching!
(define $twin-primes
  (match-all primes (list integer)
    [<join _ <cons $p <cons ,(+ p 2) _>>>
     [p (+ p 2)]]))
  
;; Enumerate first 10 twin primes
(take 10 twin-primes)
;=>{[3 5] [5 7] [11 13] [17 19] [29 31] [41 43] [59 61] [71 73] [101 103] [107 109]}


Egison 3.5.1 釋出,此版本更新內容如下:

移除 loop 表示式到模式外:

(define $list
  (lambda [$a]
    (matcher
      {[,$val []
        {[$tgt (match [val tgt] [(list a) (list a)]
                 {[[<nil> <nil>] {[]}]
                  [[<cons $x $xs> <cons ,x ,xs>] {[]}]
                  [[_ _] {}]})]}]
       [<nil> []
        {[{} {[]}]
         [_ {}]}]
       [<cons [        ubbcodeplace_2        ]nbsp;[        ubbcodeplace_2        ]gt; [a (list a)]
        {[{$x @$xs} {[x xs]}]
         [_ {}]}]
       [<snoc [        ubbcodeplace_2        ]nbsp;[        ubbcodeplace_2        ]gt; [a (list a)]
        {[{@$xs $x} {[x xs]}]
         [_ {}]}]
       [<join [        ubbcodeplace_2        ]nbsp;[        ubbcodeplace_2        ]gt; [(list a) (list a)]
        {[$tgt (match-all tgt (list a)
                 [(loop $i [1 $n] <cons $xa_i ...> $rs) [(foldr (lambda [$i $r] {xa_i @r}) {} (between 1 n))
                                                         rs]])]}]
       [<nioj [        ubbcodeplace_2        ]nbsp;[        ubbcodeplace_2        ]gt; [(list a) (list a)]
        {[$tgt (match-all tgt (list a)
                 [(loop $i [1 $n] <snoc $xa_i ...> $rs) [(foldr (lambda [$i $r] {xa_i @r}) {} (between 1 n))
                                                         rs]])]}]
       [[        ubbcodeplace_2        ]nbsp;[something]
        {[$tgt {tgt}]}]
       })))

陣列從 1 開始

(define $xa [| 1 2 3 4 5 |])
(array-size xa);=>5
xa_1;=>1
xa_5;=>5
xa_8;=>undefined


相關連結

相關閱讀
評論(1)

相關文章