Taking sum of list in Haskell










2















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









share|improve this question






















  • 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















2















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









share|improve this question






















  • 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













2












2








2


1






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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:10









Muhammad UsmanMuhammad Usman

67118




67118












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















3














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
    evaluate x or xs. 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 and xs 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.






share|improve this answer

























  • 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










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%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









3














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
    evaluate x or xs. 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 and xs 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.






share|improve this answer

























  • 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















3














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
    evaluate x or xs. 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 and xs 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.






share|improve this answer

























  • 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













3












3








3







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
    evaluate x or xs. 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 and xs 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.






share|improve this answer















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
    evaluate x or xs. 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 and xs 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 22:43

























answered Nov 13 '18 at 18:22









chepnerchepner

252k34236331




252k34236331












  • 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












  • @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



















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%2f53287115%2ftaking-sum-of-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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo