質問

I need to create my own concat function and am confused how I get the output I need. Example:

myconcat(["a", "b", "c"]) returns "abc"

([]) returns ""

I have this:

fun myconcat ([],L2) = L2
| myconcat(x::xs, L2) = x::myconcat(xs,L2);

Which just returns a list of my two strings. How would I get them to output what i need?

役に立ちましたか?

解決

You don't appear to have the correct type, to start with.
The example has type string list -> string, where your function seems to have type 'a list * 'a list -> 'a list.

To concatenate two strings, you would use ^, not ::.

The former has type string * string -> string, while the latter has type 'a * 'a list -> 'a list. As strings are not lists in SML, trying to concatenate them with :: will cause a type error.

To actually do what you want, in the simplest way, try

fun myconcat L = foldr (op^) "" L
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top