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

CPPで真偽値とリスト

CPPはC PlusPlusじゃなくてC PreProcessorだよ!

gcc -E nanntoka.c

で実行。

#define par_open (
#define par_close )

#define cat_(a,b) a##b
#define cat(a,b) cat_(a,b)

#define defif_0(t, f) f
#define defif_1(t, f) t
#define defif_(b, t, f) defif_##b(t, f)
#define defif(b, t, f) defif_(b, t, f)

#define defnot(b) defif(b, 0, 1)
#define defand(b1, b2) defif(b1, b2, 0)
#define defor(b1, b2) defif(b1, 1, b2)
#define defxor(b1, b2) defif(b1, defnot(b2), b2)

#define defnil_p_(car, cdr) defnil_p_true_
#define defnil_p_true_val 0
#define defnil_p_val 1
#define defnil_p(list) cat(defnil_p_ list, val)
#define defcar_(car, cdr) car
#define defcar(list) defcar_ list
#define defcdr_(car, cdr) cdr
#define defcdr(list) defcdr_ list

#define testlist (a,(b,(c,)))
defif(defand(1,0), true, false);
defif(defand(1,1), true, false);

testlist;
defnil_p(testlist);
defnil_p(defcdr(testlist));
defnil_p(defcdr(defcdr(testlist)));
defnil_p(defcdr(defcdr(defcdr(testlist))));