Is it possible to particulary use the default implementation of a function?
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
add a comment |
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
add a comment |
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
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
haskell typeclass
edited Nov 15 '18 at 3:19
duplode
23.1k44987
23.1k44987
asked Nov 15 '18 at 3:17
Lorem2979Lorem2979
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
It is worth noting that if you don't wantfoo
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 afterotherwise =
without having to change something else, but that will do.
– Lorem2979
Nov 15 '18 at 3:54
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
It is worth noting that if you don't wantfoo
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 afterotherwise =
without having to change something else, but that will do.
– Lorem2979
Nov 15 '18 at 3:54
add a comment |
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
It is worth noting that if you don't wantfoo
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 afterotherwise =
without having to change something else, but that will do.
– Lorem2979
Nov 15 '18 at 3:54
add a comment |
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
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
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 wantfoo
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 afterotherwise =
without having to change something else, but that will do.
– Lorem2979
Nov 15 '18 at 3:54
add a comment |
It is worth noting that if you don't wantfoo
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 afterotherwise =
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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