Why use null function instead of == [] to check for empty list in Haskell?What is the monomorphism restriction?How do I check if a list is empty?How to check empty/undefined/null string in JavaScript?Python join: why is it string.join(list) instead of list.join(string)?Getting started with Haskellnull instead of ==Haskell list comprehension returns empty?Why not inherit from List<T>?Why the cycle function cannot work with empty list?Prelude “and” and “or” functions on empty listslist of nested empty lists in Haskell

Is there a way to shorten this while condition?

Found more old paper shares from broken up companies

USA: Can a witness take the 5th to avoid perjury?

Invert Some Switches on a Switchboard

What's the 1 inch size square knob sticking out of wall?

In a script how can I signal who's winning the argument?

Can't understand how static works exactly

What is the meaning of "you has the wind of me"?

How important is a good quality camera for good photography?

How to set +60Hz refresh rate with an Intel CPU?

dos2unix is unable to convert typescript file to unix format

Monty Hall Problem with a Fallible Monty

The 50,000 row query limit is not actually a "per APEX call" as widely believed

How can I deal with someone that wants to kill something that isn't supposed to be killed?

Sextortion with actual password not found in leaks

How can the artificial womb be made affordable for the common people?

Who controls a summoned steed’s familiar?

What is the spanish equivalent of "the boys are sitting"?

Why are angular mometum and angular velocity not necessarily parallel, but linear momentum and linear velocity are always parallel?

How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?

Why do people say "I am broke" instead of "I am broken"?

My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?

How can I tell if there was a power cut when I was out?

Is it possible to eat quietly in Minecraft?



Why use null function instead of == [] to check for empty list in Haskell?


What is the monomorphism restriction?How do I check if a list is empty?How to check empty/undefined/null string in JavaScript?Python join: why is it string.join(list) instead of list.join(string)?Getting started with Haskellnull instead of ==Haskell list comprehension returns empty?Why not inherit from List<T>?Why the cycle function cannot work with empty list?Prelude “and” and “or” functions on empty listslist of nested empty lists in Haskell






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








48















I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:




null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)




I tried in ghci:



xs = [] -- and then,

xs == []
null xs


Both of them are True.



I wonder what's the difference.



Should I use the null function instead of == [] and why?










