Taking sum of list in Haskell
I am trying to take the sum of a list in Haskell but it gives the error, please see the below code.
binListToDec :: [Int] -> Int
binListToDec (x:xs) = if length binListToDec == 0 then 1
else x + binListToDec xs
It gives the following error
* No instance for (Foldable ((->) [Int]))
arising from a use of `length'
* In the first argument of `(==)', namely `length binListToDec'
In the expression: length binListToDec == 0
In the expression:
if length binListToDec == 0 then 1 else x + binListToDec xs
|
2 | binListToDec (x:xs) = if length binListToDec == 0 then 1
list haskell
add a comment |
I am trying to take the sum of a list in Haskell but it gives the error, please see the below code.
binListToDec :: [Int] -> Int
binListToDec (x:xs) = if length binListToDec == 0 then 1
else x + binListToDec xs
It gives the following error
* No instance for (Foldable ((->) [Int]))
arising from a use of `length'
* In the first argument of `(==)', namely `length binListToDec'
In the expression: length binListToDec == 0
In the expression:
if length binListToDec == 0 then 1 else x + binListToDec xs
|
2 | binListToDec (x:xs) = if length binListToDec == 0 then 1
list haskell
What islength binListToDec
supposed to do?
– Willem Van Onsem
Nov 13 '18 at 18:14
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
5
but (a) the list you pass is(x:xs)
and (b) the length can never be zero, since(x:xs)
is the pattern for a non-empty list.
– Willem Van Onsem
Nov 13 '18 at 18:16
add a comment |
I am trying to take the sum of a list in Haskell but it gives the error, please see the below code.
binListToDec :: [Int] -> Int
binListToDec (x:xs) = if length binListToDec == 0 then 1
else x + binListToDec xs
It gives the following error
* No instance for (Foldable ((->) [Int]))
arising from a use of `length'
* In the first argument of `(==)', namely `length binListToDec'
In the expression: length binListToDec == 0
In the expression:
if length binListToDec == 0 then 1 else x + binListToDec xs
|
2 | binListToDec (x:xs) = if length binListToDec == 0 then 1
list haskell
I am trying to take the sum of a list in Haskell but it gives the error, please see the below code.
binListToDec :: [Int] -> Int
binListToDec (x:xs) = if length binListToDec == 0 then 1
else x + binListToDec xs
It gives the following error
* No instance for (Foldable ((->) [Int]))
arising from a use of `length'
* In the first argument of `(==)', namely `length binListToDec'
In the expression: length binListToDec == 0
In the expression:
if length binListToDec == 0 then 1 else x + binListToDec xs
|
2 | binListToDec (x:xs) = if length binListToDec == 0 then 1
list haskell
list haskell
asked Nov 13 '18 at 18:10
Muhammad UsmanMuhammad Usman
67118
67118
What islength binListToDec
supposed to do?
– Willem Van Onsem
Nov 13 '18 at 18:14
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
5
but (a) the list you pass is(x:xs)
and (b) the length can never be zero, since(x:xs)
is the pattern for a non-empty list.
– Willem Van Onsem
Nov 13 '18 at 18:16
add a comment |
What islength binListToDec
supposed to do?
– Willem Van Onsem
Nov 13 '18 at 18:14
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
5
but (a) the list you pass is(x:xs)
and (b) the length can never be zero, since(x:xs)
is the pattern for a non-empty list.
– Willem Van Onsem
Nov 13 '18 at 18:16
What is
length binListToDec
supposed to do?– Willem Van Onsem
Nov 13 '18 at 18:14
What is
length binListToDec
supposed to do?– Willem Van Onsem
Nov 13 '18 at 18:14
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
5
5
but (a) the list you pass is
(x:xs)
and (b) the length can never be zero, since (x:xs)
is the pattern for a non-empty list.– Willem Van Onsem
Nov 13 '18 at 18:16
but (a) the list you pass is
(x:xs)
and (b) the length can never be zero, since (x:xs)
is the pattern for a non-empty list.– Willem Van Onsem
Nov 13 '18 at 18:16
add a comment |
1 Answer
1
active
oldest
votes
Among the myriad ways you could write this, two possibilities would be
binListToDec xs = if length xs == 0 then 0 -- see below
else (head xs) + binListToDec (tail xs)
and
binListToDec = 0
binListToDec (x:xs) = x + binListToDec xs
You appear to be trying to combine bits of each. There's no way to write a single pattern that matches simultaneously 1) an empty list and 2) a non-empty list with 3) its head and tail matched separately.
xs
matches 1) and 2).
all@(x:xs)
matches 2) and 3)
1) and 3) cannot be matched, because the pairing is nonsensical: an empty list doesn't have a separate head and tail.and
(x:xs)
match lists from two non-overlapping sets of possible list values.
Update: there is a lazy pattern match
all@(~(x:xs))
. The tilde prevents the match(x:xs)
from being attempted until there is a need to
evaluatex
orxs
. We think of
binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec
as equivalent to
binListToDec all = if length all == 0
then 0
else let (x:xs) = all
in x + binListToDec
A lazy pattern match can still fail, but here we defer using
x
andxs
until we know it won't.
length binListToDec
attempts to compute the length of the function itself, not the length of its argument, in your attempt. The correct argument for length
is used above. Also, the generally accepted sum of an empty list is 0, not 1.
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g.binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.
– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
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%2f53287115%2ftaking-sum-of-list-in-haskell%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
Among the myriad ways you could write this, two possibilities would be
binListToDec xs = if length xs == 0 then 0 -- see below
else (head xs) + binListToDec (tail xs)
and
binListToDec = 0
binListToDec (x:xs) = x + binListToDec xs
You appear to be trying to combine bits of each. There's no way to write a single pattern that matches simultaneously 1) an empty list and 2) a non-empty list with 3) its head and tail matched separately.
xs
matches 1) and 2).
all@(x:xs)
matches 2) and 3)
1) and 3) cannot be matched, because the pairing is nonsensical: an empty list doesn't have a separate head and tail.and
(x:xs)
match lists from two non-overlapping sets of possible list values.
Update: there is a lazy pattern match
all@(~(x:xs))
. The tilde prevents the match(x:xs)
from being attempted until there is a need to
evaluatex
orxs
. We think of
binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec
as equivalent to
binListToDec all = if length all == 0
then 0
else let (x:xs) = all
in x + binListToDec
A lazy pattern match can still fail, but here we defer using
x
andxs
until we know it won't.
length binListToDec
attempts to compute the length of the function itself, not the length of its argument, in your attempt. The correct argument for length
is used above. Also, the generally accepted sum of an empty list is 0, not 1.
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g.binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.
– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
add a comment |
Among the myriad ways you could write this, two possibilities would be
binListToDec xs = if length xs == 0 then 0 -- see below
else (head xs) + binListToDec (tail xs)
and
binListToDec = 0
binListToDec (x:xs) = x + binListToDec xs
You appear to be trying to combine bits of each. There's no way to write a single pattern that matches simultaneously 1) an empty list and 2) a non-empty list with 3) its head and tail matched separately.
xs
matches 1) and 2).
all@(x:xs)
matches 2) and 3)
1) and 3) cannot be matched, because the pairing is nonsensical: an empty list doesn't have a separate head and tail.and
(x:xs)
match lists from two non-overlapping sets of possible list values.
Update: there is a lazy pattern match
all@(~(x:xs))
. The tilde prevents the match(x:xs)
from being attempted until there is a need to
evaluatex
orxs
. We think of
binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec
as equivalent to
binListToDec all = if length all == 0
then 0
else let (x:xs) = all
in x + binListToDec
A lazy pattern match can still fail, but here we defer using
x
andxs
until we know it won't.
length binListToDec
attempts to compute the length of the function itself, not the length of its argument, in your attempt. The correct argument for length
is used above. Also, the generally accepted sum of an empty list is 0, not 1.
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g.binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.
– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
add a comment |
Among the myriad ways you could write this, two possibilities would be
binListToDec xs = if length xs == 0 then 0 -- see below
else (head xs) + binListToDec (tail xs)
and
binListToDec = 0
binListToDec (x:xs) = x + binListToDec xs
You appear to be trying to combine bits of each. There's no way to write a single pattern that matches simultaneously 1) an empty list and 2) a non-empty list with 3) its head and tail matched separately.
xs
matches 1) and 2).
all@(x:xs)
matches 2) and 3)
1) and 3) cannot be matched, because the pairing is nonsensical: an empty list doesn't have a separate head and tail.and
(x:xs)
match lists from two non-overlapping sets of possible list values.
Update: there is a lazy pattern match
all@(~(x:xs))
. The tilde prevents the match(x:xs)
from being attempted until there is a need to
evaluatex
orxs
. We think of
binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec
as equivalent to
binListToDec all = if length all == 0
then 0
else let (x:xs) = all
in x + binListToDec
A lazy pattern match can still fail, but here we defer using
x
andxs
until we know it won't.
length binListToDec
attempts to compute the length of the function itself, not the length of its argument, in your attempt. The correct argument for length
is used above. Also, the generally accepted sum of an empty list is 0, not 1.
Among the myriad ways you could write this, two possibilities would be
binListToDec xs = if length xs == 0 then 0 -- see below
else (head xs) + binListToDec (tail xs)
and
binListToDec = 0
binListToDec (x:xs) = x + binListToDec xs
You appear to be trying to combine bits of each. There's no way to write a single pattern that matches simultaneously 1) an empty list and 2) a non-empty list with 3) its head and tail matched separately.
xs
matches 1) and 2).
all@(x:xs)
matches 2) and 3)
1) and 3) cannot be matched, because the pairing is nonsensical: an empty list doesn't have a separate head and tail.and
(x:xs)
match lists from two non-overlapping sets of possible list values.
Update: there is a lazy pattern match
all@(~(x:xs))
. The tilde prevents the match(x:xs)
from being attempted until there is a need to
evaluatex
orxs
. We think of
binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec
as equivalent to
binListToDec all = if length all == 0
then 0
else let (x:xs) = all
in x + binListToDec
A lazy pattern match can still fail, but here we defer using
x
andxs
until we know it won't.
length binListToDec
attempts to compute the length of the function itself, not the length of its argument, in your attempt. The correct argument for length
is used above. Also, the generally accepted sum of an empty list is 0, not 1.
edited Nov 13 '18 at 22:43
answered Nov 13 '18 at 18:22
chepnerchepner
248k33234329
248k33234329
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g.binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.
– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
add a comment |
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g.binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.
– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g. binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.– Daniel Wagner
Nov 13 '18 at 21:37
all@(~(x:xs))
meets all three criteria. ...but that's not to say I recommend it. Just that it's possible. e.g. binListToDec all@(~(x:xs)) = if length all == 0 then 0 else x + binListToDec xs
would work, though it would be slow for all the same reasons your first solution is.– Daniel Wagner
Nov 13 '18 at 21:37
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
@DanielWagner Thanks; I figured there was some bit of syntax I was forgetting about.
– chepner
Nov 13 '18 at 22:43
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%2f53287115%2ftaking-sum-of-list-in-haskell%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
What is
length binListToDec
supposed to do?– Willem Van Onsem
Nov 13 '18 at 18:14
take the length of the string passed to the function. and if its 0 ( empty) then return 1 as the sum @WillemVanOnsem
– Muhammad Usman
Nov 13 '18 at 18:15
5
but (a) the list you pass is
(x:xs)
and (b) the length can never be zero, since(x:xs)
is the pattern for a non-empty list.– Willem Van Onsem
Nov 13 '18 at 18:16