Haskell: How to remove a List from a List in Haskell





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:



> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]

> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"

removeAll _ =
removeAll y = y

removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys









share|improve this question




















  • 1





    What is your question?

    – talex
    Nov 22 '18 at 5:43






  • 3





    You can always look at the source of the built-in functions, so you shouldn't count those out.

    – Mateen Ulhaq
    Nov 22 '18 at 5:44


















-1















I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:



> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]

> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"

removeAll _ =
removeAll y = y

removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys









share|improve this question




















  • 1





    What is your question?

    – talex
    Nov 22 '18 at 5:43






  • 3





    You can always look at the source of the built-in functions, so you shouldn't count those out.

    – Mateen Ulhaq
    Nov 22 '18 at 5:44














-1












-1








-1








I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:



> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]

> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"

removeAll _ =
removeAll y = y

removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys









share|improve this question
















I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:



> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]

> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"

removeAll _ =
removeAll y = y

removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys






haskell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:39









m0nhawk

15.9k83263




15.9k83263










asked Nov 22 '18 at 5:30









zheng zhongzheng zhong

94




94








  • 1





    What is your question?

    – talex
    Nov 22 '18 at 5:43






  • 3





    You can always look at the source of the built-in functions, so you shouldn't count those out.

    – Mateen Ulhaq
    Nov 22 '18 at 5:44














  • 1





    What is your question?

    – talex
    Nov 22 '18 at 5:43






  • 3





    You can always look at the source of the built-in functions, so you shouldn't count those out.

    – Mateen Ulhaq
    Nov 22 '18 at 5:44








1




1





What is your question?

– talex
Nov 22 '18 at 5:43





What is your question?

– talex
Nov 22 '18 at 5:43




3




3





You can always look at the source of the built-in functions, so you shouldn't count those out.

– Mateen Ulhaq
Nov 22 '18 at 5:44





You can always look at the source of the built-in functions, so you shouldn't count those out.

– Mateen Ulhaq
Nov 22 '18 at 5:44












3 Answers
3






active

oldest

votes


















0














There is a pretty straightforward recursive definition



removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys


The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.






share|improve this answer
























  • It works, thank you very much!

    – zheng zhong
    Nov 22 '18 at 14:25



















1














This is one simple way:



removeAll ignores = filter (x -> notElem x ignores)


which makes use of:




  • filter

  • notElem


If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:



myFilter _  = 
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs

myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs

removeAll ignores = myFilter (x -> myNotElem x ignores)





share|improve this answer


























  • Thank you for your answer!

    – zheng zhong
    Nov 22 '18 at 14:26



















0














A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:



removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys


If you do not want to use the built-in elem as well, you can define it similarly:



myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424422%2fhaskell-how-to-remove-a-list-from-a-list-in-haskell%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There is a pretty straightforward recursive definition



    removeAll :: Eq a => [a] -> [a] -> [a]
    removeAll _ =
    removeAll toRemove (x:xs)
    | x `belongs` toRemove = removeAll toRemove xs
    | otherwise = x:removeAll toRemove xs
    where belongs _ = False
    belongs a (y:ys)
    | a == y = True
    | otherwise = belongs a ys


    The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.






    share|improve this answer
























    • It works, thank you very much!

      – zheng zhong
      Nov 22 '18 at 14:25
















    0














    There is a pretty straightforward recursive definition



    removeAll :: Eq a => [a] -> [a] -> [a]
    removeAll _ =
    removeAll toRemove (x:xs)
    | x `belongs` toRemove = removeAll toRemove xs
    | otherwise = x:removeAll toRemove xs
    where belongs _ = False
    belongs a (y:ys)
    | a == y = True
    | otherwise = belongs a ys


    The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.






    share|improve this answer
























    • It works, thank you very much!

      – zheng zhong
      Nov 22 '18 at 14:25














    0












    0








    0







    There is a pretty straightforward recursive definition



    removeAll :: Eq a => [a] -> [a] -> [a]
    removeAll _ =
    removeAll toRemove (x:xs)
    | x `belongs` toRemove = removeAll toRemove xs
    | otherwise = x:removeAll toRemove xs
    where belongs _ = False
    belongs a (y:ys)
    | a == y = True
    | otherwise = belongs a ys


    The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.






    share|improve this answer













    There is a pretty straightforward recursive definition



    removeAll :: Eq a => [a] -> [a] -> [a]
    removeAll _ =
    removeAll toRemove (x:xs)
    | x `belongs` toRemove = removeAll toRemove xs
    | otherwise = x:removeAll toRemove xs
    where belongs _ = False
    belongs a (y:ys)
    | a == y = True
    | otherwise = belongs a ys


    The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 7:36









    lsmorlsmor

    1,004114




    1,004114













    • It works, thank you very much!

      – zheng zhong
      Nov 22 '18 at 14:25



















    • It works, thank you very much!

      – zheng zhong
      Nov 22 '18 at 14:25

















    It works, thank you very much!

    – zheng zhong
    Nov 22 '18 at 14:25





    It works, thank you very much!

    – zheng zhong
    Nov 22 '18 at 14:25













    1














    This is one simple way:



    removeAll ignores = filter (x -> notElem x ignores)


    which makes use of:




    • filter

    • notElem


    If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:



    myFilter _  = 
    myFilter pred (x:xs)
    | pred x = x : myFilter pred xs
    | otherwise = myFilter pred xs

    myNotElem e = True
    myNotElem e (x:xs)
    | e == x = False
    | otherwise = myNotElem e xs

    removeAll ignores = myFilter (x -> myNotElem x ignores)





    share|improve this answer


























    • Thank you for your answer!

      – zheng zhong
      Nov 22 '18 at 14:26
















    1














    This is one simple way:



    removeAll ignores = filter (x -> notElem x ignores)


    which makes use of:




    • filter

    • notElem


    If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:



    myFilter _  = 
    myFilter pred (x:xs)
    | pred x = x : myFilter pred xs
    | otherwise = myFilter pred xs

    myNotElem e = True
    myNotElem e (x:xs)
    | e == x = False
    | otherwise = myNotElem e xs

    removeAll ignores = myFilter (x -> myNotElem x ignores)





    share|improve this answer


























    • Thank you for your answer!

      – zheng zhong
      Nov 22 '18 at 14:26














    1












    1








    1







    This is one simple way:



    removeAll ignores = filter (x -> notElem x ignores)


    which makes use of:




    • filter

    • notElem


    If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:



    myFilter _  = 
    myFilter pred (x:xs)
    | pred x = x : myFilter pred xs
    | otherwise = myFilter pred xs

    myNotElem e = True
    myNotElem e (x:xs)
    | e == x = False
    | otherwise = myNotElem e xs

    removeAll ignores = myFilter (x -> myNotElem x ignores)





    share|improve this answer















    This is one simple way:



    removeAll ignores = filter (x -> notElem x ignores)


    which makes use of:




    • filter

    • notElem


    If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:



    myFilter _  = 
    myFilter pred (x:xs)
    | pred x = x : myFilter pred xs
    | otherwise = myFilter pred xs

    myNotElem e = True
    myNotElem e (x:xs)
    | e == x = False
    | otherwise = myNotElem e xs

    removeAll ignores = myFilter (x -> myNotElem x ignores)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 6:16

























    answered Nov 22 '18 at 6:09









    Mateen UlhaqMateen Ulhaq

    11.6k114994




    11.6k114994













    • Thank you for your answer!

      – zheng zhong
      Nov 22 '18 at 14:26



















    • Thank you for your answer!

      – zheng zhong
      Nov 22 '18 at 14:26

















    Thank you for your answer!

    – zheng zhong
    Nov 22 '18 at 14:26





    Thank you for your answer!

    – zheng zhong
    Nov 22 '18 at 14:26











    0














    A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:



    removeAll :: Eq a => [a] -> [a] -> [a]
    removeAll _ =
    removeAll x (y:ys)
    | myElem y x = removeAll x ys
    | otherwise = y:removeAll x ys


    If you do not want to use the built-in elem as well, you can define it similarly:



    myElem :: Eq a => a -> [a] -> Bool
    myElem _ = False
    myElem x (y:ys)
    | x == y = True
    | otherwise = myElem x ys





    share|improve this answer




























      0














      A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:



      removeAll :: Eq a => [a] -> [a] -> [a]
      removeAll _ =
      removeAll x (y:ys)
      | myElem y x = removeAll x ys
      | otherwise = y:removeAll x ys


      If you do not want to use the built-in elem as well, you can define it similarly:



      myElem :: Eq a => a -> [a] -> Bool
      myElem _ = False
      myElem x (y:ys)
      | x == y = True
      | otherwise = myElem x ys





      share|improve this answer


























        0












        0








        0







        A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:



        removeAll :: Eq a => [a] -> [a] -> [a]
        removeAll _ =
        removeAll x (y:ys)
        | myElem y x = removeAll x ys
        | otherwise = y:removeAll x ys


        If you do not want to use the built-in elem as well, you can define it similarly:



        myElem :: Eq a => a -> [a] -> Bool
        myElem _ = False
        myElem x (y:ys)
        | x == y = True
        | otherwise = myElem x ys





        share|improve this answer













        A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:



        removeAll :: Eq a => [a] -> [a] -> [a]
        removeAll _ =
        removeAll x (y:ys)
        | myElem y x = removeAll x ys
        | otherwise = y:removeAll x ys


        If you do not want to use the built-in elem as well, you can define it similarly:



        myElem :: Eq a => a -> [a] -> Bool
        myElem _ = False
        myElem x (y:ys)
        | x == y = True
        | otherwise = myElem x ys






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:22









        Yongwei WuYongwei Wu

        2,1761832




        2,1761832






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424422%2fhaskell-how-to-remove-a-list-from-a-list-in-haskell%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word