How do I combine multiple strings using Applicative?

Asked 2 years ago, Updated 2 years ago, 107 views

Application Style

(++)<$>Just "a"<*>Just "b" --=Just "ab"

This is often the case, such as

(++)<$>Just "a"<*>Just "b"<*>Just "c"<*>...--Just "abc...

Would it be possible to write it down so that it can be connected to ?

haskell

2022-09-30 18:39

1 Answers

(++)<$>Just "a"<*>Just "b" --=Just "ab"

So, the reason why only two Maybe Strings can be connected is because the original (++) can only take two arguments.To put it another way, the above formula is (++) "a" "b" as the Maybe application.

(++) Since you can't take any number of strings by yourself, think of a way to concatenate any number of strings with a prime string before making it applicable.

foldl1(++)["a", "b", "c"] --="abc"

Also, make the string and function applicable.

foldl1(liftA2(++))[Just "a", Just "b", Just "c"] --= Just "abc"
fold1(liftA2(++)) [Just "a", Nothing, Just "c" ] -- = Nothing


2022-09-30 18:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.