Racket[3]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DanielDingshengli/article/details/82287098

1.布尔
2.数 支持复数

‘#e or #i can prefix a number to force its parsing as an exact or inexact number. The prefixes #b, #o, and #x specify binary, octal, and hexadecimal interpretation of digits.

精确数和不精确数
不精确数就像一个污点,它的存在会导致一个不精确的结果。
函数exact-inexact and inexact->exact 可以逆转数的类型。
不精确数主要由sqrt, log, and sin函数产生

> (sin 0)   ; rational...有理
0
> (sin 1/2) ; not rational...无理
0.479425538604203

编程:sigma(f(x)) (a<=x<=b) sigma表示求和

#lang racket
(define (sigma f a b)
  (if (= a b)
      (f b)
      (+(f a)(sigma f (+ a 1) b))))

(sigma (lambda(x)(* x x)) 1 4)
30

数值相等的注意点

> (= 1 1.0)
#t
> (eqv? 1 1.0)  //eqv?和equal?的区别
#f
Beware of comparisons involving inexact numbers, which by their nature can have surprising behavior. Even apparently simple inexact numbers may not mean what you think they mean; for example, while a base-2 IEEE floating-point number can represent 1/2 exactly, it can only approximate 1/10:

Examples:
> (= 1/2 0.5)
#t
> (= 1/10 0.1)
#f
> (inexact->exact 0.1)
3602879701896397/36028797018963968

3.字符 #/
4.字符串

> "Apple"
"Apple"
> "\u03BB"
"λ"
> (display "Apple")
Apple
> (display "a \"quoted\" thing")
a "quoted" thing
> (display "two\nlines")
two
lines
> (display "\u03BB")
λ

字符串处理函数

> (string-ref "Apple" 0)  //输出字符串第n位置的字符
#\A
> (define s (make-string 5 #\.))  //输出n个#\后的字符
> s
"....."
> (string-set! s 2 #\λ)  \\改变某个位置的字符
> s
"..λ.."
(string<? "apple" "Banana")  //小写为大
#f
> (string-ci<? "apple" "Banana")//忽略大小写
#t
> (string-upcase "Straße") //改为大写
"STRASSE"

5.Bytes and Bytes Strings
byte? 断定数字是否是一字节的 0~255之间
byte string
Byte strings can be used in applications that process pure ASCII instead of Unicode text.
6.symbol

Whitespace or special characters can be included in an identifier by quoting them with | or . These quoting mechanisms are used in the printed form of identifiers that contain special characters or that might otherwise look like numbers.

> (string->symbol "one, two")
'|one, two|
> (string->symbol "6")
'|6|
> (write 'Apple)
Apple
> (display 'Apple)
Apple
> (write '|6|)
|6|
> (display '|6|)
6

7.keywords 关键字
its printed form is prefixed with #: 打印前缀带有#:

>#:apple
. #%datum: keyword used as an expression in: #:apple
> '#:apple
'#:apple

8.pairs
可变的pairs

Pairs are immutable (contrary to Lisp tradition), and pair? and list? recognize immutable pairs and lists, only. The mcons procedure creates a mutable pair, which works with set-mcar! and set-mcdr!, as well as mcar and mcdr. A mutable pair prints using mcons, while write and display print mutable pairs with { and }:

Examples:
> (define p (mcons 1 2))
> p
(mcons 1 2)
> (pair? p)
#f
> (mpair? p)
#t
> (set-mcar! p 0)
> p
(mcons 0 2)
> (write p)
{0 . 2}

9.vectors
支持常数时间的存取和元素更新

> #("a" "b" "c")
'#("a" "b" "c")
> #(name (that tune))
'#(name (that tune))
> #4(baldwin bruce)
'#(baldwin bruce bruce bruce)
> (vector-ref #("a" "b" "c") 1)
"b"
> (vector-ref #(name (that tune)) 1)
'(that tune)

10.Hash Tables
11.Boxes
A box is like a single-element vector.
11.Void and Undefined

猜你喜欢

转载自blog.csdn.net/DanielDingshengli/article/details/82287098
3
3-3