--- title: '8章: 真偽値と判断' author: kazu634 date: 2008-08-05 url: /2008/08/05/_1066/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4191;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - gauche - Lisp ---

勉強の続きだよ。

型を判定する手続き

性質を判定する手続き

not

否定だよ。

gosh> (not (= 2 3))
#t
gosh> (not (= 2 2))
#f

条件を判断するcase手続き

select caseなんかと同じみたいだ。

gosh> (define (score card)
(case card
((A) 11)
((J Q K) 10)
((2 3 4 5 6 7 8 9 10) card)))
score
gosh> (score 4)
4
gosh> (score 10)
10
gosh> (score 'A)
11

条件が真の時だけ処理を行うwhen手続き

これだとif手続きの方がいいのかもしれないけど:

gosh> (define (foo num)
(when (< num 10)
num))
foo
gosh> (foo 4)
4
gosh> (foo 11)
#<undef>

ちなみに条件式が偽のときだけ式を評価させたい場合はunlessを用いるよん。

1から任意の整数までの和を足しあわせた結果を表示

こんなんでいいのかー。すごく短い。

gosh> (define (sum n)
(if (= n 1)
1
(+ (sum (- n 1)) n)))
sum
gosh> (sum 10)
55

Cだとこんな感じかしら?

/* ======================= */
/* === Include library === */
/* ======================= */
#include <stdio.h>
#include <stdlib.h>
/* ============ */
/* === main === */
/* ============ */
int main(int argc, char *argv[])
{
int i = ;
int sum = ;
int num = atoi(argv[1]);
for (i = ; i <= num; ++i)
{
sum += i;
}
printf("%d\n", sum);
}

発想が全然違う気がする。