root/lang/ruby/nendo/trunk/init.nnd @ 32307

Revision 32307, 1.3 kB (checked in by kiyoka, 4 years ago)

Added init.nnd loading feature.
Added sample code.
Fixed some bugs.

Line 
1;;-*- mode: scheme; syntax: scheme -*-;;
2
3;; ruby's p like function.
4(define p
5  (lambda (x)
6    (printf "%s\n" x)
7    x))
8
9;; ----------------------------------------
10;; Higher-order functions
11;; ----------------------------------------
12(define map
13  (lambda (pred lst)
14    (if (null? lst)
15        '()
16        (cons
17         (pred (car lst))
18         (map pred (cdr lst))))))
19(define foreach map)
20
21(define filter
22  (lambda (pred lst)
23    (if (null? lst)
24        '()
25        (let ((result  (pred (car lst))))
26          (if result
27              (cons
28               result
29               (filter pred (cdr lst)))
30              (filter pred (cdr lst)))))))
31
32
33;; ----------------------------------------
34;; List Utility functions
35;; ----------------------------------------
36 
37
38;;(define (iota num)
39;;  (begin
40;;    (def _iota
41;;         (lambda
42;;             (num)
43;;           
44;;           (if (<= num 0)
45;;               '()
46;;               (cons
47;;                num           
48;;                (_iota (- num 1))))))
49;;    (reverse (_iota (num))))))
50
51
52
53;; test code
54;; (filter pass '(1))
55;; (filter pass '(1 2 true false nil 3 a b c)))
56
57;; ----------------------------------------
58;; Utility function for testing
59;; ----------------------------------------
60;; pass through the argument value as return value.
61(define pass
62  (lambda (x)
63    x))
64
Note: See TracBrowser for help on using the browser.