Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Functional programming using caml light

3 views
Skip to first unread message

ciol

unread,
Nov 1, 2006, 7:23:48 AM11/1/06
to caml...@inria.fr
Hello,
in this book ( http://caml.inria.fr/pub/docs/fpcl/index.html ), there is
the following grammar (chapter 10.3, on streams etc...) :

Expr ::= Mult
| Mult + Expr
| Mult - Expr

[...]

after factoring common prefixes, the author obtains :

Expr ::= Mult RestExpr

RestExpr ::= + Mult RestExpr
|- Mult RestEXpr
|(* nothing *)

[...]

But I don't obtain the same result for RestExpr :

RestExpr ::= + Mult Expr
|- Mult Expr
| (* nothing *)

Am I wrong ? (and why ?)

----------------------------------

Another question (totally different) :
I've created the type :
type expr = Plus of expr * expr
| Minus of expr * expr
| Div of expr * expr
| Sin of expr
| Cos of expr
[etc... (not important)]

when I match an expression, I do for instance :
Plus (u, v) -> blablabla (u, v)
Minus (u, v) -> blablabla (u, v) (same result as above)
Div (u, v) -> a different result

Can I reduce the code in order to do such a thing :
Div (u, v) -> blabliblou
BinaryFunction (u, v) -> blablabla (u, v)

(BinaryFunction match for both Plus and Minus (all the others binary
functions))

Thank you (maybe should I have written in french ?)

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

ciol

unread,
Nov 1, 2006, 7:47:41 AM11/1/06
to caml...@inria.fr
ciol wrote:

> after factoring common prefixes, the author obtains :
>
> Expr ::= Mult RestExpr
>
> RestExpr ::= + Mult RestExpr
> |- Mult RestEXpr
> |(* nothing *)
>
> [...]
>
> But I don't obtain the same result for RestExpr :
>
> RestExpr ::= + Mult Expr
> |- Mult Expr
> | (* nothing *)
>

_Sorry_, it's an error, I obtain :
RestExpr ::= + Expr
| - Expr
| (* nothing *)

But I've just realized that the author's solution is in fact the same as
mine (he replaced Expr in RestExpr by Mult RestExpr. But why ? Isn't it
more complicated ? The result in caml will be identical wont it be ?

ciol

unread,
Nov 2, 2006, 4:02:46 PM11/2/06
to caml...@inria.fr
c'est bon j'ai trouvé, ma grammaire ne gère pas l'associativité à gauche :
sur cet exemple "1 - 3 + 5", je vais l'analyser comme Moins (1, Plus (3,
5)), alors que c'est évidemment Plus (Moins (1, 3), 5)

désolé.

0 new messages