share|improve this question






























    48















    I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:




    null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)




    I tried in ghci:



    xs = [] -- and then,

    xs == []
    null xs


    Both of them are True.



    I wonder what's the difference.



    Should I use the null function instead of == [] and why?










    share|improve this question


























      48












      48








      48


      1






      I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:




      null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)




      I tried in ghci:



      xs = [] -- and then,

      xs == []
      null xs


      Both of them are True.



      I wonder what's the difference.



      Should I use the null function instead of == [] and why?










      share|improve this question
















      I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says:




      null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)




      I tried in ghci:



      xs = [] -- and then,

      xs == []
      null xs


      Both of them are True.



      I wonder what's the difference.



      Should I use the null function instead of == [] and why?







      list haskell is-empty empty-list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 15 at 9:37









      Andrew T.

      4,4286 gold badges30 silver badges50 bronze badges




      4,4286 gold badges30 silver badges50 bronze badges










      asked Jul 14 at 16:36









      徐保钰徐保钰

      3833 silver badges12 bronze badges




      3833 silver badges12 bronze badges






















          4 Answers
          4






          active

          oldest

          votes


















          59














          You should use null. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference:



          > null [id]
          False
          > [id] == []
          <interactive>:1:1: error:
          • No instance for (Eq (a0 -> a0)) arising from a use of ‘==’
          (maybe you haven't applied a function to enough arguments?)
          • In the expression: [id] == []
          In an equation for ‘it’: it = [id] == []





          share|improve this answer


















          • 24





            “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

            – leftaroundabout
            Jul 14 at 17:16







          • 5





            I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

            – Silvio Mayolo
            Jul 15 at 18:14






          • 5





            For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

            – mb21
            Jul 16 at 12:19


















          45














          There is a difference. In order to use x == [], the type of the elements of the list should be a member of the Eq typeclass. Indeed, checking the equality of two lists is defined by the instance declaration:



          instance Eq a => Eq [a] where
          [] == [] = True
          (x:xs) == (y:ys) = x == y && xs == ys
          _ == _ = False


          That means that you can not use x == [] if x is for example a list of IO Ints.



          null :: [a] -> Bool on the other hand, uses pattern matching. This is implemented as:



          null :: [a] -> Bool
          null [] = True
          null (_:_) = False


          So regardless what type the elements of the list are, it will always typecheck.






          share|improve this answer
































            25














            In addition to the good answers given so far, null actually has type



            null :: Foldable t => t a -> Bool


            I don't know if you've gotten to typeclasses in LYAH, but the short of it is that null can be used not just for lists, but for any data structure that implements null.



            This is to say that using null on a Map or a Set is valid, too.



            > null Map.empty
            True
            > null (Map.singleton 1)
            False
            > null Set.empty
            True
            > null (Set.singleton 1)
            False
            > null []
            True
            > null [1]
            False


            I don't think it's especially common to write functions that need to be this general, but it doesn't hurt to default to writing more general code.



            A side note



            In many cases, you'll end up wanting to use a function like null to do conditional behavior on a list (or other data structure). If you already know that your input is a specific data structure, it's more elegant to just pattern match on its empty case.



            Compare



            myMap :: (a -> b) -> [a] -> [b]
            myMap f xs
            | null xs = []
            myMap f (x:xs) = f x : myMap f xs


            to



            myMap' :: (a -> b) -> [a] -> [b]
            myMap' f [] = []
            myMap' f (x:xs) = f x : myMap' f xs


            In general, you should try to prefer pattern matching if it makes sense.






            share|improve this answer






























              2














              Also a simple function that filter all empty list would fail:



              withoutEmpty = filter (== [])


              and that would be:



              withoutEmpty = filter null


              notice that:



              withoutEmpty ls = filter (==[]) ls


              will work just fine, but the important point is that in some cases like the other one could fail.



              Also look at @cole answer, it complements all the answers here, the typeclass Foldable has the null function there to be implemented:



              To see more info of Foldable here






              share|improve this answer




















              • 1





                This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                – Daniel Wagner
                Jul 15 at 3:30











              • @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                – Leif Willerts
                Jul 16 at 15:08






              • 1





                @LeifWillerts What is the monomorphism restriction?

                – Daniel Wagner
                Jul 16 at 16:09













              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%2f57029258%2fwhy-use-null-function-instead-of-to-check-for-empty-list-in-haskell%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              59














              You should use null. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference:



              > null [id]
              False
              > [id] == []
              <interactive>:1:1: error:
              • No instance for (Eq (a0 -> a0)) arising from a use of ‘==’
              (maybe you haven't applied a function to enough arguments?)
              • In the expression: [id] == []
              In an equation for ‘it’: it = [id] == []





              share|improve this answer


















              • 24





                “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

                – leftaroundabout
                Jul 14 at 17:16







              • 5





                I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

                – Silvio Mayolo
                Jul 15 at 18:14






              • 5





                For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

                – mb21
                Jul 16 at 12:19















              59














              You should use null. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference:



              > null [id]
              False
              > [id] == []
              <interactive>:1:1: error:
              • No instance for (Eq (a0 -> a0)) arising from a use of ‘==’
              (maybe you haven't applied a function to enough arguments?)
              • In the expression: [id] == []
              In an equation for ‘it’: it = [id] == []





              share|improve this answer


















              • 24





                “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

                – leftaroundabout
                Jul 14 at 17:16







              • 5





                I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

                – Silvio Mayolo
                Jul 15 at 18:14






              • 5





                For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

                – mb21
                Jul 16 at 12:19













              59












              59








              59







              You should use null. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference:



              > null [id]
              False
              > [id] == []
              <interactive>:1:1: error:
              • No instance for (Eq (a0 -> a0)) arising from a use of ‘==’
              (maybe you haven't applied a function to enough arguments?)
              • In the expression: [id] == []
              In an equation for ‘it’: it = [id] == []





              share|improve this answer













              You should use null. In most cases it doesn't matter, but it is a good habit to get into anyway, because occasionally you may want to check if a list of non-comparable things is empty. Here is a short, crisp example showing this difference:



              > null [id]
              False
              > [id] == []
              <interactive>:1:1: error:
              • No instance for (Eq (a0 -> a0)) arising from a use of ‘==’
              (maybe you haven't applied a function to enough arguments?)
              • In the expression: [id] == []
              In an equation for ‘it’: it = [id] == []






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 14 at 16:38









              Daniel WagnerDaniel Wagner

              109k7 gold badges172 silver badges301 bronze badges




              109k7 gold badges172 silver badges301 bronze badges







              • 24





                “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

                – leftaroundabout
                Jul 14 at 17:16







              • 5





                I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

                – Silvio Mayolo
                Jul 15 at 18:14






              • 5





                For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

                – mb21
                Jul 16 at 12:19












              • 24





                “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

                – leftaroundabout
                Jul 14 at 17:16







              • 5





                I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

                – Silvio Mayolo
                Jul 15 at 18:14






              • 5





                For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

                – mb21
                Jul 16 at 12:19







              24




              24





              “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

              – leftaroundabout
              Jul 14 at 17:16






              “In most cases it doesn't matter” – I'd disagree. It's very common to write polymorphic list functions, which shouldn't require an Eq constraint (even if they will indeed in most cases be instantiated with Eq types).

              – leftaroundabout
              Jul 14 at 17:16





              5




              5





              I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

              – Silvio Mayolo
              Jul 15 at 18:14





              I agree with everything written in this answer. For the sake of posterity and completeness, it may also be worth mentioning why length xs == 0 is a bad idea.

              – Silvio Mayolo
              Jul 15 at 18:14




              5




              5





              For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

              – mb21
              Jul 16 at 12:19





              For the record: length xs == 0 is a bad idea because it needs to traverse the whole linked list (which is O(n) and never terminates on an infinte list).

              – mb21
              Jul 16 at 12:19













              45














              There is a difference. In order to use x == [], the type of the elements of the list should be a member of the Eq typeclass. Indeed, checking the equality of two lists is defined by the instance declaration:



              instance Eq a => Eq [a] where
              [] == [] = True
              (x:xs) == (y:ys) = x == y && xs == ys
              _ == _ = False


              That means that you can not use x == [] if x is for example a list of IO Ints.



              null :: [a] -> Bool on the other hand, uses pattern matching. This is implemented as:



              null :: [a] -> Bool
              null [] = True
              null (_:_) = False


              So regardless what type the elements of the list are, it will always typecheck.






              share|improve this answer





























                45














                There is a difference. In order to use x == [], the type of the elements of the list should be a member of the Eq typeclass. Indeed, checking the equality of two lists is defined by the instance declaration:



                instance Eq a => Eq [a] where
                [] == [] = True
                (x:xs) == (y:ys) = x == y && xs == ys
                _ == _ = False


                That means that you can not use x == [] if x is for example a list of IO Ints.



                null :: [a] -> Bool on the other hand, uses pattern matching. This is implemented as:



                null :: [a] -> Bool
                null [] = True
                null (_:_) = False


                So regardless what type the elements of the list are, it will always typecheck.






                share|improve this answer



























                  45












                  45








                  45







                  There is a difference. In order to use x == [], the type of the elements of the list should be a member of the Eq typeclass. Indeed, checking the equality of two lists is defined by the instance declaration:



                  instance Eq a => Eq [a] where
                  [] == [] = True
                  (x:xs) == (y:ys) = x == y && xs == ys
                  _ == _ = False


                  That means that you can not use x == [] if x is for example a list of IO Ints.



                  null :: [a] -> Bool on the other hand, uses pattern matching. This is implemented as:



                  null :: [a] -> Bool
                  null [] = True
                  null (_:_) = False


                  So regardless what type the elements of the list are, it will always typecheck.






                  share|improve this answer















                  There is a difference. In order to use x == [], the type of the elements of the list should be a member of the Eq typeclass. Indeed, checking the equality of two lists is defined by the instance declaration:



                  instance Eq a => Eq [a] where
                  [] == [] = True
                  (x:xs) == (y:ys) = x == y && xs == ys
                  _ == _ = False


                  That means that you can not use x == [] if x is for example a list of IO Ints.



                  null :: [a] -> Bool on the other hand, uses pattern matching. This is implemented as:



                  null :: [a] -> Bool
                  null [] = True
                  null (_:_) = False


                  So regardless what type the elements of the list are, it will always typecheck.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 15 at 9:29









                  Will Ness

                  49k4 gold badges72 silver badges134 bronze badges




                  49k4 gold badges72 silver badges134 bronze badges










                  answered Jul 14 at 16:40









                  Willem Van OnsemWillem Van Onsem

                  173k18 gold badges174 silver badges262 bronze badges




                  173k18 gold badges174 silver badges262 bronze badges





















                      25














                      In addition to the good answers given so far, null actually has type



                      null :: Foldable t => t a -> Bool


                      I don't know if you've gotten to typeclasses in LYAH, but the short of it is that null can be used not just for lists, but for any data structure that implements null.



                      This is to say that using null on a Map or a Set is valid, too.



                      > null Map.empty
                      True
                      > null (Map.singleton 1)
                      False
                      > null Set.empty
                      True
                      > null (Set.singleton 1)
                      False
                      > null []
                      True
                      > null [1]
                      False


                      I don't think it's especially common to write functions that need to be this general, but it doesn't hurt to default to writing more general code.



                      A side note



                      In many cases, you'll end up wanting to use a function like null to do conditional behavior on a list (or other data structure). If you already know that your input is a specific data structure, it's more elegant to just pattern match on its empty case.



                      Compare



                      myMap :: (a -> b) -> [a] -> [b]
                      myMap f xs
                      | null xs = []
                      myMap f (x:xs) = f x : myMap f xs


                      to



                      myMap' :: (a -> b) -> [a] -> [b]
                      myMap' f [] = []
                      myMap' f (x:xs) = f x : myMap' f xs


                      In general, you should try to prefer pattern matching if it makes sense.






                      share|improve this answer



























                        25














                        In addition to the good answers given so far, null actually has type



                        null :: Foldable t => t a -> Bool


                        I don't know if you've gotten to typeclasses in LYAH, but the short of it is that null can be used not just for lists, but for any data structure that implements null.



                        This is to say that using null on a Map or a Set is valid, too.



                        > null Map.empty
                        True
                        > null (Map.singleton 1)
                        False
                        > null Set.empty
                        True
                        > null (Set.singleton 1)
                        False
                        > null []
                        True
                        > null [1]
                        False


                        I don't think it's especially common to write functions that need to be this general, but it doesn't hurt to default to writing more general code.



                        A side note



                        In many cases, you'll end up wanting to use a function like null to do conditional behavior on a list (or other data structure). If you already know that your input is a specific data structure, it's more elegant to just pattern match on its empty case.



                        Compare



                        myMap :: (a -> b) -> [a] -> [b]
                        myMap f xs
                        | null xs = []
                        myMap f (x:xs) = f x : myMap f xs


                        to



                        myMap' :: (a -> b) -> [a] -> [b]
                        myMap' f [] = []
                        myMap' f (x:xs) = f x : myMap' f xs


                        In general, you should try to prefer pattern matching if it makes sense.






                        share|improve this answer

























                          25












                          25








                          25







                          In addition to the good answers given so far, null actually has type



                          null :: Foldable t => t a -> Bool


                          I don't know if you've gotten to typeclasses in LYAH, but the short of it is that null can be used not just for lists, but for any data structure that implements null.



                          This is to say that using null on a Map or a Set is valid, too.



                          > null Map.empty
                          True
                          > null (Map.singleton 1)
                          False
                          > null Set.empty
                          True
                          > null (Set.singleton 1)
                          False
                          > null []
                          True
                          > null [1]
                          False


                          I don't think it's especially common to write functions that need to be this general, but it doesn't hurt to default to writing more general code.



                          A side note



                          In many cases, you'll end up wanting to use a function like null to do conditional behavior on a list (or other data structure). If you already know that your input is a specific data structure, it's more elegant to just pattern match on its empty case.



                          Compare



                          myMap :: (a -> b) -> [a] -> [b]
                          myMap f xs
                          | null xs = []
                          myMap f (x:xs) = f x : myMap f xs


                          to



                          myMap' :: (a -> b) -> [a] -> [b]
                          myMap' f [] = []
                          myMap' f (x:xs) = f x : myMap' f xs


                          In general, you should try to prefer pattern matching if it makes sense.






                          share|improve this answer













                          In addition to the good answers given so far, null actually has type



                          null :: Foldable t => t a -> Bool


                          I don't know if you've gotten to typeclasses in LYAH, but the short of it is that null can be used not just for lists, but for any data structure that implements null.



                          This is to say that using null on a Map or a Set is valid, too.



                          > null Map.empty
                          True
                          > null (Map.singleton 1)
                          False
                          > null Set.empty
                          True
                          > null (Set.singleton 1)
                          False
                          > null []
                          True
                          > null [1]
                          False


                          I don't think it's especially common to write functions that need to be this general, but it doesn't hurt to default to writing more general code.



                          A side note



                          In many cases, you'll end up wanting to use a function like null to do conditional behavior on a list (or other data structure). If you already know that your input is a specific data structure, it's more elegant to just pattern match on its empty case.



                          Compare



                          myMap :: (a -> b) -> [a] -> [b]
                          myMap f xs
                          | null xs = []
                          myMap f (x:xs) = f x : myMap f xs


                          to



                          myMap' :: (a -> b) -> [a] -> [b]
                          myMap' f [] = []
                          myMap' f (x:xs) = f x : myMap' f xs


                          In general, you should try to prefer pattern matching if it makes sense.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 14 at 19:02









                          colecole

                          4504 silver badges11 bronze badges




                          4504 silver badges11 bronze badges





















                              2














                              Also a simple function that filter all empty list would fail:



                              withoutEmpty = filter (== [])


                              and that would be:



                              withoutEmpty = filter null


                              notice that:



                              withoutEmpty ls = filter (==[]) ls


                              will work just fine, but the important point is that in some cases like the other one could fail.



                              Also look at @cole answer, it complements all the answers here, the typeclass Foldable has the null function there to be implemented:



                              To see more info of Foldable here






                              share|improve this answer




















                              • 1





                                This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                                – Daniel Wagner
                                Jul 15 at 3:30











                              • @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                                – Leif Willerts
                                Jul 16 at 15:08






                              • 1





                                @LeifWillerts What is the monomorphism restriction?

                                – Daniel Wagner
                                Jul 16 at 16:09















                              2














                              Also a simple function that filter all empty list would fail:



                              withoutEmpty = filter (== [])


                              and that would be:



                              withoutEmpty = filter null


                              notice that:



                              withoutEmpty ls = filter (==[]) ls


                              will work just fine, but the important point is that in some cases like the other one could fail.



                              Also look at @cole answer, it complements all the answers here, the typeclass Foldable has the null function there to be implemented:



                              To see more info of Foldable here






                              share|improve this answer




















                              • 1





                                This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                                – Daniel Wagner
                                Jul 15 at 3:30











                              • @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                                – Leif Willerts
                                Jul 16 at 15:08






                              • 1





                                @LeifWillerts What is the monomorphism restriction?

                                – Daniel Wagner
                                Jul 16 at 16:09













                              2












                              2








                              2







                              Also a simple function that filter all empty list would fail:



                              withoutEmpty = filter (== [])


                              and that would be:



                              withoutEmpty = filter null


                              notice that:



                              withoutEmpty ls = filter (==[]) ls


                              will work just fine, but the important point is that in some cases like the other one could fail.



                              Also look at @cole answer, it complements all the answers here, the typeclass Foldable has the null function there to be implemented:



                              To see more info of Foldable here






                              share|improve this answer















                              Also a simple function that filter all empty list would fail:



                              withoutEmpty = filter (== [])


                              and that would be:



                              withoutEmpty = filter null


                              notice that:



                              withoutEmpty ls = filter (==[]) ls


                              will work just fine, but the important point is that in some cases like the other one could fail.



                              Also look at @cole answer, it complements all the answers here, the typeclass Foldable has the null function there to be implemented:



                              To see more info of Foldable here







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jul 15 at 3:34

























                              answered Jul 14 at 19:07









                              Damian LatteneroDamian Lattenero

                              10.9k2 gold badges21 silver badges48 bronze badges




                              10.9k2 gold badges21 silver badges48 bronze badges







                              • 1





                                This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                                – Daniel Wagner
                                Jul 15 at 3:30











                              • @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                                – Leif Willerts
                                Jul 16 at 15:08






                              • 1





                                @LeifWillerts What is the monomorphism restriction?

                                – Daniel Wagner
                                Jul 16 at 16:09












                              • 1





                                This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                                – Daniel Wagner
                                Jul 15 at 3:30











                              • @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                                – Leif Willerts
                                Jul 16 at 15:08






                              • 1





                                @LeifWillerts What is the monomorphism restriction?

                                – Daniel Wagner
                                Jul 16 at 16:09







                              1




                              1





                              This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                              – Daniel Wagner
                              Jul 15 at 3:30





                              This answer is a bit misleading. withoutEmpty = filter (==[]) may cause an error, but withoutEmpty x = filter (==[]) x works fine.

                              – Daniel Wagner
                              Jul 15 at 3:30













                              @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                              – Leif Willerts
                              Jul 16 at 15:08





                              @DanielWagner (or Damian) could either of you elaborate why only the point-free version fails to type-check?

                              – Leif Willerts
                              Jul 16 at 15:08




                              1




                              1





                              @LeifWillerts What is the monomorphism restriction?

                              – Daniel Wagner
                              Jul 16 at 16:09





                              @LeifWillerts What is the monomorphism restriction?

                              – Daniel Wagner
                              Jul 16 at 16:09

















                              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%2f57029258%2fwhy-use-null-function-instead-of-to-check-for-empty-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

                              Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                              Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                              Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?