Is it possible to particulary use the default implementation of a function?










0















My code works. I just would like to know if it is possible to just add something in an instance declaration to be checked first before using the default implementation, instead of having to copy the code. An example:



class (Eq a, Ord a, Show a, Num a) => Fibo a where
fib :: a -> a
fib n
| n == 0 = 0
| n == 1 = 1
| otherwise = fib (n-1) + fib (n-2)

instance Fibo Integer where
fib n
| n < 0 = -1
| n == 0 = 0
| n == 1 = 1
| otherwise = fib (n-1) + fib (n-2)


But can it also be written in a way where I dont have to re-implement the whole function? Something like:



instance Fibo Integer where
fib n
| n < 0 = -1
| otherwise = default


Or is there any other way to do without using the same code in two places?



The Solution is NOT to change the default implementation!










share|improve this question




























    0















    My code works. I just would like to know if it is possible to just add something in an instance declaration to be checked first before using the default implementation, instead of having to copy the code. An example:



    class (Eq a, Ord a, Show a, Num a) => Fibo a where
    fib :: a -> a
    fib n
    | n == 0 = 0
    | n == 1 = 1
    | otherwise = fib (n-1) + fib (n-2)

    instance Fibo Integer where
    fib n
    | n < 0 = -1
    | n == 0 = 0
    | n == 1 = 1
    | otherwise = fib (n-1) + fib (n-2)


    But can it also be written in a way where I dont have to re-implement the whole function? Something like:



    instance Fibo Integer where
    fib n
    | n < 0 = -1
    | otherwise = default


    Or is there any other way to do without using the same code in two places?



    The Solution is NOT to change the default implementation!










    share|improve this question


























      0












      0








      0


      0






      My code works. I just would like to know if it is possible to just add something in an instance declaration to be checked first before using the default implementation, instead of having to copy the code. An example:



      class (Eq a, Ord a, Show a, Num a) => Fibo a where
      fib :: a -> a
      fib n
      | n == 0 = 0
      | n == 1 = 1
      | otherwise = fib (n-1) + fib (n-2)

      instance Fibo Integer where
      fib n
      | n < 0 = -1
      | n == 0 = 0
      | n == 1 = 1
      | otherwise = fib (n-1) + fib (n-2)


      But can it also be written in a way where I dont have to re-implement the whole function? Something like:



      instance Fibo Integer where
      fib n
      | n < 0 = -1
      | otherwise = default


      Or is there any other way to do without using the same code in two places?



      The Solution is NOT to change the default implementation!










      share|improve this question
















      My code works. I just would like to know if it is possible to just add something in an instance declaration to be checked first before using the default implementation, instead of having to copy the code. An example:



      class (Eq a, Ord a, Show a, Num a) => Fibo a where
      fib :: a -> a
      fib n
      | n == 0 = 0
      | n == 1 = 1
      | otherwise = fib (n-1) + fib (n-2)

      instance Fibo Integer where
      fib n
      | n < 0 = -1
      | n == 0 = 0
      | n == 1 = 1
      | otherwise = fib (n-1) + fib (n-2)


      But can it also be written in a way where I dont have to re-implement the whole function? Something like:



      instance Fibo Integer where
      fib n
      | n < 0 = -1
      | otherwise = default


      Or is there any other way to do without using the same code in two places?



      The Solution is NOT to change the default implementation!







      haskell typeclass






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 3:19









      duplode

      23.1k44987




      23.1k44987










      asked Nov 15 '18 at 3:17









      Lorem2979Lorem2979

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          5














          Just put the common code to a helper function:



          foo::(Eq a, Ord a, Show a, Num a) =>a -> a
          foo n
          | n == 0 = 0
          | n == 1 = 1
          | otherwise = foo (n-1) + foo (n-2)

          class (Eq a, Ord a, Show a, Num a) => Fibo a where
          fib :: a -> a
          fib = foo

          instance Fibo Integer where
          fib n
          | n < 0 = -1
          | otherwise = foo n





          share|improve this answer

























          • It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

            – duplode
            Nov 15 '18 at 3:41











          • Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

            – Lorem2979
            Nov 15 '18 at 3:54











          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%2f53311894%2fis-it-possible-to-particulary-use-the-default-implementation-of-a-function%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Just put the common code to a helper function:



          foo::(Eq a, Ord a, Show a, Num a) =>a -> a
          foo n
          | n == 0 = 0
          | n == 1 = 1
          | otherwise = foo (n-1) + foo (n-2)

          class (Eq a, Ord a, Show a, Num a) => Fibo a where
          fib :: a -> a
          fib = foo

          instance Fibo Integer where
          fib n
          | n < 0 = -1
          | otherwise = foo n





          share|improve this answer

























          • It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

            – duplode
            Nov 15 '18 at 3:41











          • Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

            – Lorem2979
            Nov 15 '18 at 3:54















          5














          Just put the common code to a helper function:



          foo::(Eq a, Ord a, Show a, Num a) =>a -> a
          foo n
          | n == 0 = 0
          | n == 1 = 1
          | otherwise = foo (n-1) + foo (n-2)

          class (Eq a, Ord a, Show a, Num a) => Fibo a where
          fib :: a -> a
          fib = foo

          instance Fibo Integer where
          fib n
          | n < 0 = -1
          | otherwise = foo n





          share|improve this answer

























          • It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

            – duplode
            Nov 15 '18 at 3:41











          • Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

            – Lorem2979
            Nov 15 '18 at 3:54













          5












          5








          5







          Just put the common code to a helper function:



          foo::(Eq a, Ord a, Show a, Num a) =>a -> a
          foo n
          | n == 0 = 0
          | n == 1 = 1
          | otherwise = foo (n-1) + foo (n-2)

          class (Eq a, Ord a, Show a, Num a) => Fibo a where
          fib :: a -> a
          fib = foo

          instance Fibo Integer where
          fib n
          | n < 0 = -1
          | otherwise = foo n





          share|improve this answer















          Just put the common code to a helper function:



          foo::(Eq a, Ord a, Show a, Num a) =>a -> a
          foo n
          | n == 0 = 0
          | n == 1 = 1
          | otherwise = foo (n-1) + foo (n-2)

          class (Eq a, Ord a, Show a, Num a) => Fibo a where
          fib :: a -> a
          fib = foo

          instance Fibo Integer where
          fib n
          | n < 0 = -1
          | otherwise = foo n






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 3:54

























          answered Nov 15 '18 at 3:38









          assembly.jcassembly.jc

          2,1041215




          2,1041215












          • It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

            – duplode
            Nov 15 '18 at 3:41











          • Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

            – Lorem2979
            Nov 15 '18 at 3:54

















          • It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

            – duplode
            Nov 15 '18 at 3:41











          • Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

            – Lorem2979
            Nov 15 '18 at 3:54
















          It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

          – duplode
          Nov 15 '18 at 3:41





          It is worth noting that if you don't want foo to be part of the public interface of your code, you can just leave it out of the export list of your module.

          – duplode
          Nov 15 '18 at 3:41













          Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

          – Lorem2979
          Nov 15 '18 at 3:54





          Thank you for the answer, i hoped for a single command after otherwise = without having to change something else, but that will do.

          – Lorem2979
          Nov 15 '18 at 3:54



















          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%2f53311894%2fis-it-possible-to-particulary-use-the-default-implementation-of-a-function%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo