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")  ;非法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值