0

So I've got this list of lists of strings:

[["#@","**","#@"],["##","*%","##"]]

What I want to do is transform each inner list into a single string like this:

["#@**#@","##*%##"]

Resulting in a list of strings.

I've tried various combinations of map, foldr, and anonymous functions, but I can't for the life of me figure out how to achieve my desired result.

1 Answer 1

1

There is a function concat : string list -> string in the String structure in the Basis Library, which happens to be at the top level. Therefore, you can define your function:

val concatEach = map concat

It is going to have type string list list -> string list, which I guess is what you are looking for.

If you want to define your own concat function, you can do it this way:

val myConcat = foldr (op ^) ""

Or, without using the op keyword:

val myConcat' = foldr (fn (x, y) => x ^ y) ""
Sign up to request clarification or add additional context in comments.

3 Comments

@ joom Thank you for the suggestion. Unfortunately, that's something I already tried and it doesn't work. Here is the result off attempting the first myConcat: - val myConcat = foldr (op ^) ""; val myConcat = fn : string list -> string - - myConcat([["#@","**","#@"],["##","*%","##"]]); stdIn:1816.1-1816.46 Error: operator and operand don't agree [tycon mismatch] operator domain: string list operand: string list list in expression: myConcat (("#@" :: "**" :: <exp> :: <exp>) :: ("##" :: <exp> :: <exp>) :: nil)
Yes. myConcat was just a replacement for the concat function in the library. Have you tried the concatEach function I defined in my answer?
@ joom Actually, I just tried concatEach and it did work (I was under the impression that they worked the same way. My mistake. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.