Page XXX of XXX| first | backward | forward | last |

OCamlkurs

Geschichte von OCaml

1973
ML: Milner und andere in Edinburgh
erste Anwendung: metalanguage for interactive proofs in LCF
1984
Standard ML, aktuelle Version SML90
formale und verifizierte Sprachdefinition
1985
CAML: französische Variante mit anderer Syntax und neuer Implementierungstechnik
1985
CAML: französische Variante mit anderer Syntax und neuer Implementierungstechnik
1990
CAML light: Xavier Leroy und Michel Mauny
1997
Objective Caml: Rémy und Vouillon

Eigenschaften von OCaml

Anwendungen von OCaml

Das interaktive System

OCaml Kode in Dateien

OCamls Datentypen

#  (1=2);;
- : bool = false
# 42;;
- : int = 42
# 3.1415926536;;
- : float = 3.1415926536
# "resistentialism";;
- : string = "resistentialism"
# 'x';;
- : char = 'x'
# ();;
- : unit = ()
# 

OCaml's Funktionen

# let square x = x * x ;;
val square : int -> int = <fun>
# square 42;;
- : int = 1764
# let average x y = (x + y) / 2;;
val average : int -> int -> int = <fun>
# average 21 63;;
- : int = 42
# let paren x = "("^x^")";;
val paren : string -> string = <fun>
# let rec power x n =
    if n=0
    then 1
    else x * power x (n-1)
    ;;
val power : int -> int -> int = <fun>
# 

Aufgaben

celsius2fahrenheit und Umkehrung fahrenheit2celsius
0° Celsius = 32° Fahrenheit; 100° Celsius = 212° Fahrenheit
int2binary : int -> string
Umkehrung
int2base : int -> int -> string
Umkehrung
isbntest
ISBN is the abbreviation for the International Standard Book Number. ISBN numbers are 10 digits in length. In an ISBN of the form X-XX-XXXXXX-X:
  • The first block of digits on the left represents the language of the book (0 is used to represent English). This block is usually 1 digit in length.
  • The second block of digits represents the publisher. This block is usually 2 or 3 digits in length
  • The third block of digits represents is the number assigned to the book by the publishing company. This is usually 5 or 6 digits in length.
  • The fourth block consists of the check digit.

For the purposes of this document, we will consider the ISBN 0-13-190190-7 which is the ISBN for Number Theory with Applications by James A. Anderson and James M. Bell.

To determine the check digit for this Internation Standard Book Number, follow the below steps.

Step 1: Mutliply all nine assigned digits by weighted values. The weighted values are 1 for the first digit from the left, 2 for the second digit, three for third digit, etc.

1*0 + 2*1 + 3*3 + 4*1 + 5*9 + 6*0 + 7*1 + 8*9 + 9*0 = 139

Step 2: ISBN uses (mod 11) to determine the check digit.

139 = 7 (mod 11)

To determine if a check digit in an ISBN number is valid multiply all 10 digits by weighted values to get a value t, and            t = 0(mod 11). In our case 1*0 + 2*1 + 3*3 + 4*1 + 5*9 + 6*0 + 7*1 + 8*9 + 9*0 + 10*7 = 0(mod 11).

The below applet allows a person to validate an ISBN number. If the ISBN given is invalid, the valid check digit for the 9 assigned digits will be provided. Enter the ISBN in the first text field and then press the button titled "Verify ISBN Number".

Listen - Notation

Funktionen auf Listen - Pattern Matching

Aufgaben

Für Spezialisten

Funktionen

Abstraktion von Listenfunktionen

Manipulation von Funktionen

Aufgaben mit Funktionen höherer Ordnung

Datentyp: Tupel

Datentyp: Records

Datentyp: Varianten

Datentyp: Algebraische, rekursive Datentypen

Anwendung: Ausdrücke modellieren und verarbeiten

Veränderliche Datenstrukturen

Veränderliche Records

# type point2d =
    { mutable x : float ; mutable y : float };;
# let translate p dx dy =
    begin
      p.x <- p.x +. dx;
      p.y <- p.y +. dy;
    end;;
val translate : point2d -> float -> float -> unit = <fun>

Record als Objekt

# type pointmethods =
    { move : float -> float -> unit;
      scale : float -> unit;
      getx : unit -> float;
      gety : unit -> float;
    };;
