CPSC 107 BSL基本知识
BSL(又幸福了姐/.)
基本运算表达
开方:(sqrt 16) output:4
平方: (sqr 2) output:4
无限小数表达:#i1.
boolean是小写开头!!
基本定义
Experssion 定义
Is either a value, or of the form (
Value定义
Values are numbers, strings, and images
String
1.运算
(substring "string" 1 2)
output:"tr"(是1号字符后的2个字符(含1),不是1-2
(string-length "apple")
output:5
(string-append "1" "2")
output:"12"
Image
(require 2htdp/image)
必须写在文件开头,等于import
1. 重要图形
(circle 10 "solid" "blue")
圆形:函数, 大小,实心或者空心(outline) 颜色
(rectangle 30 60 "solid" "blue")
(text "apple" 26 "blue")
输出文本样式的image (26号大小的蓝色的apple)
2.函数语句
(above image1 image2)把图形1 垒放在2上
(beside image1 image2)1 放在2 边上
(overlay image1 image2) 1和2 叠在一起,1 在上
(rotate 数值 image) 旋转 image 数值角度。正数值向左,负数值向右)
Constant Definitions
(define “名字” 数值)
这个定义一个固定的变量值
后面即使你再define一遍这个变量名,其数值也不会改变
Function Define
普通定义函数:
(define (函数名 变量名)函数部分)
(cond [条件 结果]) 可以用于一个或多个条件语句的表达
等于 if语句,但能同时表达更多的结果
可以用else在cond中
[else 结果]
(cons A B)把A 加入B这个list中 (B可以为(cons。。。。)或者empty
cons中从左到右为list第一个-最后一个elements
(first) list中的第一个字母
(rest 除了第一个之外的
Local 函数
目的:提升函数可读性 精简函数
有点像在文档中插入另一个文档,是个全新世界,所以原来的define 在local中可以重新define(但不影响外界的define值)
(local [(define x 3)
(define (timesx y)
(x y))]
(timex 5))
这个的evalution first step是取消local,在大环境中重新定义函数和常量
(define x_0 3)
(define (timesx_0 y)
(x_0 y))
(timex_0 5))
浙公网安备 33010602011771号