zoukankan      html  css  js  c++  java
  • 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)))

  • 相关阅读:
    洛谷P1501 动态树(LCT)
    Beijing Institute of Technology 2019.6 Monthly Contest (For grade 2018)
    [BJOI2018]求和
    [JSOI2015]最小表示
    简单题
    [Ynoi2016]掉进兔子洞
    乘积
    飞扬的小鸟
    [CTSC2008]网络管理
    Sequence
  • 原文地址:https://www.cnblogs.com/samwu/p/3690428.html
Copyright © 2011-2022 走看看