Jess的一些使用示例
生活随笔
收集整理的這篇文章主要介紹了
Jess的一些使用示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入: ( + 2 2)
輸出:4
輸入:if(< 2 3) then (printout t "2 is less than 3" crlf)?? ;變量可控制
輸出:if
Jess> TRUE
Jess> then
Jess> 2 is less than 3
crlf 是換行,類似于\n
輸入:(bind ?x (+ (* 20 3) ( - 37 23)))
輸出:74
輸入:(+ (+ 2 3) (* 3 3) )
輸出:14
輸入:long aLongValue=123456789L;
?? ?(bind ?aLongValue (long "123456789"))
輸出:123456789
即使調用已經賦值的變量,bind函數還是給變量重新假設了一個數值
輸入:(bind ?x "The value")
輸出:"The value"
弱變量
輸入:(bind ?a (+ 2 2))
輸出:4
輸入:?a
輸出:4
輸入:(+ ?a 2)
輸出:2
Jess> (defglobal ?*x* = 3)
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> ?*x*
4
Jess> (reset) ;; Jess will reset ?*x* to its initial value of 3
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> (set-reset-globals nil)
FALSE
Jess> (reset) ;; This time, ?*x* will not be changed.
TRUE
Jess> ?*x*
4
Jess> (bind ?grocery-list (create$ eggs bread milk))
(eggs bread milk)
Jess> (printout t (nth$ 2 ?grocery-list) crlf)
bread
Jess> (first$ ?grocery-list)
(eggs)
Jess> (rest$ ?grocery-list)
(bread milk)
Jess> (bind ?more-groceries (create$ ?grocery-list salt soap))
(eggs bread milk salt soap)
循環語句foreach
格式:(foreach <variable> <list> <expression>+)
Jess> (bind ?grocery-list (create$ eggs milk bread))
(eggs milk bread)
Jess> (foreach ?e ?grocery-list(printout t ?e crlf))
eggs
milk
bread
while語句
格式:(while <Boolean expression> do <expression>+)
(bind ?i 1)
(bind ?sum 0)
(while(<= ?i 10) do (bind ?sum (+ ?sum ?i)) (bind ?i (+ ?i 1)))
?sum
if-then-else
格式:(if <Boolean expression> then <expression>+ [else <expression>+])
Jess> (bind ?grocery-list (create$ eggs milk bread))
(eggs milk bread)
Jess> (if (member$ eggs ?grocery-list) then (printout t "I need to buy eggs" crlf) else (printout t "No eggs, thanks" crlf))
I need to buy eggs
Jess> (bind ?x 1)
1
Jess> (if (= ?x 3) then
(printout t "?x is three." crlf)
else
(if (= ?x 2) then
(printout t "?x is two." crlf)
else
(if (= ?x 1) then
(printout t "?x is one." crlf))))
?x is one.
The progn function evaluates a list of expressions and returns the value of the
last one:
(progn <expression>+)
The progn function is useful when you need to group multiple expressions
together into one expression, usually due to syntax restrictions of other functions,
as in the following example:
Jess> (bind ?n 2)
2
Jess> (while (progn (bind ?n (* ?n ?n)) (< ?n 1000)) do
(printout t ?n crlf))
4
16
256
FALSE
(apply (read) 1 2 3)
*
6
(apply (read) 1 2 3)
+
6
(apply (read) 1 2 3)
-
-4
Jess> (is-a-function printout)
TRUE
Jess> (is-a-function deftemplate)
FALSE
Jess> (call ?prices put bread 0.99)
Jess> (call ?prices put peas 1.99)
Jess> (call ?prices put beans 1.79)
Jess> (call ?prices get peas)
1.99
現在已經能明白一些吧?完事開頭難,進了門后面的就方便多了。
(bind ?pt (new java.awt.Point))
(set-member ?pt x 37)
(set-member ?pt y 42)
(get-member ?pt x)
(deffunction parseInt (?string)
(try
(bind ?i (call Integer parseInt ?string))
(printout t "The answer is " ?i crlf)
catch
(printout t "Invalid argument" crlf)))
(parrseInt "10")? ;合法
(parrseInt "dd")? ;非法
輸出:4
輸入:if(< 2 3) then (printout t "2 is less than 3" crlf)?? ;變量可控制
輸出:if
Jess> TRUE
Jess> then
Jess> 2 is less than 3
crlf 是換行,類似于\n
輸入:(bind ?x (+ (* 20 3) ( - 37 23)))
輸出:74
輸入:(+ (+ 2 3) (* 3 3) )
輸出:14
輸入:long aLongValue=123456789L;
?? ?(bind ?aLongValue (long "123456789"))
輸出:123456789
即使調用已經賦值的變量,bind函數還是給變量重新假設了一個數值
輸入:(bind ?x "The value")
輸出:"The value"
弱變量
輸入:(bind ?a (+ 2 2))
輸出:4
輸入:?a
輸出:4
輸入:(+ ?a 2)
輸出:2
Jess> (defglobal ?*x* = 3)
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> ?*x*
4
Jess> (reset) ;; Jess will reset ?*x* to its initial value of 3
TRUE
Jess> ?*x*
3
Jess> (bind ?*x* 4)
4
Jess> (set-reset-globals nil)
FALSE
Jess> (reset) ;; This time, ?*x* will not be changed.
TRUE
Jess> ?*x*
4
Jess> (bind ?grocery-list (create$ eggs bread milk))
(eggs bread milk)
Jess> (printout t (nth$ 2 ?grocery-list) crlf)
bread
Jess> (first$ ?grocery-list)
(eggs)
Jess> (rest$ ?grocery-list)
(bread milk)
Jess> (bind ?more-groceries (create$ ?grocery-list salt soap))
(eggs bread milk salt soap)
循環語句foreach
格式:(foreach <variable> <list> <expression>+)
Jess> (bind ?grocery-list (create$ eggs milk bread))
(eggs milk bread)
Jess> (foreach ?e ?grocery-list(printout t ?e crlf))
eggs
milk
bread
while語句
格式:(while <Boolean expression> do <expression>+)
(bind ?i 1)
(bind ?sum 0)
(while(<= ?i 10) do (bind ?sum (+ ?sum ?i)) (bind ?i (+ ?i 1)))
?sum
if-then-else
格式:(if <Boolean expression> then <expression>+ [else <expression>+])
Jess> (bind ?grocery-list (create$ eggs milk bread))
(eggs milk bread)
Jess> (if (member$ eggs ?grocery-list) then (printout t "I need to buy eggs" crlf) else (printout t "No eggs, thanks" crlf))
I need to buy eggs
Jess> (bind ?x 1)
1
Jess> (if (= ?x 3) then
(printout t "?x is three." crlf)
else
(if (= ?x 2) then
(printout t "?x is two." crlf)
else
(if (= ?x 1) then
(printout t "?x is one." crlf))))
?x is one.
The progn function evaluates a list of expressions and returns the value of the
last one:
(progn <expression>+)
The progn function is useful when you need to group multiple expressions
together into one expression, usually due to syntax restrictions of other functions,
as in the following example:
Jess> (bind ?n 2)
2
Jess> (while (progn (bind ?n (* ?n ?n)) (< ?n 1000)) do
(printout t ?n crlf))
4
16
256
FALSE
(apply (read) 1 2 3)
*
6
(apply (read) 1 2 3)
+
6
(apply (read) 1 2 3)
-
-4
Jess> (is-a-function printout)
TRUE
Jess> (is-a-function deftemplate)
FALSE
Jess> (call ?prices put bread 0.99)
Jess> (call ?prices put peas 1.99)
Jess> (call ?prices put beans 1.79)
Jess> (call ?prices get peas)
1.99
現在已經能明白一些吧?完事開頭難,進了門后面的就方便多了。
(bind ?pt (new java.awt.Point))
(set-member ?pt x 37)
(set-member ?pt y 42)
(get-member ?pt x)
(deffunction parseInt (?string)
(try
(bind ?i (call Integer parseInt ?string))
(printout t "The answer is " ?i crlf)
catch
(printout t "Invalid argument" crlf)))
(parrseInt "10")? ;合法
(parrseInt "dd")? ;非法
總結
以上是生活随笔為你收集整理的Jess的一些使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 查看文件夹大小 删除文件
- 下一篇: 电子设计教程12:Buck降压电路