diff --git a/web/examples/biblio.cd b/web/examples/biblio.cd index a39297e1b4b91cc8a585dafd88b8d7d9106c942f..c00a008d53aa16d123ef44c223d8d3db9b48932a 100644 --- a/web/examples/biblio.cd +++ b/web/examples/biblio.cd @@ -1,33 +1,32 @@ - -type Biblio = [Heading Paper*];; -type Heading = [ PCDATA ];; -type Paper = [ Author+ Title Conference File ];; -type Author = [ PCDATA ];; -type Title = [ PCDATA ];; -type Conference = <conference>[ PCDATA ];; -type File = <file>[ PCDATA ];; +type Biblio = <bibliography>[Heading Paper*] +type Heading = <heading>[ PCDATA ] +type Paper = <paper>[ Author+ Title Conference File ] +type Author = <author>[ PCDATA ] +type Title = <title>[ PCDATA ] +type Conference = <conference>[ PCDATA ] +type File = <file>[ PCDATA ] (* Simplified HTML *) -type Html = <html>[ <head>[ <title>[ PCDATA ] ] <body>Mix ];; +type Html = <html>[ <head>[ <title>[ PCDATA ] ] <body>Mix ] type Mix = [ ( <h1>Mix | <a href=String>Mix | <p>Mix | <em>Mix - | <ul>[ <li>Mix +] | Char )* ];; + | <ul>[ <li>Mix +] | Char )* ] -let fun do_authors ([Author+] -> Mix) +let do_authors ([Author+] -> Mix) | [ <author>a ] -> a | [ <author>a <author>b ] -> a @ " and, " @ b - | [ <author>a; x] -> a @ ", " @ (do_authors x);; + | [ <author>a; x] -> a @ ", " @ (do_authors x) -let fun do_paper (Paper -> <li>Mix) +let do_paper (Paper -> <li>Mix) <paper>[ x::_* <title>t <conference>c <file>f ] -> - <li>[ <a href=f>t !(do_authors x) '; in ' <em>c '.' ];; + <li>[ <a href=f>t !(do_authors x) '; in ' <em>c '.' ] -let fun do_biblio (Biblio -> Html) +let do_biblio (Biblio -> Html) <bibliography>[ <heading>h; p ] -> let body = match p with | [] -> "Empty bibliography" | l -> [ <h1>h <ul>(map l with x -> do_paper x) ] in - <html>[ <head>[ <title>h ] <body>body ];; + <html>[ <head>[ <title>h ] <body>body ] let bib : Biblio = <bibliography>[ @@ -57,6 +56,6 @@ let bib : Biblio = <conference>"PLANX-02" <file>"planx.ps.gz" ] - ];; - -do_biblio bib;; + ] +in +do_biblio bib diff --git a/web/examples/functions.cd b/web/examples/functions.cd index 111857f6912c24c983138274fed58a8f455e401f..070510c676f9feaf0b1661b413a7cb188371499a 100644 --- a/web/examples/functions.cd +++ b/web/examples/functions.cd @@ -1,22 +1,25 @@ (* Simple functions can be defined this way: *) -let fun f1 (x : Int) : Int = x + 3;; -f1 5;; +let f1 (x : Int) : Int = x + 3 +;; +f1 5 (* With several arguments: *) -let fun f2 (x : Int, y : Int) : Int = x + y;; -f2 (10,20);; +let f2 (x : Int, y : Int) : Int = x + y +;; +f2 (10,20) (* You may directly deconstruct the arguments: *) -type A = <a href=String>String;; -let fun f3 (<a href=url>txt : A) : String = url @ "=>" @ txt;; +type A = <a href=String>String +let f3 (<a href=url>txt : A) : String = url @ "=>" @ txt +;; f3 <a href="http://www.cduce.org">"CDuce homepage";; (* In general, if you want to specify several arrow types, or use several pattern matching branches, you have the general form: *) -let fun f4 (A -> String; ['0'--'9'+] -> Int) +let f4 (A -> String; ['0'--'9'+] -> Int) | x & A -> f3 x -| x -> int_of x;; - -f4 "123";; +| x -> int_of x +;; +f4 "123" diff --git a/web/examples/integers.cd b/web/examples/integers.cd index 2b030ff024d3d6fc8d9378652a24b0895125bf1e..b04cf2b18fd891268953ba51bac80f9dc5bcf252 100644 --- a/web/examples/integers.cd +++ b/web/examples/integers.cd @@ -1,13 +1,13 @@ (* Yes, CDuce can handle large integers! *) -let fun facto (Int -> Int) +let facto (Int -> Int) | 0 | 1 -> 1 | n -> n * (facto (n - 1)) in -facto 300;; +facto 300 (* The tail-recursive way *) -let fun facto ((Int,Int) -> Int) +let facto ((Int,Int) -> Int) | (x, 0 | 1) -> x | (x, n) -> facto (x * n, n - 1) in -facto (1,10000);; +facto (1,10000) diff --git a/web/examples/mutrec.cd b/web/examples/mutrec.cd index 0731e7be358a25174748b227c9b9acdfe52aad3d..1b46d7df1cbb3ef0b6506ef5780610bd7c1931df 100644 --- a/web/examples/mutrec.cd +++ b/web/examples/mutrec.cd @@ -1,12 +1,12 @@ (* Adjacent type declarations are mutually recursive *) -type T = <t>S;; -type S = [ (Char | T)* ];; -let x : S = [ 'abc' <t>['def'] 'ghi' ];; +type T = <t>S +type S = [ (Char | T)* ] +let x : S = [ 'abc' <t>['def'] 'ghi' ] (* Similarly for toplevel function definitions *) -let fun f (x : Int) : Int = g x;; -let fun g (x : Int) : Int = 3;; -let a = 2;; -let fun h (x : Int) : Int = f x;; +let f (x : Int) : Int = g x +let g (x : Int) : Int = 3 +let a = 2 +let h (x : Int) : Int = f x (* f and g are mutually recursive, but they cannot use h *) diff --git a/web/examples/note.cd b/web/examples/note.cd index dbe3bfa4443d0ed796a6e3960bb5d4a93f9f9980..0249e0943c5d7ea5d3f1b684aa72509a31d889e8 100644 --- a/web/examples/note.cd +++ b/web/examples/note.cd @@ -1,26 +1,25 @@ +type Doc = <doc>Text +type Text = [ (Char | (Letter+ ' '* Note))* ] +type Letter = 'a'--'z' | 'A'--'Z' +type Note = <note>[ PCDATA ] -type Doc = <doc>Text;; -type Text = [ (Char | (Letter+ ' '* Note))* ];; -type Letter = 'a'--'z' | 'A'--'Z';; -type Note = <note>[ PCDATA ];; +type Flow = [ (Char | <ref no=Int>[ PCDATA ])* ] +type Notes = [ <note no=Int>[ PCDATA ]* ] +type Result = <doc>[ <body>Flow <notes>Notes ] -type Flow = [ (Char | <ref no=Int>[ PCDATA ])* ];; -type Notes = [ <note no=Int>[ PCDATA ]* ];; -type Result = <doc>[ <body>Flow <notes>Notes ];; - -let fun format (<doc>s : Doc) : Result = +let format (<doc>s : Doc) : Result = let (body,notes) = text (s,1) in - <doc>[ <body>body <notes>notes ];; + <doc>[ <body>body <notes>notes ] -let fun text ( (Text,Int) -> (Flow,Notes) ) +let text ( (Text,Int) -> (Flow,Notes) ) | ([ pre::Char*? (word::Letter+ ' '* <note>n); rem ], count) -> let (body,notes) = text (rem, count + 1) in (pre @ [<ref no=count>word] @ body, [<note no=count>n] @ notes) - | (body,_) -> (body, []);; + | (body,_) -> (body, []) let src : Doc = <doc>[ 'CDuce ' <note>"Frisch, Castagna, Benzaken" ' is an XML ' <note>"a W3C standard" - '-friendly programming language.' ];; - -format src;; + '-friendly programming language.' ] +in +format src diff --git a/web/examples/ovfun.cd b/web/examples/ovfun.cd index 28a8246a09af2677220fe81acf2e5620103e3204..84c7f0356b9a84621dab98e79dec7fd63955bbd3 100644 --- a/web/examples/ovfun.cd +++ b/web/examples/ovfun.cd @@ -1,23 +1,23 @@ -type Person = FPerson | MPerson;; -type FPerson = <person gender = "F" >[ Name Children (Tel | Email)?];; -type MPerson = <person gender="M">[ Name Children (Tel | Email)?];; -type Children = <children>[Person*];; -type Name = <name>[ PCDATA ];; -type Tel = <tel kind=?"home"|"work">['0'--'9'+ '-' '0'--'9'+];; -type Email = <email>[PCDATA '@' PCDATA];; +type Person = FPerson | MPerson +type FPerson = <person gender = "F" >[ Name Children (Tel | Email)?] +type MPerson = <person gender="M">[ Name Children (Tel | Email)?] +type Children = <children>[Person*] +type Name = <name>[ PCDATA ] +type Tel = <tel kind=?"home"|"work">['0'--'9'+ '-' '0'--'9'+] +type Email = <email>[PCDATA '@' PCDATA] -type Man = <man name=String>[ Sons Daughters ];; -type Woman = <woman name=String>[ Sons Daughters ];; -type Sons = <sons>[ Man* ];; -type Daughters = <daughters>[ Woman* ];; +type Man = <man name=String>[ Sons Daughters ] +type Woman = <woman name=String>[ Sons Daughters ] +type Sons = <sons>[ Man* ] +type Daughters = <daughters>[ Woman* ] -let fun split (MPerson -> Man ; FPerson -> Woman) - <person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*]; _] -> - let tag = match g with "F" -> `woman | "M" -> `man in - let s = map mc with x -> split x in - let d = map fc with x -> split x in - <(tag) name=n>[ <sons>s <daughters>d ] -;; +let split (MPerson -> Man ; FPerson -> Woman) + <person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*]; _] -> + let tag = match g with "F" -> `woman | "M" -> `man in + let s = map mc with x -> split x in + let d = map fc with x -> split x in + <(tag) name=n>[ <sons>s <daughters>d ] + let base : Person = <person gender="F">[ @@ -41,7 +41,5 @@ let base : Person = ] <tel kind="home"> "271-828182" ] -;; - - -split base;; +in +split base diff --git a/web/examples/projection.cd b/web/examples/projection.cd index 0844446ca6492c7d25cc7ce1ddde9633f2944b7f..4a441a3b54beb94d0c6f714139b643f840e7a743 100644 --- a/web/examples/projection.cd +++ b/web/examples/projection.cd @@ -1,13 +1,13 @@ (* The projection e/t is translated to: transform e with [ (x::t|_)* ] -> x *) -type Biblio = <bibliography>[Heading Paper*];; -type Heading = <heading>[ PCDATA ];; -type Paper = <paper>[ Author+ Title Conference File ];; -type Author = <author>[ PCDATA ];; -type Title = <title>[ PCDATA ];; -type Conference = <conference>[ PCDATA ];; -type File = <file>[ PCDATA ];; +type Biblio = <bibliography>[Heading Paper*] +type Heading = <heading>[ PCDATA ] +type Paper = <paper>[ Author+ Title Conference File ] +type Author = <author>[ PCDATA ] +type Title = <title>[ PCDATA ] +type Conference = <conference>[ PCDATA ] +type File = <file>[ PCDATA ] let bib : Biblio = <bibliography>[ @@ -37,8 +37,8 @@ let bib : Biblio = <conference>"PLANX-02" <file>"planx.ps.gz" ] - ];; + ] -let titles = [bib]/<paper>_/<title>_;; -let authors = [bib]/<paper>_/<author>_;; -let titles_concat = [bib]/<paper>_/<title>_/Char;; +let titles = [bib]/<paper>_/<title>_ +let authors = [bib]/<paper>_/<author>_ +let titles_concat = [bib]/<paper>_/<title>_/Char diff --git a/web/examples/sequence.cd b/web/examples/sequence.cd index 3ab614a7f6eb3a17dd25c6b7fc13dcbeccb2ddb4..dd88850eba75d3582570b4059fc73ba70959d048 100644 --- a/web/examples/sequence.cd +++ b/web/examples/sequence.cd @@ -1,15 +1,15 @@ (* Sequence are just defined with pairs and the atom `nil; the following notation are equivalent: *) -let l1 = (1,2,3,`nil);; -let l2 = (1,(2,(3,`nil)));; -let l3 = [ 1 2 3 ];; +let l1 = (1,2,3,`nil) +let l2 = (1,(2,(3,`nil))) +let l3 = [ 1 2 3 ] (* The [...] notation allow to specify a tail after a semi-colon : *) -let l4 = (10,20,l1);; -let l5 = [ 10 20 ; l1 ];; +let l4 = (10,20,l1) +let l5 = [ 10 20 ; l1 ] (* Concatenation @ *) -let l6 = [ 1 2 3 ] @ [ 4 5 6 ];; +let l6 = [ 1 2 3 ] @ [ 4 5 6 ] (* Inside [...], it is possible to escape a subsequence with a ! *) -let l7 = [ 1 2 !l6 !l1 5 ];; +let l7 = [ 1 2 !l6 !l1 5 ] diff --git a/web/examples/sumtype.cd b/web/examples/sumtype.cd index e6943f4d43368c5993d2f4fe5b6d743effe33c57..e4d66768579b0f66128d7fd34bd64da58dd08404 100644 --- a/web/examples/sumtype.cd +++ b/web/examples/sumtype.cd @@ -3,13 +3,13 @@ type Expr = | (`mul, Expr, Expr) | (`sub, Expr, Expr) | (`div, Expr, Expr) - | Int;; + | Int -let fun eval ( Expr -> Int ) +let eval ( Expr -> Int ) | (`add,x,y) -> eval x + eval y | (`mul,x,y) -> eval x * eval y | (`sub,x,y) -> eval x - eval y | (`div,x,y) -> (eval x) div (eval y) | n -> n in -eval (`add, 10, (`mul, 20, 5));; +eval (`add, 10, (`mul, 20, 5))