s表達式和json表達式


s表達式 + 1 2 3
普通表達式 1+2+3
json表達式
{
+:[1, 2, 3]
}
優點,一個運算符,無限個參數

 

s表達式 * (+ 1 2) 3
普通表達式 1+(2*3)
json表達式
{
*:[{+:[1,2]} , 3]
}
優點,閱讀代碼的時候,無需記住運算優先級。普通表達式則要記住運算優先級


s表達式判斷 if (< x 0) (-x) (x)
普通表達式 if(x<0){return -x} else{return x}
json表達式
{
if:[{ <: [x,0]}, -x, x]
}


s表達式and判斷 if (and (> x 0) (< x 10)) (-x) (x)
json表達式
{
if: [ {and: [{>:[x, 0]} , {<: [x, 10]} ]}, -x, x]
}

s表達式的遞歸
define (factorial n)
(if (= n 1))
(1)
(* n (factorial (- n 1)))
json表達式
{
define: [{factorial:n}, {if:[{=:[n, 1]}, 1, {*:[n, {factorial:[{-:[n, 1]}] }]} ]}]
}

 


s表達式的迭代
define (factorial n) (fact-iter 1 1 n)
{
define:[{factorial:n}, {fact-iter:[1, 1, n]}]
}

define (fact-iter product counter max-count)
(if (> counter max-count))
(product)
(fact-iter (* counter product)) (+ counter 1) (max-count)))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM