fork download
  1. open System
  2.  
  3. type AP = | Alpha of string | Punkt of string
  4. with static member cons (isPunct, s) = if not isPunct then Alpha s else Punkt s
  5.  
  6. [<EntryPoint>]
  7. let main argv =
  8. let s = "The quick brown fox hmm fox, jumps over the lazy dog dog."
  9. printfn "Source : %A" (s)
  10. let chartest x = Char.IsPunctuation x || Char.IsSeparator x || Char.IsWhiteSpace x
  11. let dict = set [] |> ref
  12. let xs = seq { let isChar = ref <| chartest s.[0]
  13. let a = ref 0
  14. for i in 0 .. s.Length - 1 do
  15. if chartest s.[i] = !isChar then ()
  16. else yield AP.cons(!isChar, s.[!a .. i - 1])
  17. a := i
  18. isChar := chartest s.[i]
  19. yield AP.cons(!isChar, s.[!a .. s.Length - 1]) } |> Seq.toList
  20. let rec loop xs acc =
  21. match xs with
  22. | [] -> List.rev acc
  23. | Punkt s :: Alpha a :: t when Set.contains a !dict -> loop t acc
  24. | Punkt s :: t -> loop t (s :: acc)
  25. | Alpha a :: t when Set.contains a !dict -> loop t acc
  26. | Alpha a :: t -> dict := Set.add a !dict; loop t (a :: acc)
  27. let xs = String.Join("", loop xs [] |> List.toArray)
  28.  
  29. printfn "Result : %A" (xs)
  30. 0
  31.  
Success #stdin #stdout 0.2s 12728KB
stdin
Standard input is empty
stdout
Source : "The quick brown   fox hmm fox, jumps over the lazy dog dog."
Result : "The quick brown   fox hmm, jumps over the lazy dog."