読者です 読者をやめる 読者になる 読者になる

Gauche のリスト処理手続き : 1

gauche

呼称: リストの基本操作
目的: Gauche のリストの代表的な操作を学ぶ

#!/usr/local/bin/gosh
;; -*- coding: utf-8 -*-

;;; basic list operation

;; make list
(print "using quote : " '(1 2 3))
(print "using list  : " (list 1 2 3))

;; list operations
(print "car  : " (car '(1 2 3)))
(print "cdr  : " (cdr '(1 2 3)))
(print "cons : " (cons 1 '(2 3)))

;; make dotted pair
(print "dotted pair1 : " (cons 1 2))
(print "dotted pair2 : " '(1 . 2))

;; make dotted list
(print "dotted list : " (cons 1 (cons 2 3)))

;; make proper list
(print "proper list1 : " '(1 . ()))
(print "proper list2 : " (cons 1 ()))

;; null? procedure
(print "null? 1 : " (null? '(1 2 3)))
(print "null? 2 : " (null? '()))
(print "null? 3 : " (null? 3))

;; pair? procedure
(print "pair? 1 : " (pair? '(1 2 3)))
(print "pair? 2 : " (pair? '()))
(print "pair? 3 : " (pair? 3))

;; confirm proper list
(define (list? obj)
  (or (null? obj) (and (pair? obj) (list? (cdr obj)))))

(print "list? 1 : " (list? '()))
(print "list? 2 : " (list? '(1 . ())))
(print "list? 3 : " (list? '(1 . 2)))

;; eval procedure
(print "eval : " (eval '(+ 1 2) (interaction-environment)))

実行結果。

using quote : (1 2 3)
using list  : (1 2 3)
car  : 1
cdr  : (2 3)
cons : (1 2 3)
dotted pair1 : (1 . 2)
dotted pair2 : (1 . 2)
dotted list : (1 2 . 3)
proper list1 : (1)
proper list2 : (1)
null? 1 : #f
null? 2 : #t
null? 3 : #f
pair? 1 : #t
pair? 2 : #f
pair? 3 : #f
list? 1 : #t
list? 2 : #t
list? 3 : #f
eval : 3
広告を非表示にする