(define name "peter") ;定義字串變數name name ;---"peter"
(string-length name) ;字串變數name的長度—5
(string-ref name 3) ;字串變數name第3個位置的字元--#/e
(string-ref name 0) ;字串變數name第0個位置的字元--#/p
;error (string-set! name 0 #/g) ; 更改字符串首字母(第0个字符)为小写字母g (#/g)
#! -----------------------------!#
(define pi 3.14159) ;定義字串變數pi pi ;---"3.14159"
(set! pi "hello") ;變數設值pi pi
#! -----------------------------!#
(not #t) ;#f 用#t,#f表示true,false
;除了一般的+,-,*之外,除法分整數除法 quotient 與實數除法 /
(+ 4 5.2 7.5 2.0) ;18.7
(/ 81 3 3 2) ;4 1/2
(/ 81 3 3 1.3) ;6.9230769230769225
(* '5 '7) ;35
(quotient 9 4) ;2
#! -----------------------------!#
(define other (string #/h #/e #/l #/l #/o )) other
#! -----------------------------!#
();空串列,稱為NIL
(list 'a 'b 'c) ; (a b c)
(list '(a b c) '(d e f)); ((a b c)(d e f))
;quote / ' : 將list視為資料而不評估它
(quote (a b c));(a b c)
(quote (+ 3 4)); (+ 3 4)
(quote (quote(+ 3 4))) ; '(+ 3 4)
;car : returns the first element of a list(從list之中抓出第一個成員)
(car '(1 2 3)); 1
(car '((a b)(c d))); (a b)
(car '(first second third)); first
;cdr : returns the list without first element(去除第一個成員後的list)
(cdr '(1 2 3)); (2 3)
(cdr '((a b) (c d))) ; ((c d))
(cdr '(first second third)); (second third)
;cons : ;takes two elements and returns a list(將前面的成員推入後面的list)
(cons '1 '(2 3 4)); (1 2 3 4)
(cons '(1 2 3) '(4 5 6)); ((1 2 3) 4 5 6)
;takes two elements and returns a pair(若後面非list,則2個成員將組成一個pair) (cons 'a 'b) ;(a . b) ;結合2個list :
(append '(1 2 3) '(4 5 6)) ;(1 2 3 4 5 6)
;反轉一個list : (reverse '(1 2 3)) ; (3 2 1)
#! -----------------------------!#
;是否為字串:
(string? "5")
;是否為數字:
(number? (car (list 5 6)))
;整數是否為偶數:
(even? (car (list 5 6)))
;整數是否為奇數:
(odd? (car (list 5 6)))
;實數是否為正數:
(positive? (car (list 5.7 6))) ;實數是否為負數:
(negative? (car (list -5.7 6))) ;是否為0:
(zero? (car (list 5 6))) ;是否為邏輯值:
(boolean? 5)
;是否為null:
(null? (cdr '(a)))
#! -----------------------------!#
(if (> 6 5) ; 判斷式
(* 3 5) ; 為真
(* 8 9)) ; 為假
(define n -1) (cond ((< n 0) 'lower) ((> n 0) 'upper) (else 'equal) )
#! -----------------------------函数!#
(define (f2c t) (* 5/9 (- t 32))) (f2c 12) (define singleton (cons 'sample null)) singleton (define singleton (cons 'sample 'aaa)) singleton
#! -------------------------无名函数!#
((lambda (x) (+ x x)) (* 3 4)) ((lambda (x y) (+ x y)) 3 4) (define add2Num(lambda (x y) (+ x y))) (add2Num 7777 9999)
;(eval運算式)
;eval可以接受list當參數,然後評估其值,list內的變數,只能參照外層設定
(define x 4) (set! x 3 )
(define (f x) (eval '(print x))) (f 4) ;不回车
(let ((x 3) (y 2)) (print (+ x y)) ) ;回车
(let ((x 3) (y 2)) (+ x y) ) x
#! -------------------------向量(vector)!#;
(define v (vector 1 2 3 4 5)) v (vector-ref v 0) ; 求第n个变量的值
(vector-length v) ; 求vector的长度
(vector-set! v 2 "abc") ; 设定vector第n个元素的值 v
(define x (make-vector 5 6)) ; 创建向量表 x
(vector-ref x 2)
(vector-ref x 3)
;make-vector用来创建一个向量表,第一个参数是数量,后一个参数是添充的值,这和列表中的make-list非常相似。
#! -------------------------(List)!#;
(define la (list 1 2 3 4 )) la (length la) ; 取得
;(list-set! la 2 99) ; 设定列表第2项的值为99------------------------语言版本的不支持 la ;
;(define y (make-list 5 6)) ;创建列表------------------------语言版本的不支持
#! 算术运算 Scheme语言中的运算符有: + , - , * , / 和 expt (指数运算) 其中 - 和 / 还可以用于单目运算,如:!#
(- 4)
(/ 4)
;max 求最大
(max 8 89 90 213)
;min 求最小
(min 3 4 5 6 7)
;abs 求绝对值
(abs -7)
;2的三次方
(expt 2 3)
#! 转换!#
#! Scheme语言中用符号组合"->"来标明类型间的转换(很象C语言中的指针)的过程,就象用问号来标明类型判断过程一样。下面是一些常见的类型转换过程:!#
(number->string 123); 数字转换为字符串
(string->number "456") ;符串转换为数字
(char->integer #/a) ;字符转换为整型数,小写字母a的ASCII码值为96
(char->integer #/A) ;大写字母A的值为65
(integer->char 97) ;整型数转换为字符
(string->list "hello") ;字符串转换为列表
;(list->string (make-list 4 #/a)) ;列表转换为字符串------------------------语言版本的不支持
(string->symbol "good") ;字符串转换为符号类型
(symbol->string 'better) ;符号类型转换为字符串
#! 转换第一个List的值 !#
(define (f2c i) (cons i (cdr(list 1 2 3 4 )))) (f2c 7)
#! if !#
(if(> 4 5)#t #f)
#! if !#
;If statement has three parts: ;Predicate (Mandatory) ;True part (Mandatory) ;False part (Optional)
(define (findMax a b)( if(> a b) a b)) (findMax 3 11)
#! if !#
;True part and false part can have multiple lines of code, which can be blocked using ;Begin ;If ;Cond ;Case ;Let
(define (findMax a b) (if(> a b) (begin (set! a(* a b)) a ) b )) (findMax 13 11)
#! if-eles !#
(define num 4) (cond [(= num 1) "Hi"] [(= num 2) "Hello"] [(= num 3) "Bye"] [else "Choice not recognized"])
#! case !#
(define num2 4) (case num2 [(1) "Hi"] [else "bye"])
#! case函数 !#
;For invalid inputs, the cond will return a unexpected void output.
(define num2 4) (define (postMessage) (case num2 [(1) "Hi"] [(2) "bye"])) (postMessage) (void? (postMessage))
#! Recursion 递归!#
(define temp 0) (let loop() (set! temp(+ temp 12)) (if(< temp 200) (loop) temp))
#! Recursion for loop !# ;
(define (temp a) ;
(if (> a 200);
a ;
(temp (+ a 12)) ;
))
#! List example!验证是否都是数字#
(define (vNum inputNum) (let accessElement((tempL inputNum)) (if(null? tempL) #t (begin (if(number?(car tempL))(accessElement(cdr tempL)) #f))))) (vNum(list 3 "ss" 5)) (vNum(list 3 4 5))
#! List example!验证是否都是数字#
(define validateList (lambda(aList) (let traverseList ((index 0) (lengthList (length aList))) (if (< index lengthList) (if(not (number? (list-ref aList index))) #f (traverseList (+ index 1) lengthList)) #t)))) (validateList(list 2 4 6)) (validateList(list 2 4 "a"))
(define breakLine "--------------------") breakLine
#! test lengthList!#
(define lengthList(length (list 2 4 6))) lengthList
#! 老师的例子 teacher's example! calculateAverage !#
(define calculateAverage (lambda(aList)
(if (and (list? aList);验证是否是数字
(validateList aList)) (let traverseList ((LengthList (length aList)) (index 0) (sum 0)) (if(< index LengthList) (begin (set! sum(+ sum (list-ref aList index)));得到List的第index元素+sum
(traverseList LengthList (+ index 1) sum)) (/ sum LengthList))) "Invalid Input" )))
(calculateAverage(list 2 4 6)) (calculateAverage(list 2 4 "as"))
(define breakLine "--------------------") breakLine
#! struct 结构体 !#
(define-struct ILPTrainee(ID name batch))
(define traubee1(make-ILPTrainee 2222 "s" "sad"))
(ILPTrainee? traubee1)
(ILPTrainee-name traubee1)
(ILPTrainee-ID traubee1)
(set-ILPTrainee-ID! traubee1 22233)
(ILPTrainee-ID traubee1)
#! test !#
(define sum 10) (define a 5) (/ sum a)
#! 1. Write a program to find nth term of an arithmetic progression.!#
(define arithmeticProgression (lambda(a1 n d) (+ (* (- n 1) d) a1) ))
(arithmeticProgression 3 4 2)
#! Recursion 递归!#
(define temp 0) (let loop() (set! temp(+ temp 12)) (if(< temp 200) (loop) temp))
#! 2. Write a program to find nth term of Fibonacci series.!#
(define findFiboTeam (lambda(n) (cond ((= n 0) 0) ((= n 1) 1)
(else (+ (findFiboTeam(- n 1)) (findFiboTeam(- n 2)))))))
(findFiboTeam 0)
#! same 2 two !#
(define (findFiboTeam n)
(cond ((= n 0) 0) ((= n 1) 1) (else (+ (findFiboTeam(- n 1)) (findFiboTeam(- n 2))))))
(findFiboTeam 5)
#! 3. Write a program to find average of n numbers. !#
#! 老师的例子 teacher's example! calculateAverage !#
#! 4. Write a program to remove nth element of an array. !#??????????????????
(define tempList () )
(append tempList '(4 5 6)) ;(1 2 3 4 5 6)
(cons tempList 'a) tempList
(define removeElement (lambda(aList n)
(let traverList ((LengthList (length aList))
(index 0)
(flag n))
(if(< index LengthList)
(cond ((> n index)
(append tempList (list-ref aList index))
(traverList LengthList (+ index 1) n) )
((= n index)(traverList LengthList (+ index 1) n)))
((< n index) (append tempList (list-ref aList index))
(traverList LengthList (+ index 1) n)) ) tempList )))
(removeElement(list 2 4 6) 1)
#! 7. Write a program to find average of n numbers. !#
#! struct 结构体 !#
(define-struct Student(name ID score))
(define student1(make-Student "zhang" 1 78))
(Student? student1)
(Student-name student1)
(Student-ID student1)
(set-Student-ID! student1 2)
(Student-ID student1)
#! 6 like switch case !#
(define calculatePay (lambda(n)
(cond ((< (* n 15) 100) (* (* n 15) 0.98))
((and (>= (* n 15) 100) (< (* n 15) 200)) (* (* n 15) 0.95))
((and (>= (* n 15) 200) (< (* n 15) 500)) (* (* n 15) 0.92))
(else (* (* n 15) 0.9)) )))
(calculatePay 3)
(calculatePay 10)
(calculatePay 20)
(calculatePay 200)
#! for loop !#
(define loop (lambda(x y)
(if(<= x y)
(begin (display x)
(display #/space)
(set! x (+ x 1))
(loop x y)) )))
(loop 1 10)
#! 7 Bubble Sort!#
;(define loop1
; (lambda(x y)
; (if(<= x y)
; (define loop2
;(lambda(x y)
; (if(<= x y)
; (begin (display x) (display #/space) (set! x (+ x 1))
; (loop2 x y)) ; )))
; (loop x y))))))
#! 4 !#-----------my error
;(define removeElement
; (lambda(aList n)
; (let traverseList
; (( lengthList (length aList))
; (index 0))
; (if(< index lengthList)
; (cond
; ((< index n) (begin(display (list-ref aList index)) (display 'if--< )))
; ((= index n) (begin(display 'if--==)))
; ((> index n) (begin (display (list-ref aList index) (display 'if-->))))
; (traverseList lengthList (+ index 1))
; ))(display '--else))))
;(removeElement (list 1 2 3 4 ) 1)
#! 4 by jassica!#
(define removeNthElement
(lambda(aList n)
(append (reverse
(list-tail (list-tail (reverse aList) (- (length aList) n)) 1))
(list-tail aList n) ) ) )
(removeNthElement (list 1 2 3 4 5 ) 3)
(removeNthElement (list 1 2 3 4 5 ) 2)
#! 5 !#
(define addElement
(lambda(aList n a)
(append (reverse (list-tail (reverse aList) (- (length aList) n)))
(cons a (list-tail aList n))) ) )
(addElement (list 1 2 3 4 ) 4 9)
(addElement (list 1 2 3 4 ) 3 9)
(addElement (list 1 2 3 4 ) 2 9)
(addElement (list 1 2 3 4 ) 1 9)
(addElement (list 1 2 3 4 ) 0 9)
;(addElement (list 1 2 3 4 ) 5 9)
#! 7 !#
(require (lib "list.ss"))
(define (ascList aList) (sort aList <=))
(ascList (list 1 2 9 4 5 6 7 10 8 3 ))
;(define (delete aList n)
; (remove 3 aList ))
;error question
;(delete 3 (list 1 2 3 4 5 ))
#! 7 qsort!#
(define (qsort ls)
(if (null? ls) '()
(let ((x (car ls))
(xs (cdr ls)))
(let ((lt (filter (lambda (y) (< y x)) xs))
(st (filter (lambda (y) (>= y x)) xs)))
(append (qsort lt) (list x) (qsort st))))))
(qsort (list 1 2 9 4 5 6 7 10 8 3 ))
#! 8 !#
(define-struct structStudent(name ID score))
(define student1(make-structStudent "zhang" 1 78))
(define student2(make-structStudent "lisi" 2 95))
(define student3(make-structStudent "wang" 3 90))
; (structStudent-ID student1)
(define a (list student1 student2 student3))
(structStudent? (car a))
(structStudent-ID (car a))
(define temp1 0)
(set! temp1 (structStudent-name (car a)))
temp1 (define id 0) (define score 0) (define temp 0)
(define scoreStuct (lambda(aList)
(if (> (length aList) 0)
(begin (set! temp (structStudent-score (car aList)))
(if (> temp score);如果分数大的
(begin (set! score temp);分数的保存
(set! id (structStudent-ID (car aList)))));就把id保存
(scoreStuct (cdr aList));传递剩下的list--不管大小 )
(display 'oktoO/P--)) id ))
(scoreStuct a)
;(define-struct structStudent(name ID score))
;(define student1(make-structStudent "zhang" 1 78))
;(define student2(make-structStudent "lisi" 2 80))
;(define student3(make-structStudent "wang" 3 90))
(define (findMax aList) (let ((stu (car aList)))
(let execute((templist aList))
(if (null? (cdr templist)) (structStudent-ID stu)
(if(< (structStudent-score stu) (structStudent-score (car (cdr templist))))
(begin (set! stu (car (cdr templist)) )
(execute (cdr templist)) )
(execute (cdr templist)) )) )))
(findMax a)
#! 9 !#
(define-struct structPrice(name ID price))
(define price1(make-structPrice "zhang" 1 78))
(define price2(make-structPrice "lisi" 2 95))
(define price3(make-structPrice "wang" 3 90))
(define a1 (list price1 price2 price3))
(define getNamebyProductID (lambda(aList1 n)
(if (> (length aList1) 0)
(cond ((= (structPrice-ID (car aList1)) n)
(structPrice-name (car aList1)))
(else (getNamebyProductID (cdr aList1) n));传递剩下的list--不管大小
) )))
(getNamebyProductID a1 1)
#! 10 !#
(define-struct structCar(model name mileage))
(define car1(make-structCar "car" "Benz" 200))
(define car2(make-structCar "sprotCar" "BMW" 195))
(define car3(make-structCar "big" "ferrari" 140))
(define a2 (list car1 car2 car3))
(define getListInStructCarMileageBig
(lambda(aList2 n) (if (> (length aList2) 0)
(cond ((> (structCar-mileage (car aList2)) n)
(begin (display (structCar-model (car aList2)))
(display '@)
(display (structCar-name (car aList2)))
(display '$$) )
(getListInStructCarMileageBig (cdr aList2) n) ) ) )))
(getListInStructCarMileageBig a2 150)