packages feed

egison-3.2.14: sample/sequence.egi

;;;
;;;
;;; Pattern-matching against sequence of natural numbers
;;;
;;;

; first 100 natural numbers
(test (take 100 nats))

; inifinite list of two-combinations of numbers
(define $pairs (match-all nats (list integer)
                 [<join _ (& <cons $m _> <join _ <cons $n _>>)>
                  [m n]]))

; test 'pairs'
(test (take 100 pairs))

; infinite list of pythagoras numbers
(define $pyths (map (lambda [$m $n] (+ (power m 2) (power n 2))) pairs))

; test 'pyths'
(test (take 100 pyths))

; function that enumerates all common elements between two lists
(define $commons
  (lambda [$xs $ys]
    (match-all [xs ys] [(multiset integer) (multiset integer)]
      [[<cons $c _> <cons ,c _>]
       c])))

; test 'commons'
(test (commons (take 100 primes) (take 300 pyths)))