2017-02-11 8 views
0

リストを解析し、このケース名で必要な単語を含む新しいリストを作成する関数を作成しようとしています。匿名関数のOcaml演算子

私は例えば1名

let extract_name (lst : string list) : string list = 
List.filter (fun x -> x = "George") (lst) 

私がしようとすると、私はエラーを取得する複数の名前のためにそれを書くための機能を令状することができます。私はカッコを数回並べ替えましたが、まだエラーが出ます。

let extract_name (lst : string list) : string list = 
List.filter (fun x -> x = ("George" || "Victoria")) (lst) 

エラー

let extract_name (lst : string list) : string list = 
List.filter (fun x -> x = "George" || "Victoria") (lst) 
;; 
Characters 93-103: 
List.filter (fun x -> x = "George" || "Victoria") (lst) 
             ^^^^^^^^^^ 
Error: This expression has type string but an expression was expecte of type bool 
# let extract_name (lst : string list) : string list = 
List.filter (fun x -> x = ("George" || "Victoria")) (lst);; 
Characters 82-90: 
List.filter (fun x -> x = ("George" || "Victoria")) (lst);; 
          ^^^^^^^^ 
Error: This expression has type string but an expression was expected  of type bool 

どのように私はこの問題を解決するのですか?

答えて

3

boolean ||演算子を2つの文字列に適用しようとしていますが、これは機能しないため、型エラーが発生します。あなたは別に、両方の文字列のxとの平等をテストする必要があり、その後、OR結果:

List.filter (fun x -> (x = "George") || (x = "Victoria")) lst 
+0

あなたは、リスト内の値と比較したい場合は何? – Tank

+0

次に、['fun x - > List.mem x [" George "、" Victoria "]'](https://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html)のようなものを使用してください。 #VALmem) – Bergi