scheme basic 2 Scheme functions: Bindings Examples for functions Note:

Infix: Expression in Math. (3 + 4);
Prefix: Scheme is prefix language (+ 3 4)

Advantage of prefix over infix:
Prefix is easy for machine to parse, and there is no any confusing question for machine to know operator precedence. More precise.

Scheme functions:

(if (> a 0) (+ 2 3) (+ 2 "foo")): if checks (a > 0) true or false, if (a > 0) returns #t, it is true and run the expression (+ 2 3), if it returns #f, it is false and run expression `(+ 2 “foo”) which will produce a run-time type clash error.

1
2
3
4
(cond 
((< 3 2) 1)
((< 4 3) 2)
(else 3))

This Function’s result is 3. COND same as if…else if…else:

(boolean? x) to check if x is a Boolean.

(char? x) to check if x is a character?

(string? x) to check if x is a string?

(symbol? x) to check if x is a symbol?

(number? x) to check if x is a number?

(pair? x) to check if x is a (not necessarily proper) pair?

(list? x) ; to check if x is a (proper) list?

(apply ): apply a expression function to a list. It has two parameters that the first is function and the second one is list.

(eval) is used to evaluate a list that has been created as data structure.

(map ) calls its function on for each elements on the the list, or corresponding sets of elements from the lists.

Bindings

Names can be bound to values by the function (let ) It takes two or more arguments. The first one is the list that contains pairs. In each pairs, the first element is the name and the second element is value of that name. This is similar to declaration in c++ or java.

Bindings Examples:

1
2
3
4
5
6
7
(let ((a 3))
(let ((a 4)
(b a)
)
(+ a b)
)
)

Comment: This code will return 7.
The first argument in first let is a list of declaration, which make a = 3; and the second argument in second let is a list of declaration that a = 4; and b = a;

In the second let, (a 4) which makes a new a variable and signed 4 to it. This is inter a which is different from outer a which is in the first argument in the first let. And (b a) in the second let assign the outer a‘s value to b whose value is 4. Because the argument(+ a b) is in the second let, a is the inter a in the second let. Therefore, the (+ a b) is (+ 4 3) => 7.

  • (let ((x 3)) (+ x 3)) => 6
  • (let ((x 3) (y 6)) (+ x y)) => 9
1
2
3
4
5
6
7
8
(let ((x 5))
(let ((x 4)
(y x)
(squarediff (lambda (a b) (- (* a a) (* b b) ) ))
)
(squarediff x y)
)
)

Comment: There is only one declaration in the outer let is (x 5) and there are three declarations in the second let such as: (x y), (y x) and (squarediff (lambda (a b) (- (* a a) (* b b)) ), the last declaration is declare a function named squarediff. Declaration (y x) is that y takes value from outer x whose value is 5, and expression (squarediff x y) is that pass inner x and the inner y as parameters to function squarediff. therefore, this function is 44 - 55 = -9.

Examples for functions

  • APPLY
    • (apply + '(3 4)) => 7
    • (apply (lambda (x) (- x 3)) ‘(4) )` =>1
  • EVAL

    • (eval (cdr '(3 + 4 5)))
      Comment: run (cdr `(3 + 4 5)) get list ‘(+ 4 5) and use EVAL to evaluate ‘(+ 4 5) list that has been created as data structure, we can get result 9.
  • MAP

    • (map + '(2 3) '(9 10)) => ‘(11 13)

    • (map car '((2 3) (texas kansas))) => (2 taxes)

    • (map cdr '((2 3) (texas kansas))) => ‘((3) (kansas))

    • (map cdr (cdr '((2 3) (taxes kansas)))) => '((kansas))
      Comment: This statement will run the (cdr '((2 3) (texas kansas))) first, we can get the list '((texas kansas)), and MAP call the function CDR on the each element of the list '((texas kansas)), so we get the lsit '((kansas)).

Note:

There must be a quote in front of the list when call the function MAP and EVAL, otherwise there will be error.