2017-03-07 1 views
1

私が達成したいのは、文字列とブール値をリストに渡すことです。 'switch'演算子は、input型の最初の2つの要素、 'and'演算子、および最初の2つの要素を切り替えます。データ型にないハンドル変数

しかし、 'と'ブール値と文字列を使用する場合は、エラー文字列をリストに追加する方法(「エラー」)はありますか?また、SMlはx::y::xsを受け入れません。タイプに関係なく切り替えたいので、代わりに何を入れてください。

datatype input = Bool_value of bool | String_Value of string | Exp_value of string 
datatype bin_op = switch | and 

fun helper(switch, x::y::xs) = y::x::stack 
    | helper(and, Bool_value(x)::Bool_value(y)::xs) = Bool_value(x and y)::xs 

ご協力いただきありがとうございます。

答えて

3

andはキーワードですので、bin_opswitch | and_opに変更してください。 x::y::zsは完全に有効なsmlです。ヘルパー関数の最初の行にはstackが定義されていません。最後に、smlで2つのブール値を「and」にキーワードを指定すると、andalsoとなります。

datatype input = Bool_value of bool | String_Value of string | Exp_value of string 
datatype bin_op = switch | and_op 

fun helper(switch, x::y::xs) = y::x::xs 
| helper(and_op, Bool_value(x)::Bool_value(y)::xs) = Bool_value(x andalso y)::xs 

は、比類のないパターンがありますが、私はあなたがそれらを残したり、後でそれらを置くのいずれかの前提としています。ここ

はコンパイルコードです。

+0

ありがとうございました。それは今私にとって意味があります。 – PeskyPotato

1

ダイナミックに入力された 言語のインタープリタを作成しているようです。それが本当であれば、私はあなたのプログラムの抽象構文 と、エラーを示すために例外または値を使用するかどうかにかかわらず、インタプリタのエラー処理を区別します。 たとえば、

datatype value = Int of int 
       | Bool of bool 
       | String of string 

datatype exp = Plus of Exp * Exp 
      | And of Exp * Exp 
      | Concat of Exp * Exp 
      | Literal of value 

exception TypeError of value * value 

fun eval (Plus (e1, e2)) = (case (eval e1, eval e2) of 
           (Int i, Int j) => Int (i+j) 
           | bogus => raise TypeError bogus) 
    | eval (And (e1, e2)) = (case eval e1 of 
           Bool a => if a 
             then ... 
             else ... 
          | bogus => ...) 
    | eval (Concat (e1, e2)) = (case (eval e1, eval e2) of 
            (String s, String t) => String (s^t) 
           | bogus => raise TypeError bogus) 
+0

応答ありがとう、これは私が通訳者とやったことと似ています。 – PeskyPotato

関連する問題