-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.lisp
More file actions
57 lines (45 loc) · 1.72 KB
/
common.lisp
File metadata and controls
57 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;; Copyright (C) 2014 Wang Jing
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; Author: wangjild <wangjild@gmail.com>
;; Blog: http://www.liloke.com/
;; Github: https://github.com/wangjild/sicp
;; File: common.lisp
;; Lauguage: common lisp
;; Date: 14-04-14 12:08:23
;; Descripton:
(defun accumulate (op initial seque)
(if (null seque)
initial
(funcall op (car seque) (accumulate op initial (cdr seque)))))
(defun accumulate-n (op init seqs)
(if (null (car seqs))
nil
(cons (accumulate op init (map 'list #'car seqs))
(accumulate-n op init (map 'list #'cdr seqs)))))
(defun enumerate-interval (low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(defun flatmap (proc seq)
(accumulate #'append nil (map 'list proc seq)))
(defun primep (x)
(let ((sqrt-x (sqrt x)))
(do ((i 2 (1+ i)))
((> i sqrt-x) t)
(when (eq (mod x i) 0)
(return nil)))))
(defun filter (predicate seq)
(cond ((null seq) nil)
((funcall predicate (car seq))
(cons (car seq)
(filter predicate (cdr seq))))
(t (filter predicate (cdr seq)))))