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