欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Jess的一些使用示例

发布时间:2024/3/7 58 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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")  ;非法

总结

以上是生活随笔为你收集整理的Jess的一些使用示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。