# let newpoint x y =
  let p = { x=x; y=y; } in
  { move  = (fun dx dy -> (p.x <- p.x +. dx; p.y <- p.y +. dy));
    scale = (fun d -> (p.x <- d*.p.x; p.y <- d*.p.y));
    getx = (fun () -> p.x);
    gety = (fun () -> p.y);
  } ;;
val newpoint : float -> float -> pointmethods = <fun>
# let p = newpoint 20. 30.;;
val p : pointmethods =
  {move = <fun>; scale = <fun>; getx = <fun>; gety = <fun>}
# p.scale 0.1;;
- : unit = ()
# p.getx ();;
- : float = 2.
# p.gety ();;
- : float = 3.
		

Arrays ... lassen wir weg

Das Batchsystem

Beispiel

In der Datei convert.ml befindet sich
let int2base v b =
  ...
let main () =
  let base = int_of_string Sys.argv.(1) in
  let valu = int_of_string Sys.argv.(2) in
  let result = int2base valu base in
  print_string result;
  print_newline ();
  exit 0;;
main ()
      
Compilieren geschieht mit
ocaml convert.ml -o convert
bzw mit dem Native code compiler
ocamlopt convert.ml -o convert
Beide Dateien sind direkt ausführbar:
> ./convert 16 10006
2716
> ./convert 3 10000
111201101
      

Von Datei zum Modul

Beispiel

Datei HTMLStrings.mli enthält
type t

val make : string -> t
val extract : t -> string
val concat : t -> t -> t
Datei HTMLStrings.ml enthält
type t = string

let htmlentities s = s (* fix this *)
let htmldecode s = s (* fix this *)

let make s = htmlentities s
let extract s = htmldecode s
let concat s1 s2 = s1 ^ s2
      
Nach außen hin ist t ein abstrakter Typ, dh. seine Definition ist nicht sichtbar. Ebenso sind die Funktionen htmlentities und htmldecode nicht nach außen hin sichtbar.

Mehr über Module

Funktoren

Beispiel

# type comparison = Less | Equal | Greater;;
type comparison = Less | Equal | Greater
 
# module type ORDERED_TYPE =
   sig
     type t
     val compare: t -> t -> comparison
   end;;
module type ORDERED_TYPE = sig type t val compare : t -> t -> comparison end
 
# module Set =
   functor (Elt: ORDERED_TYPE) ->
     struct
       type element = Elt.t
       type set = element list
       let empty = []
       let rec add x s =
         match s with
           [] -> [x]
         | hd::tl ->
            match Elt.compare x hd with
              Equal   -> s         (* x is already in s *)
            | Less    -> x :: s    (* x is smaller than all elements of s *)
            | Greater -> hd :: add x tl
       let rec member x s =
         match s with
           [] -> false
         | hd::tl ->
             match Elt.compare x hd with
               Equal   -> true     (* x belongs to s *)
             | Less    -> false    (* x is smaller than all elements of s *)
             | Greater -> member x tl
     end;;
module Set :
  functor (Elt : ORDERED_TYPE) ->
    sig
      type element = Elt.t
      type set = element list
      val empty : 'a list
      val add : Elt.t -> Elt.t list -> Elt.t list
      val member : Elt.t -> Elt.t list -> bool
    end
Verwendung:
  1. Erzeuge Struktur mit Signatur ORDERED_TYPE
  2. Erzeuge Mengenstruktur durch Anwenden von Set auf diese Struktur
# module OrderedString =
   struct
     type t = string
     let compare x y = if x = y then Equal else if x < y then Less else Greater
   end;;
module OrderedString :
  sig
    type t = string
    val compare : 'a -> 'a -> comparison
  end
 
# module StringSet = Set(OrderedString);;
module StringSet :
  sig
    type element = OrderedString.t
    type set = element list
    val empty : 'a list
    val add : OrderedString.t -> OrderedString.t list -> OrderedString.t list
    val member : OrderedString.t -> OrderedString.t list -> bool
  end
 
# StringSet.member "bar" (StringSet.add "foo" StringSet.empty);;
- : bool = false

Aufgaben


Copyright © 2004 Peter Thiemann
Last modified: Wed Feb 2 21:05:20 CET 2005