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)))

  • 相关阅读:
    递归方法:对于树形结构的表,根据当前数据获取无限极的父级名称
    P
    A
    今年暑假不AC1
    J
    今年暑假不AC
    A
    *max_element函数和*min_element函数
    1199: 房间安排
    素数
  • 原文地址:https://www.cnblogs.com/samwu/p/3690428.html
Copyright © 2011-2022 走看看