Next: , Previous: Conditionals, Up: Control Structures   [Contents][Index]


9.3 ¾ò·ï¤ÎÁȤ߹ç¤ï¤»

ËÜÀá¤Ç¤Ï¡¢if¤äcond¤È¤È¤â¤ËÍѤ¤¤ÆÊ£»¨¤Ê¾ò·ï¤òɽ¸½¤¹¤ë¤¿¤á¤Ë ¤·¤Ð¤·¤Ð»È¤ï¤ì¤ë3¤Ä¤Î¹½Â¤¤òÀâÌÀ¤·¤Þ¤¹¡£ and¤äor¤Î¹½Â¤¤Ï¡¢ Ê£¿ô¤Î¾ò·ïÉÕ¤­¹½Â¤¤Î°ì¼ï¤È¤·¤ÆÃ±ÆÈ¤Ç»È¤¦¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

Function: not condition

¤³¤Î´Ø¿ô¤Ï¡¢condition¤¬µ¶¤Ç¤¢¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë¡£ condition¤¬nil¤Ç¤¢¤ì¤Ðt¤òÊÖ¤·¡¢ ¤µ¤â¤Ê¤±¤ì¤Ðnil¤òÊÖ¤¹¡£ ´Ø¿ônot¤Ïnull¤ÈƱ°ì¤Ç¤¢¤ë¤¬¡¢ ¶õ¥ê¥¹¥È¤«¤É¤¦¤«Ä´¤Ù¤ë¾ì¹ç¤Ë¤Ï¡¢null¤ò»È¤¦¤³¤È¤ò´«¤á¤ë¡£

Special Form: and conditions…

¥¹¥Ú¥·¥ã¥ë¥Õ¥©¡¼¥àand¤Ï¡¢ ¤¹¤Ù¤Æ¤Îconditions¤¬¿¿¤Ç¤¢¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë¡£ conditions¤ò1¤Ä1¤Ä½ñ¤«¤ì¤¿½ç¤Ëɾ²Á¤·¤ÆÄ´¤Ù¤ë¡£

conditions¤Î¤É¤ì¤«¤¬nil¤Ëɾ²Á¤µ¤ì¤ë¤È¡¢ and¤Î·ë²Ì¤Ï¡¢»Ä¤ê¤Îconditions¤Ë´Ø·¸¤Ê¤¯¡¢nil¤Ë¤Ê¤ë¡£ ¤Ä¤Þ¤ê¡¢and¤Ï¤¿¤À¤Á¤Ë´°Î»¤·¡¢ conditions¤Î»Ä¤ê¤ò̵»ë¤¹¤ë¡£

conditions¤¹¤Ù¤Æ¤¬nil°Ê³°¤Ç¤¢¤ë¤³¤È¤¬¤ï¤«¤ë¤È¡¢ ¤½¤ì¤é¤ÎºÇ¸å¤ÎÃͤ¬¥Õ¥©¡¼¥àand¤ÎÃͤȤʤ롣

Îã¤ò¼¨¤½¤¦¡£ ºÇ½é¤Î¾ò·ï¤ÏÀ°¿ô1¤òÊÖ¤·¡¢¤³¤ì¤Ïnil¤Ç¤Ï¤Ê¤¤¡£ ƱÍͤˡ¢2ÈÖÌܤξò·ï¤ÏÀ°¿ô2¤òÊÖ¤·¡¢nil¤Ç¤Ï¤Ê¤¤¡£ 3ÈÖÌܤξò·ï¤Ïnil¤Ê¤Î¤Ç¡¢»Ä¤ê¤Î¾ò·ï¤òɾ²Á¤·¤Ê¤¤¡£

(and (print 1) (print 2) nil (print 3))
     -| 1
     -| 2
⇒ nil

and¤ò»È¤Ã¤¿¤è¤ê¸½¼ÂŪ¤ÊÎã¤Ï¤Ä¤®¤Î¤È¤ª¤ê¡£

(if (and (consp foo) (eq (car foo) 'x))
    (message "foo is a list starting with x"))

(consp foo)¤¬nil¤òÊÖ¤¹¤È(car foo)¤Ï¼Â¹Ô¤µ¤ì¤º¡¢ ¤½¤Î¤¿¤á¥¨¥é¡¼¤ò²óÈò¤¹¤ë¤³¤È¤ËÃí°Õ¡£

and¤Ï¡¢if¤äcond¤Çɽ¸½¤Ç¤­¤ë¡£ ¤¿¤È¤¨¤Ð¡¢¤Ä¤®¤Î¤È¤ª¤ê¡£

(and arg1 arg2 arg3)
≡
(if arg1 (if arg2 arg3))
≡
(cond (arg1 (cond (arg2 arg3))))
Special Form: or conditions…

¥¹¥Ú¥·¥ã¥ë¥Õ¥©¡¼¥àor¤Ï¡¢ conditions¤Î¾¯¤Ê¤¯¤È¤â1¤Ä¤¬¿¿¤Ç¤¢¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë¡£ conditions¤ò1¤Ä1¤Ä½ñ¤«¤ì¤¿½ç¤Ëɾ²Á¤·¤ÆÄ´¤Ù¤ë¡£

conditions¤Î¤É¤ì¤«¤¬nil°Ê³°¤Ëɾ²Á¤µ¤ì¤ë¤È¡¢ or¤Î·ë²Ì¤Ïnil°Ê³°¤Ë¤Ê¤ë¡£ ¤½¤·¤Æ¡¢or¤Ï¤¿¤À¤Á¤Ë´°Î»¤·¡¢ conditions¤Î»Ä¤ê¤ò̵»ë¤¹¤ë¡£ Ìá¤êÃͤϡ¢nil°Ê³°¤Ëɾ²Á¤µ¤ì¤¿ÃͤǤ¢¤ë¡£

conditions¤¹¤Ù¤Æ¤¬nil¤Ç¤¢¤ë¤³¤È¤¬¤ï¤«¤ë¤È¡¢ or¤Ïnil¤òÊÖ¤¹¡£

¤¿¤È¤¨¤Ð¡¢¤Ä¤®¤Î¼°¤Ï¡¢x¤¬0¤«nil¤Ç¤¢¤ë¤³¤È¤òÄ´¤Ù¤ë¡£

(or (eq x nil) (eq x 0))

and¹½Â¤¤ÈƱÍͤˡ¢or¤Ïcond¤Ç½ñ¤­É½¤»¤ë¡£ ¤¿¤È¤¨¤Ð¡¢¤Ä¤®¤Î¤È¤ª¤ê¡£

(or arg1 arg2 arg3)
≡
(cond (arg1)
      (arg2)
      (arg3))

or¤òif¤Ç½ñ¤¯¤³¤È¤â¤À¤¤¤¿¤¤¤Ç¤­¤ë¤¬¡¢ ÅÓÃæ¤ÇÈ´¤±½Ð¤»¤Ê¤¤¡£

(if arg1 arg1
  (if arg2 arg2 
    arg3))

¤³¤ì¤Ï´°Á´¤Ë¤ÏƱ°ì¤Ç¤Ï¤Ê¤¤¡£ ¤È¤¤¤¦¤Î¤Ï¡¢arg1¤äarg2¤ò2ÅÙɾ²Á¤¹¤ë¤«¤é¤Ç¤¢¤ë¡£ °ìÊý¡¢(or arg1 arg2 arg3)¤Ï¡¢ ¤É¤Î°ú¿ô¤â°ìÅÙ¤À¤±É¾²Á¤¹¤ë¡£