racket struct与list操作示例代码
代码语言:racket
所属分类:数组
代码描述:racket struct与list操作示例代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
#lang racket (struct dog (name breed age)) (define my-pet (dog "lassie" "collie" 5)) my-pet ; => #<dog> ; returns whether the variable was constructed with the dog constructor (dog? my-pet) ; => #t ; accesses the name field of the variable constructed with the dog constructor (dog-name my-pet) ; => "lassie" ; You can explicitly declare a struct to be mutable with the #:mutable option (struct rgba-color (red green blue alpha) #:mutable) (define burgundy (rgba-color 144 0 32 1.0)) (set-rgba-color-green! burgundy 10) (rgba-color-green burgundy) ; => 10 ;;; Pairs (immutable) ;; `cons' constructs pairs, `car' and `cdr' extract the first ;; and second elements (cons 1 2) ; => '(1 . 2) (car (cons 1 2)) ; => 1 (cdr (cons 1 2)) .........完整代码请登录后点击上方下载按钮下载查看
网友评论0