Bits o' Scheme
Author |
Message |
wtd
|
Posted: Fri Dec 23, 2005 1:29 am Post subject: Bits o' Scheme |
|
|
A few quick bits of syntax
With comparisons to O'Caml.
if ... else ...
code: | (if condition result-if-true result-if-false) |
O'Caml:
code: | if condition then result_if_true else result_if_false |
if ... elsif ... else ...
code: | (cond (condition1 result1)
(condition2 result2)
(else default-result)) |
O'Caml:
code: | if condition1 then result1
else if condition2 then result2
else default_result |
list literals
O'Caml:
list construction
O'Caml:
list concatenation
code: | (append '(1 2) '(3 4)) |
O'Caml:
retrieving the first element from a list
O'Caml:
code: | List.hd [1; 2; 3; 4] |
code: | match [1; 2; 3; 4] with
| x::_ -> x
| [] -> raise Not_found |
retrieving everything else from a list
O'Caml:
code: | List.tl [1; 2; 3; 4] |
code: | match [1; 2; 3; 4] with
| _::xs -> xs
| [] -> raise Not_found |
defining a function
code: | (define (my-function)
(write "hello")
(newline)) |
O'Caml:
code: | let my_function () =
print_string "Hello";
print_newline () |
defining a function with arguments
code: | (define (my-function arg)
(write arg)
(newline)) |
O'Caml:
code: | let my_function arg =
print_string arg;
print_newline () |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|