Computer Science Canada

Bits o' Scheme

Author:  wtd [ 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

code:
'(1 2 3 4)


O'Caml:

code:
[1; 2; 3; 4]


list construction

code:
(cons 1 '(2 3 4))


O'Caml:

code:
1 :: [2; 3; 4]


list concatenation

code:
(append '(1 2) '(3 4))


O'Caml:

code:
[1; 2] @ [3; 4]


retrieving the first element from a list

code:
(car '(1 2 3 4))


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

code:
(cdr '(1 2 3 4))


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 ()


: