boolean.tex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. \section{Boolean Operators}
  2. \begin{Concept}{boolean value}
  3. There are no extra symbols for the truth values true
  4. and false. Instead, \nameref{nil} and the number zero
  5. are interpreted as truth value false in algebraic
  6. programs (see \nameref{false}), while any different
  7. value is considered as true (see \nameref{true}).
  8. \end{Concept}
  9. \begin{Operator}{EQUAL}
  10. \index{equation}
  11. The operator \name{equal} is an infix binary comparison
  12. operator. It is identical with \name{=}. It returns \nameref{true} if its two
  13. arguments are equal.
  14. \begin{Syntax}
  15. \meta{expression} \name{equal} \meta{expression}
  16. \end{Syntax}
  17. Equality is given between floating point numbers and integers that have
  18. the same value.
  19. \begin{Examples}
  20. on rounded; \\
  21. a := 4; & A := 4 \\
  22. b := 4.0; & B := 4.0 \\
  23. if a equal b then write "true" else write "false";
  24. & true \\
  25. if a equal 5 then write "true" else write "false";
  26. & false \\
  27. if a equal sqrt(16) then write "true" else write "false";
  28. & true
  29. \end{Examples}
  30. \begin{Comments}
  31. Comparison operators can only be used as conditions in conditional commands
  32. such as \name{if}\ldots\name{then} and \name{repeat}\ldots\name{until}.
  33. \meta{equal} can also be used as a prefix operator. However, this use
  34. is not encouraged.
  35. \end{Comments}
  36. \end{Operator}
  37. \begin{Operator}{EVENP}
  38. The \name{evenp} logical operator returns \nameref{true} if its argument is an
  39. even integer, and \nameref{nil} if its argument is an odd integer. An error
  40. message is returned if its argument is not an integer.
  41. \begin{Syntax}
  42. \name{evenp}\(\meta{integer}\) or \name{evenp} \meta{integer}
  43. \end{Syntax}
  44. \meta{integer} must evaluate to an integer.
  45. \begin{Examples}
  46. aa := 1782; & AA := 1782 \\
  47. if evenp aa then yes else no; & YES \\
  48. if evenp(-3) then yes else no; & NO \\
  49. \end{Examples}
  50. \begin{Comments}
  51. Although you would not ordinarily enter an expression such as the last
  52. example above, note that the negative term must be enclosed in parentheses
  53. to be correctly parsed. The \name{evenp} operator can only be used in
  54. conditional statements such as \name{if}\ldots\name{then}\ldots\name{else}
  55. or \name{while}\ldots\name{do}.
  56. \end{Comments}
  57. \end{Operator}
  58. \begin{Concept}{false}
  59. The symbol \nameref{nil} and the number zero are considered
  60. as \nameref{boolean value} false if used in a place where
  61. a boolean value is required. Most builtin operators return
  62. \nameref{nil} as false value. Algebraic programs use better zero.
  63. Note that \name{nil} is not printed when returned as result to
  64. a top level evaluation.
  65. \end{Concept}
  66. \begin{Operator}{FREEOF}
  67. The \name{freeof} logical operator returns
  68. \nameref{true} if its first argument does
  69. not contain its second argument anywhere in its structure.
  70. \begin{Syntax}
  71. \name{freeof}\(\meta{expression},\meta{kernel}\) or
  72. \meta{expression} \name{freeof} \meta{kernel}
  73. \end{Syntax}
  74. \meta{expression} can be any valid scalar REDUCE expression, \meta{kernel} must
  75. be a kernel expression (see \name{kernel}).
  76. \begin{Examples}
  77. a := x + sin(y)**2 + log sin z;
  78. & A := LOG(SIN(Z)) + SIN(Y)^{2} + X \\
  79. if freeof(a,sin(y)) then write "free" else write "not free";
  80. & not free \\
  81. if freeof(a,sin(x)) then write "free" else write "not free";
  82. & free \\
  83. if a freeof sin z then write "free" else write "not free";
  84. & not free
  85. \end{Examples}
  86. \begin{Comments}
  87. Logical operators can only be used in conditional expressions such as \\
  88. \name{if}\ldots\name{then} or \name{while}\ldots\name{do}.
  89. \end{Comments}
  90. \end{Operator}
  91. \begin{Operator}{LEQ}
  92. The \name{leq} operator is a binary infix or prefix logical operator. It
  93. returns \nameref{true} if its first argument is less than or equal to its second
  94. argument. As an infix operator it is identical with \name{<=}.
  95. \begin{Syntax}
  96. \name{leq}\(\meta{expression},\meta{expression}\) or \meta{expression}
  97. \name{leq} \meta{expression}
  98. \end{Syntax}
  99. \meta{expression} can be any valid REDUCE expression that evaluates to a
  100. number.
  101. \begin{Examples}
  102. a := 15; & A := 15 \\
  103. if leq(a,25) then write "yes" else write "no";
  104. & yes \\
  105. if leq(a,15) then write "yes" else write "no";
  106. & yes \\
  107. if leq(a,5) then write "yes" else write "no";
  108. & no
  109. \end{Examples}
  110. \begin{Comments}
  111. Logical operators can only be used in conditional statements such as \\
  112. \name{if}\ldots\name{then}\ldots\name{else} or \name{while}\ldots\name{do}.
  113. \end{Comments}
  114. \end{Operator}
  115. \begin{Operator}{LESSP}
  116. The \name{lessp} operator is a binary infix or prefix logical operator. It
  117. returns \nameref{true} if its first argument is strictly less than its second
  118. argument. As an infix operator it is identical with \name{<}.
  119. \begin{Syntax}
  120. \name{lessp}\(\meta{expression},\meta{expression}\)
  121. or \meta{expression} \name{lessp} \meta{expression}
  122. \end{Syntax}
  123. \meta{expression} can be any valid REDUCE expression that evaluates to a
  124. number.
  125. \begin{Examples}
  126. a := 15; & A := 15 \\
  127. if lessp(a,25) then write "yes" else write "no";
  128. & yes \\
  129. if lessp(a,15) then write "yes" else write "no";
  130. & no \\
  131. if lessp(a,5) then write "yes" else write "no";
  132. & no
  133. \end{Examples}
  134. \begin{Comments}
  135. Logical operators can only be used in conditional statements such as \\
  136. \name{if}\ldots\name{then}\ldots\name{else} or \name{while}\ldots\name{do}.
  137. \end{Comments}
  138. \end{Operator}
  139. \begin{Operator}{MEMBER}
  140. \index{list}
  141. \begin{Syntax}
  142. \meta{expression} \name{member} \meta{list}
  143. \end{Syntax}
  144. \name{member} is an infix binary comparison operator that evaluates to
  145. \nameref{true} if \meta{expression} is \nameref{equal} to a member of
  146. the \nameref{list} \meta{list}.
  147. \begin{Examples}
  148. if a member {a,b} then 1 else 0; & 1 \\
  149. if 1 member(1,2,3) then a else b; & a \\
  150. if 1 member(1.0,2) then a else b; & b
  151. \end{Examples}
  152. \begin{Comments}
  153. Logical operators can only be used in conditional statements such as \\
  154. \name{if}\ldots\name{then}\ldots\name{else} or \name{while}\ldots\name{do}.
  155. \meta{member} can also be used as a prefix operator. However, this use
  156. is not encouraged. Finally, \nameref{equal} (\name{=}) is used for the test
  157. within the list, so expressions must be of the same type to match.
  158. \end{Comments}
  159. \end{Operator}
  160. \begin{Operator}{NEQ}
  161. The operator \name{neq} is an infix binary comparison
  162. operator. It returns \nameref{true} if its two
  163. arguments are not \nameref{equal}.
  164. \begin{Syntax}
  165. \meta{expression} \name{neq} \meta{expression}
  166. \end{Syntax}
  167. An inequality is satisfied between floating point numbers and integers
  168. that have the same value.
  169. \begin{Examples}
  170. on rounded; \\
  171. a := 4; & A := 4 \\
  172. b := 4.0; & B := 4.0 \\
  173. if a neq b then write "true" else write "false";
  174. & false \\
  175. if a neq 5 then write "true" else write "false";
  176. & true
  177. \end{Examples}
  178. \begin{Comments}
  179. Comparison operators can only be used as conditions in conditional commands
  180. such as \name{if}\ldots\name{then} and \name{repeat}\ldots\name{until}.
  181. \meta{neq} can also be used as a prefix operator. However, this use
  182. is not encouraged.
  183. \end{Comments}
  184. \end{Operator}
  185. \begin{Operator}{NOT}
  186. The \name{not} operator returns \nameref{true} if its argument evaluates to
  187. \nameref{nil}, and \name{nil} if its argument is \name{true}.
  188. \begin{Syntax}
  189. \name{not}\(\meta{logical expression}\)
  190. \end{Syntax}
  191. \begin{Examples}
  192. if not numberp(a) then write "indeterminate" else write a;
  193. & indeterminate; \\
  194. a := 10; & A := 10 \\
  195. if not numberp(a) then write "indeterminate" else write a;
  196. & 10 \\
  197. if not(numberp(a) and a < 0) then write "positive number";
  198. & positive number
  199. \end{Examples}
  200. \begin{Comments}
  201. Logical operators can only be used in conditional statements such as \\
  202. \name{if}\ldots\name{then}\ldots\name{else} or \name{while}\ldots\name{do}.
  203. \end{Comments}
  204. \end{Operator}
  205. \begin{Operator}{NUMBERP}
  206. The \name{numberp} operator returns \nameref{true} if its argument is a number,
  207. and \nameref{nil} otherwise.
  208. \begin{Syntax}
  209. \name{numberp}\(\meta{expression}\) or \name{numberp} \meta{expression}
  210. \end{Syntax}
  211. \meta{expression} can be any REDUCE scalar expression.
  212. \begin{Examples}
  213. cc := 15.3; & CC := 15.3 \\
  214. if numberp(cc) then write "number" else write "nonnumber"; & number \\
  215. if numberp(cb) then write "number" else write "nonnumber"; & nonnumber
  216. \end{Examples}
  217. \begin{Comments}
  218. Logical operators can only be used in conditional expressions, such as \\
  219. \name{if}\ldots\name{then}\ldots\name{else} and \name{while}\ldots\name{do}.
  220. \end{Comments}
  221. \end{Operator}
  222. \begin{Operator}{ORDP}
  223. \index{order}
  224. The \name{ordp} logical operator returns \nameref{true} if its first argument is
  225. ordered ahead of its second argument in canonical internal ordering, or is
  226. identical to it.
  227. \begin{Syntax}
  228. \name{ordp}\(\meta{expression1},\meta{expression2}\)
  229. \end{Syntax}
  230. \meta{expression1} and \meta{expression2} can be any valid REDUCE scalar
  231. expression.
  232. \begin{Examples}
  233. if ordp(x**2 + 1,x**3 + 3) then write "yes" else write "no";
  234. & no \\
  235. if ordp(101,100) then write "yes" else write "no";
  236. & yes \\
  237. if ordp(x,x) then write "yes" else write "no";
  238. & yes
  239. \end{Examples}
  240. \begin{Comments}
  241. Logical operators can only be used in conditional expressions, such as \\
  242. \name{if}\ldots\name{then}\ldots\name{else} and \name{while}\ldots\name{do}.
  243. \end{Comments}
  244. \end{Operator}
  245. \begin{Operator}{PRIMEP}
  246. \index{prime number}
  247. \begin{Syntax}
  248. \name{primep}\(\meta{expression}\) or \name{primep} \meta{simple\_expression}
  249. \end{Syntax}
  250. If \meta{expression} evaluates to a integer, \name{primep} returns
  251. \nameref{true}
  252. if \meta{expression} is a prime number (i.e., a number other than 0 and
  253. plus or minus 1 which is only exactly divisible by itself or a unit)
  254. and \nameref{nil} otherwise.
  255. If \meta{expression} does not have an integer value, a type error occurs.
  256. \begin{Examples}
  257. if primep 3 then write "yes" else write "no"; & YES \\
  258. if primep a then 1; & ***** A invalid as integer
  259. \end{Examples}
  260. \end{Operator}
  261. \begin{Concept}{TRUE}
  262. Any value of the boolean part of a logical expression which is neither
  263. \nameref{nil} nor \name{0} is considered as \name{true}. Most
  264. builtin test and compare functions return \nameref{t} for \name{true}
  265. and \nameref{nil} for \nameindex{false}.
  266. \begin{Examples}
  267. if member(3,{1,2,3}) then 1 else -1;&1\\
  268. if floor(1.7) then 1 else -1; & 1 \\
  269. if floor(0.7) then 1 else -1; & -1\\
  270. \end{Examples}
  271. \end{Concept}