Put the function name into Parentheses instead of put the name out of Parentheses.
Example: add(3, 4)
return 7 in java, but in the scheme, the function would be (+ 3 4)
which returns 7
car
function is used to get first element from the given list.
cdr
function is used to get the list from original list without first element.
cadr
function same as (car (cdr ...))
caddr
function same as (car (cdr (cdr ...)))
lamdda
expression creates a function
define
give a name to function
Examples:
(car '(+ 4 5))
=> ‘3
(cdr '(+ 4 5))
=> ‘(4 5)
(cadr '(3 4 5 6 taxes))
=> 4
(caddr '(3 4 5 6 taxes))
=> 5
(lamdda (x) (x * x) 6)
=> 36
function 6 square, pass parameter x = 6 into function
(define square (lamdda (x) (x * x)))
give this function name ‘square’
(square 5)
=> 25
More examples:
(car (car '( (3) 4 (5) )))
=> 3
comment: first run function (car '( (3) 4 (5) )
we will get list ‘(3) and then run function (car '(3) )
return the the first element of list (3) which is 3
(car (car '(3 4 (5)) ))
=> error
comment: first run function (car '(3 4 (5)) )
return 3 which is the single fisrt elements of the list. secondly run (car 3)
will be error, the reason is that the function is used on list instead of a element
(car (car '((taxes mission) 3 4)))
=> ‘taxes
comment: first run function (car '((taxes mission) 3 4)
returns (taxes mission)
the first elements of the list is the other list, secondly run (car '(taxes mission))
which return taxes
(cdr (car '((texas mission) 3 4)))
=> '(mission)
comment: first run function (car '((taxes mission) 3 4)
returns (taxes mission)
the first elements of the list is the other list, secondly run (cdr '(taxes mission))
which return '(mission)
. cdr
is the function that get the rest of list without the head.
((lambda (x) x) car)
=> return the function of car
more: ((lambda (x) x) car) '(taxes oklahoma)
=> ‘texas
this function same as (car '(taxes oklahoma))
pass parameter car into function (lambda (x) x)
, and return a new function.
((lambda (x) x) (* 3 5) (* x x) 6)
=> 36((lambda (x) x) (* x x) 6) (* 3 5)
=> 15
comment: in lambda function, it runs every staments in lambda but only output the last one.
##Note:()
is used to indicate list.
Put the quote
in front of list is used to tell scheme not to evaluate the function in ()
Example:'(+ 3 5)
=> (+ 3 5)
近期评论