comparison - Need to test string for instance of words from list in F# -
i have list of words in f# , string input. want see if of words in list contained in string. if using c# id run foreach loop on each word in string.split , run list.contains comparison. have come following code far, can't seem access list.contains on value 'str'
let checkforvalue(x:string) = str in x.split(' ') match str commandlist -> console.writeline(str + " found: " + x) () the current function returns true , executes console.writeline method.
any ideas doing wrong?
i think have misunderstood semantics of pattern matching syntax. match str commandlist not go through commandlist , see if str 'matches' of strings in commandlist.
instead, try destructure str pattern provide, in case says nothing actual structure, matched , bound name commandlist.
as @johnpalmer pointed out, shadow other commandlist binding. anyway, don't think pattern matching proper way solve problem, (especially?) if go far using active patterns.
here's how solve problem instead:
let checkforvalue (str : string) commandlist = // convert set better performance let commands = set.oflist commandlist str.split(' ') |> seq.exists (fun x -> set.contains x commands)
Comments
Post a Comment