0

I was having a problem in ocamlyacc where the type of my start point didn't match the return type of all my rules (I was returning a string at one point and a string list -> string lift at another point), so I made a new type:

type S = 
    | StringFun of (string list -> string list)
    | String of string;;

So I could set the type of the entry point to that to stop the error I was getting (I don't know if there's a better way to do it?), but this means now that when I want to concatenate 2 strings that are type S I can't just use ^. Is there any way to do this?

Thanks

2 Answers2

2

If you only want to concatenate values of the String variant, the following function is probably pretty good:

let concat a b =
    match a, b with
    | String sa, String sb -> String (sa ^ sb)
    | _ -> failwith "invalid argument: concat"

I suspect it would be better to adjust your ocamlyacc code so it works the way you want, however.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
-1

Maybe something like

let append_s a b = match a, b with
  | StringFun f, StringFun g -> StringFun (fun xs -> g (f xs))
  | StringFun f, String s -> StringFun (fun xs -> f (s::xs))
  | String s, StringFun f -> StringFun (fun xs -> s::f xs)
  | String x, String y -> String (x ^ y)

This doesn't look that great to me. Maybe you can redesign things such that this kind of weird concatenation isn't necessary.

gsg
  • 9,167
  • 1
  • 21
  • 23