企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 分支 ### if表达式 格式 `(if predicate then_value else_value) ` ``` (define (demo num) (if (> num 10) (- num 1) (+ num 1))) ``` ``` (demo 15) ;14 (demo 5) ;6 ``` #### begin 用法 如果你的代码体中有多条 S-表达式 ``` (define (demo n) (if (> n 15) (begin (print 1) (print 2) ) (print 3))) (demo 16) ;12 (demo 10) ;3 ``` ### and and具有任意个数的参数如果某一参数为#f,那么它就返回`#f`, ``` (and #f 0) ;Value: () (and 1 2 3) ;Value: 3 (and 1 2 3 #f) ;Value: () ``` ### or 返回第一个不是 `#f` 的值 ``` (or #f 0) ;Value: 0 (or 1 2 3) ;Value: 1 (or #f 1 2 3) ;Value: 1 (or #f #f #f) ;Value: () ``` ### cond表达式 (类似 if-elseif) 格式 ``` (cond (predicate_1 clauses_1) (predicate_2 clauses_2) ...... (predicate_n clauses_n) (else clauses_else)) ``` demo ``` (define (fee age) (cond ((or (<= age 3) (>= age 65)) 0) ((<= 4 age 6) 0.5) ((<= 7 age 12) 1.0) ((<= 13 age 15) 1.5) ((<= 16 age 18) 1.8) (else 2.0))) //也可以使用 中括号 (define (fee age) (cond [(or (<= age 3) (>= age 65)) 0] [(<= 4 age 6) 0.5)] [(<= 7 age 12) 1.0)] [(<= 13 age 15) 1.5] [(<= 16 age 18) 1.8)] [else 2.0])) ``` ### eq? 该函数比较两个对象的地址 ``` (define str "hello") ;Value: str (eq? str str) ;Value: #t (eq? "hello" "hello") ;Value: () ”hello”和”hello”被储存在了不同的地址中 (eq? 1 1) ;Value: #t ;;; comparing numbers depends on implementations (eq? 1.0 1.0) ;Value: () ``` ### eqv? 比较值 ``` (eqv? 1.0 1.0) ;Value: #t (eqv? 1 1.0) ;Value: () (eqv? (list 1 2 3) (list 1 2 3)) ;Value: () ;;; don't use it to compare sequences (eqv? "hello" "hello") ;Value: () (eqv? (lambda(x) x) (lambda (x) x)) ;Value: () ;;; the following depends on implementations ``` ### equal? 比较表或字符串 ``` (equal? (list 1 2 3) (list 1 2 3)) ;Value: #t 比较表 (equal? "hello" "hello") ;Value: #t ```