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"] ? _______________________________________________ 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. |
(take 2 A) ++ B ------------------ 李 鹏 四川大学计算机学院 ------------------ 原始邮件 ------------------ 发件人: "mirone"<[hidden email]>; 发送时间: 2017年11月4日(星期六) 晚上6:48 收件人: "haskell-cafe"<[hidden email]>; 主题: [Haskell-cafe] Replace list item with list. 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"] ? _______________________________________________ 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. |
If you sure "z" is the last element of A you can use init A ++ B if it's somwhere in the middle consider cocat $ map f A where f x | x == "z" = B | otherwise = [x] On Sat, Nov 4, 2017 at 2:41 PM, Денис Мирзоев <[hidden email]> wrote:
_______________________________________________ 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. |
In reply to this post by FlyingSheep
There is a function "concatMap" to simplify "concat $ map". Furthermore, you could use inline notation as follow: concatMap (\x -> x == "z" then B else [x]) A On Sat, Nov 4, 2017 at 7:50 AM, Денис Мирзоев <[hidden email]> wrote:
_______________________________________________ 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. |
In reply to this post by 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. _______________________________________________ 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. |
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"] _______________________________________________ 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. |
In reply to this post by Richard A. O'Keefe
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] On Mon, Nov 6, 2017 at 12:20 AM, Richard A. O'Keefe <[hidden email]> wrote:
-- Markus Läll
_______________________________________________ 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. |
Free forum by Nabble | Edit this page |