Version française
Home     About     Download     Resources     Contact us    
Browse thread
How can I have a string matched my custom type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: jeremy <flashvan@g...>
Subject: How can I have a string matched my custom type?
hi list, 
    I want to parse an expression string from the command line arguments, and have written some codes like below:
    
    open Printf

    type expr =      
          Plus        of expr * expr
        | Minus     of expr * expr
        | Times     of expr * expr            
        | Divide    of expr * expr
        | Value     of string

    let rec to_string e =
        match e with
              Plus      (left, right)   -> "(" ^ (to_string left) ^ "+" ^ (to_string right) ^ ")"
            | Minus     (left, right)   -> "(" ^ (to_string left) ^ "-" ^ (to_string right) ^ ")"
            | Times     (left, right)   -> "(" ^ (to_string left) ^ "*" ^ (to_string right) ^ ")"
            | Divide    (left, right)   -> "(" ^ (to_string left) ^ "/" ^ (to_string right) ^ ")"
            | Value     v               -> v

    let _ =
         print_endline (to_string Sys.argv.(1))

when I compile it  , It raise an error : 
This expression has type string but is here used with type expr 

How can I solve this problem?  thx .