#!/usr/bin/guile -s
!#
(define func (lambda (a)
	(if(< a 10) 
		(begin (display a)(newline)(set! a (+ a 1))(func a))
	)
))
(func 0)
 
$ ./test.txt
0
1
2
3
4
5
6
7
8
9
 
;コメント 
(display '(1 2 3 4 "test"))
(newline)
(display (+ 1 2 3 4))
(newline)
(display (+ 1/2 2/1))
 
$ ./test.txt
(1 2 3 4 test)
10
5/2
 
(define abc 4)  ;変数 abc を宣言し 4 で初期化
(set! abc 5)    ;変数 abc の値を変更
(display abc)
 
(define func (lambda (a b)(+ a b)))
(display (func 19 20))
 
(define (func a b)(+ a b))
(display (func 19 20))
 
(let ((変数名 0))
	変数の有効範囲
)
 
変数を宣言した括弧の中のみ有効なローカル変数を作成できます
(let ((a 1)(b 2)) ;ローカル変数 a bを宣言
(begin (display (+ a b))(newline)))
(let ((a 3)(b 4)) ;ローカル変数 a bを宣言
(set! a 5)        ;ローカル変数 a の値を変更
(begin (display (+ a b))(newline)))
 
(display (if(< 1 2) "true" "false"))
 
(display (if #f "true" "false"))
 
(= 1 1)
(> 1 2)
(< 1 2)
(>= 1 2)
(<= 1 2)
(not (= 1 1))	;notを行う事により実行結果を反転します
 
(display (begin (+ 1 1)(+ 2 2)(+ 3 3)))
 
;0から9までループ
(define loop (lambda (a)
	(if(< a 10) 
		(begin (display a)(newline)(loop (+ a 1)))
	)
))
(loop 0)
 
;0から9までループ
(let loop ((a 0))
	(if (< a 10) (begin (display a)(newline)(loop (+ a 1)))
))
 
$ ./test.txt
0
1
2
3
4
5
6
7
8
9
 
(define ar (vector "a" "b" "c" "d" "e"))
(display ar)
 
(define ar #("a" "b" "c" "d" "e"))
(display ar)
 
$ ./test.txt
#(a b c d e)
 
(define ar (make-vector 5 "x"))
(display ar)
 
$ ./test.txt
#(x x x x x)
 
(define ar (vector "a" "b" "c" "d" "e"))
(display (vector-length ar))
 
(define ar (vector "a" "b" "c" "d" "e"))
(display (vector-ref ar 4))
 
(define ar (vector "a" "b" "c" "d" "e"))
(vector-set! ar 2 "x")
(display ar)
 
$ ./test.txt
#(a b x d e)
 
(define ar #("a" "b" "c" "d"))	;コピー元
(define ar2 (make-vector 10 "x"))	;コピー先
(vector-move-left! ar 1 3 ar2 2)
(display ar2)
 
$ ./test.txt
#(x x b c x x x x x x)
 
上記プログラムではコピー元ベクターの1〜3の要素を、コピー先の2個目の要素から順にコピーしています
;ベクターからリストに変換
(display (vector->list #("a" "b" "c" "d" "e")))
(newline);改行
;リストからベクターに変換
(display (list->vector (list "a" "b" "c" "d" "e")))
 
$ ./test.txt
(a b c d e)
#(a b c d e)
 
(define i 100)
(define (func a b)(+ a b))
(let ((j 0)) ;ローカル変数jを作成し、iとfuncを入れ替える
	(set! j func)
	(set! func i)
	(set! i j)
)
(display (i 19 20))
(newline)
(display func)
 
(define (func a b)(+ a b))
(define (func2 a b)(- a b))
(define ar (vector "a" "b" func2))
(vector-set! ar 1 func)
(display (vector-ref ar 0))
(newline)
(display ((vector-ref ar 1) 19 20))
(newline)
(display ((vector-ref ar 2) 19 20))
 
((lambda(i)(+ i 1)) 2)
 
((lambda(i j)(+ i j)) 2 3)
 
(define ar (vector (lambda(i j)(+ i j)) (lambda(i j)(- i j))))
(display ((vector-ref ar 0) 2 3))
(newline)
(display ((vector-ref ar 1) 2 3))
 
(define func (lambda (a)
	(a 2 3)
))
(display (func (lambda(i j)(+ i j))))
 (cons 要素A 要素B) '(要素A . 要素B) (car (cons 要素A 要素B)) (cdr (cons 要素A 要素B)) (set-car! x 要素)
(set-cdr! x 要素) 
(display (cons "car" "cdr"))
(newline)
(display (car (cons "car" "cdr")))	;carを参照して表示
(newline)
(display (cdr (cons "car" "cdr")))	;cdrを参照して表示
(newline)
(define x '(0 . 0))
(set-car! x "car")	;carを変更
(set-cdr! x "cdr")	;cdrを変更
(display x)
 
$ ./test.txt
(car . cdr)
car
cdr
(car . cdr)
 
(define x '(((1 . 2) . (3 . 4)) . ((5 . 6) . (7 . 8))) )
(display (car x))		;実行結果 → ((1 . 2) 3 . 4) 
(newline)
(display (cdr x))		;実行結果 → ((5 . 6) 7 . 8) 
(newline)
(display (cadr x))		;実行結果 → (5 . 6) 
(newline)
(display (cdar x))		;実行結果 → (3 . 4) 
(newline)
(display (cdaar x))	;実行結果 → 2 
(newline)
(display (caddr x))	;実行結果 → 7 
(newline)
(set-car! (cadr x) "test")	;参照されているcons pairを修正 
(display x)		;実行結果 → (((1 . 2) 3 . 4) (test . 6) 7 . 8) 
 
(list 1 2 3)
 
'(1 2 3)
 
'(1 2 3) → '(1 .(2 .(3 . ())))
 
'(1 2 3 . 4)
 
'(1 2 3 . 4) → '(1 .(2 .(3 . 4)))
 
(display '(1 2 3))
(newline)
(display '(1 .(2 .(3 . ()))))
(newline)
(display '(1 2 3 . 4))
(newline)
(display '(1 .(2 .(3 . 4))))
(newline)
 
$ ./test.txt
(1 2 3)
(1 2 3)
(1 2 3 . 4)
(1 2 3 . 4)
 
(display (map - '(1 2 3)))		;実行結果 → (-1 -2 -3) 
(display (map + '(1 2 3) '(1 2 3)))	;実行結果 → (2 4 6) 
 
(for-each display '("a" "b" "c" "d"))	;実行結果 → abcd 
 
(display (length '("a" "b" "c" "d")))	;実行結果 → 4 
 
(display (list-ref '("a" "b" "c" "d") 2))	;実行結果 → c 
 
(define x '("a" "b" "c" "d"))
(list-set! x 2 "test")
(display x)	;実行結果 → (a b test d) 
 
(display (append '("a" "b" "c" "d") '(1 2)))	;実行結果 → (a b c d 1 2) 
 
(display (reverse '("a" "b" "c" "d")))	;実行結果 → (d c b a) 
 
(define x '("a" "b" "c" "d"))
(display (delete "b" x))	;実行結果 → (a c d) 
(display x)		;実行結果 → (a b c d) 
(display (delete! "b" x))	;実行結果 → (a c d) 
(display x)		;実行結果 → (a c d) 
 
(define x '("a" "b" "c" "d"))
(define i 0)
(set! i x)
(delete! "b" x)
(display i)	;実行結果 → (a c d) 
 
(define x '("a" "b" "c" "d"))
(define i 0)
(set! i (list-copy x))
(delete! "b" x)
(display i)	;実行結果 → (a b c d) 
 
(define x '(1 2 3 4))
(let loop ((i x))
	(if (not (equal? i '())) (begin (display (car i))(newline)(loop (cdr i)))
))
 
(display (+ 10 20 30))		;実行結果 → 60 
(display (+ . (10 . (20 . (30)))))	;実行結果 → 60 
 
(define x (list + 10 20 30))
(display (eval x (interaction-environment)))
 
(display (eval-string "(+ 1 2)"))
 
(display "start\n")
(call/cc (lambda (name1)	;※ ジャンプ先
(begin
(display "1\n")
(display "2\n")
(display "3\n")
(display "4\n")
(name1 0)			;name1が呼ばれることにより処理が中断され※にジャンプする
(display "5\n")
(display "6\n")
)
))
(display "end\n")
 
$ ./test.txt
start
1
2
3
4
end
 
1〜4までの数字しか表示されず、処理が中止されることが確認できます
(use-modules (ice-9 readline))
(activate-readline)
 
(display (readline))
 ▲トップページ 
 > 
その他