Replace list item with list.

Hello everyone! If I have a list A : ["x", "y", "z"] and a list B: ["f", "g"]. Here is my question: What's the simplest way to replace "z" in the list A by list B, and get ["x", "y", "f", "g"] ?

(take 2 A) ++ B
------------------
李 鹏
四川大学计算机学院
------------------ 原始邮件 ------------------
发件人: "mirone"

On 4/11/17 11:48 PM, mirone wrote:
Hello everyone! If I have a list A : ["x", "y", "z"] and a list B: ["f", "g"]. Here is my question: What's the simplest way to replace "z" in the list A by list B, and get ["x", "y", "f", "g"] ?
f _ _ = ["x","y","f","g"] That's a joke, but it really isn't clear what the question means. One possibility is f a b = init a ++ b f [1,2,3,4,5] [9,8,7] => [1,2,3,4,9,8,7] If you mean that you want to replace "z" by ... wherever it occurs, something like -- (f needle straws haystack) replaces each occurrence of needle as -- an element of haystack with the elements of straws. f :: Eq t => t -> [t] -> ([t] -> [t]) f needle straws = concatMap (\straw -> if straw == needle then straws else [straw]) f "z" ["f","g"] ["x","z","y","z"] => ["x","f","g","y","f","g"] Your question admits of too many generalisations for us to be really helpful.

On 6/11/17 12:20 PM, Richard A. O'Keefe wrote:
On 4/11/17 11:48 PM, mirone wrote:
Hello everyone! If I have a list A : ["x", "y", "z"] and a list B: ["f", "g"]. Here is my question: What's the simplest way to replace "z" in the list A by list B, and get ["x", "y", "f", "g"] ? Your question admits of too many generalisations for us to be really helpful.
Here are some questions that might help. Let's suppose for the sake of argument that you want a function f needle straws {-B-} haystack {-A-} such that f "z" ["f","g"] ["x","y","z"] => ["x","y","f","g"] What should f n s h do when n does not occur in h? What should f n s h do when n is the first element of h? What should f n s h do when n occurs more than once in h? What should f n s h do when n occurs in s? What should f n s h do when h has 0, 1, 2, 4 or more elements? May f assume that h is in sorted order? May f use compare at all? May f use ==? What if we want to match more than one element; should we be looking for an *element* equal to "z" or a *prefix* equal to ["z"] in this case but possibly longer in others? With different answers to these questions, you might get f' :: Eq t => [t] -> [t] -> [t] -> [t] f' needles straws haystack | needles `isPrefixOf` haystack = straws ++ (length needles `drop` haystack) f' needles straws [] = [] f' needles straws (item:items) = item : f' needles straws items f' ["z"] ["f","g"] ["x","y","z","z"] => ["x","y","f","g","z"]

Yeah, the question is so open that it's hard to answer. Was thinking
recommending to inline the desired list all together, but thought it too
impolite :).
But the `concatMap` is the simplest one (to replace all "z"s that is), and
if the OP is interested, then for lists >>= is defined as concatMap, so:
a = ["x", "y", "z"]
b = ["f", "g"]
f :: String -> [String]
f str = case str of
"z" -> b
_ -> [str]
answer = a >>= f
(Didn't run this through the compiler)
On Mon, Nov 6, 2017 at 12:20 AM, Richard A. O'Keefe
On 4/11/17 11:48 PM, mirone wrote:
Hello everyone! If I have a list A : ["x", "y", "z"] and a list B: ["f", "g"]. Here is my question: What's the simplest way to replace "z" in the list A by list B, and get ["x", "y", "f", "g"] ?
f _ _ = ["x","y","f","g"]
That's a joke, but it really isn't clear what the question means. One possibility is
f a b = init a ++ b
f [1,2,3,4,5] [9,8,7] => [1,2,3,4,9,8,7]
If you mean that you want to replace "z" by ... wherever it occurs, something like -- (f needle straws haystack) replaces each occurrence of needle as -- an element of haystack with the elements of straws. f :: Eq t => t -> [t] -> ([t] -> [t]) f needle straws = concatMap (\straw -> if straw == needle then straws else [straw])
f "z" ["f","g"] ["x","z","y","z"] => ["x","f","g","y","f","g"]
Your question admits of too many generalisations for us to be really helpful.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- Markus Läll
participants (4)
-
FlyingSheep
-
Markus Läll
-
mirone
-
Richard A. O'Keefe