examples.ml 2.72 KB
 Pietro Abate committed Oct 05, 2007 1 `````` `````` Pietro Abate committed Oct 05, 2007 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ``````let examples = [ "integers"," (* Yes, CDuce can handle large integers ! *) let fun facto (Int -> Int) | 0 | 1 -> 1 | n -> n * (facto (n - 1)) in facto 300;; (* The tail-recursive way *) let fun facto ((Int,Int) -> Int) | (x, 0 | 1) -> x | (x, n) -> facto (x * n, n - 1) in facto (1,10000);; ";"ovfun"," `````` Pietro Abate committed Oct 05, 2007 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 ``````type Person = FPerson | MPerson;; type FPerson = [ Name Children (Tel | Email)?];; type MPerson = [ Name Children (Tel | Email)?];; type Children = [Person*];; type Name = [ PCDATA ];; type Tel = ['0'--'9'+ '-' '0'--'9'+];; type Email = [PCDATA '@' PCDATA];; type Man = [ Sons Daughters ];; type Woman = [ Sons Daughters ];; type Sons = [ Man* ];; type Daughters = [ Woman* ];; let fun sort (MPerson -> Man ; FPerson -> Woman) [ n [(mc::MPerson | fc::FPerson)*]; _] -> let tag = match g with \"F\" -> `woman | \"M\" -> `man in let s = map mc with x -> sort x in let d = map fc with x -> sort x in <(tag) name=n>[ s d ] ;; let base : Person = [ \"Claude\" [ [ \"Vronique\" [ [ \"Ilaria\" [] ] ] \"314-1592654\" ] ] \"271-828182\" ] ;; sort base;; ";"note"," type Doc = Text;; type Text = [ (Char | (Letter+ ' '* Note))* ];; type Letter = 'a'--'z' | 'A'--'Z';; type Note = [ PCDATA ];; type Flow = [ (Char | [ PCDATA ])* ];; type Notes = [ [ PCDATA ]* ];; type Result = [ Flow Notes ];; let fun format (s : Doc) : Result = let (body,notes) = text (s,1) in [ body notes ];; let fun text ( (Text,Int) -> (Flow,Notes) ) | ([ pre::Char*? (word::Letter+ ' '* n); rem ], count) -> let (body,notes) = text (rem, count + 1) in (pre @ [word] @ body, [n] @ notes) | (body,_) -> (body, []);; let src : Doc = [ 'CDuce ' \"Frisch, Castagna, Benzaken\" ' is an XML ' \"a W3C standard\" '-friendly programming language.' ];; format src;; "; ];; let present = "