Haskell Type Errors with division












-1















I have the below simple program to find the average of a list. I know my error is related to the inference of types but I cannot correct it. Can I get a correction and a simple explanation for this?



average :: Float
average= uncurry (/) . sumlen

sumlen ::[Int]-> (Int,Int)
sumlen = foldl f (0,0)
where f (s,n) x = (s+x,n+1)


The error is:



  • Couldn't match expected type ‘Float’
with actual type ‘[Int] -> Int’
• Probable cause: ‘(.)’ is applied to too few arguments
In the expression: uncurry (/) . sumlen
In an equation for ‘average’: average = uncurry (/) . sumlen









share|improve this question























  • The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

    – sepp2k
    Nov 26 '18 at 10:47






  • 1





    firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

    – Luis Morillo
    Nov 26 '18 at 10:49













  • Possible duplicate of Haskell types frustrating a simple 'average' function

    – Redu
    Nov 26 '18 at 14:31
















-1















I have the below simple program to find the average of a list. I know my error is related to the inference of types but I cannot correct it. Can I get a correction and a simple explanation for this?



average :: Float
average= uncurry (/) . sumlen

sumlen ::[Int]-> (Int,Int)
sumlen = foldl f (0,0)
where f (s,n) x = (s+x,n+1)


The error is:



  • Couldn't match expected type ‘Float’
with actual type ‘[Int] -> Int’
• Probable cause: ‘(.)’ is applied to too few arguments
In the expression: uncurry (/) . sumlen
In an equation for ‘average’: average = uncurry (/) . sumlen









share|improve this question























  • The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

    – sepp2k
    Nov 26 '18 at 10:47






  • 1





    firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

    – Luis Morillo
    Nov 26 '18 at 10:49













  • Possible duplicate of Haskell types frustrating a simple 'average' function

    – Redu
    Nov 26 '18 at 14:31














-1












-1








-1








I have the below simple program to find the average of a list. I know my error is related to the inference of types but I cannot correct it. Can I get a correction and a simple explanation for this?



average :: Float
average= uncurry (/) . sumlen

sumlen ::[Int]-> (Int,Int)
sumlen = foldl f (0,0)
where f (s,n) x = (s+x,n+1)


The error is:



  • Couldn't match expected type ‘Float’
with actual type ‘[Int] -> Int’
• Probable cause: ‘(.)’ is applied to too few arguments
In the expression: uncurry (/) . sumlen
In an equation for ‘average’: average = uncurry (/) . sumlen









share|improve this question














I have the below simple program to find the average of a list. I know my error is related to the inference of types but I cannot correct it. Can I get a correction and a simple explanation for this?



average :: Float
average= uncurry (/) . sumlen

sumlen ::[Int]-> (Int,Int)
sumlen = foldl f (0,0)
where f (s,n) x = (s+x,n+1)


The error is:



  • Couldn't match expected type ‘Float’
with actual type ‘[Int] -> Int’
• Probable cause: ‘(.)’ is applied to too few arguments
In the expression: uncurry (/) . sumlen
In an equation for ‘average’: average = uncurry (/) . sumlen






haskell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 10:43









GakuoGakuo

394213




394213













  • The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

    – sepp2k
    Nov 26 '18 at 10:47






  • 1





    firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

    – Luis Morillo
    Nov 26 '18 at 10:49













  • Possible duplicate of Haskell types frustrating a simple 'average' function

    – Redu
    Nov 26 '18 at 14:31



















  • The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

    – sepp2k
    Nov 26 '18 at 10:47






  • 1





    firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

    – Luis Morillo
    Nov 26 '18 at 10:49













  • Possible duplicate of Haskell types frustrating a simple 'average' function

    – Redu
    Nov 26 '18 at 14:31

















The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

– sepp2k
Nov 26 '18 at 10:47





The average of which list are you trying to find? Do you maybe want average to be a function, not a Float?

– sepp2k
Nov 26 '18 at 10:47




1




1





firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

– Luis Morillo
Nov 26 '18 at 10:49







firstly: average is not a Float. It is a function [Int] -> Float. Also, you should explictly convert your Ints into Float when applying (/)

– Luis Morillo
Nov 26 '18 at 10:49















Possible duplicate of Haskell types frustrating a simple 'average' function

– Redu
Nov 26 '18 at 14:31





Possible duplicate of Haskell types frustrating a simple 'average' function

– Redu
Nov 26 '18 at 14:31












1 Answer
1






active

oldest

votes


















4














Summary: Haskell is a strongly typed language.



You force the type of sumlen to be [Int] -> (Int,Int).



The type of (/) is Fractional a => a -> a -> a.



Let us typecheck your function. First we will try to infer its type:



average = (uncurry (/)) . sumlen 
uncurry :: (a -> b -> c) -> (a,b) -> c
(/) :: Fractional d => d -> d -> d
(.) :: (f -> g) -> (e -> f) -> e -> g
sumlen :: [Int] -> (Int,Int)
So:
e = [Int]
f = (Int,Int) = (a,b) = (d,d)
g = d = Int
average :: Fractional Int => [Int] -> Int


Now you tell Haskell that average :: Float and Haskell tells you that a float is not a function. This is your error.



If you remove the annotation then you will be told that there is no instance Fractional Int. This is because you can’t represent or reasonably approximate 1 / 2 as an Int.



How to fix this?



Step 0 is to not write down a type signature that is wrong (ie average :: Float)



One thing you could do is have a more general sumlen. Try Num a => [a] -> (a,a) instead. You could even have (Num a, Num b) => [a] -> (a,b). Then you can have the sensible type average :: Fractional a => [a] -> a



Another thing you could do is convert from integers:



average xs = fromIntegral s / fromIntegral l where
(s,l) = sumlen xs





share|improve this answer


























    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%2f53479379%2fhaskell-type-errors-with-division%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









    4














    Summary: Haskell is a strongly typed language.



    You force the type of sumlen to be [Int] -> (Int,Int).



    The type of (/) is Fractional a => a -> a -> a.



    Let us typecheck your function. First we will try to infer its type:



    average = (uncurry (/)) . sumlen 
    uncurry :: (a -> b -> c) -> (a,b) -> c
    (/) :: Fractional d => d -> d -> d
    (.) :: (f -> g) -> (e -> f) -> e -> g
    sumlen :: [Int] -> (Int,Int)
    So:
    e = [Int]
    f = (Int,Int) = (a,b) = (d,d)
    g = d = Int
    average :: Fractional Int => [Int] -> Int


    Now you tell Haskell that average :: Float and Haskell tells you that a float is not a function. This is your error.



    If you remove the annotation then you will be told that there is no instance Fractional Int. This is because you can’t represent or reasonably approximate 1 / 2 as an Int.



    How to fix this?



    Step 0 is to not write down a type signature that is wrong (ie average :: Float)



    One thing you could do is have a more general sumlen. Try Num a => [a] -> (a,a) instead. You could even have (Num a, Num b) => [a] -> (a,b). Then you can have the sensible type average :: Fractional a => [a] -> a



    Another thing you could do is convert from integers:



    average xs = fromIntegral s / fromIntegral l where
    (s,l) = sumlen xs





    share|improve this answer






























      4














      Summary: Haskell is a strongly typed language.



      You force the type of sumlen to be [Int] -> (Int,Int).



      The type of (/) is Fractional a => a -> a -> a.



      Let us typecheck your function. First we will try to infer its type:



      average = (uncurry (/)) . sumlen 
      uncurry :: (a -> b -> c) -> (a,b) -> c
      (/) :: Fractional d => d -> d -> d
      (.) :: (f -> g) -> (e -> f) -> e -> g
      sumlen :: [Int] -> (Int,Int)
      So:
      e = [Int]
      f = (Int,Int) = (a,b) = (d,d)
      g = d = Int
      average :: Fractional Int => [Int] -> Int


      Now you tell Haskell that average :: Float and Haskell tells you that a float is not a function. This is your error.



      If you remove the annotation then you will be told that there is no instance Fractional Int. This is because you can’t represent or reasonably approximate 1 / 2 as an Int.



      How to fix this?



      Step 0 is to not write down a type signature that is wrong (ie average :: Float)



      One thing you could do is have a more general sumlen. Try Num a => [a] -> (a,a) instead. You could even have (Num a, Num b) => [a] -> (a,b). Then you can have the sensible type average :: Fractional a => [a] -> a



      Another thing you could do is convert from integers:



      average xs = fromIntegral s / fromIntegral l where
      (s,l) = sumlen xs





      share|improve this answer




























        4












        4








        4







        Summary: Haskell is a strongly typed language.



        You force the type of sumlen to be [Int] -> (Int,Int).



        The type of (/) is Fractional a => a -> a -> a.



        Let us typecheck your function. First we will try to infer its type:



        average = (uncurry (/)) . sumlen 
        uncurry :: (a -> b -> c) -> (a,b) -> c
        (/) :: Fractional d => d -> d -> d
        (.) :: (f -> g) -> (e -> f) -> e -> g
        sumlen :: [Int] -> (Int,Int)
        So:
        e = [Int]
        f = (Int,Int) = (a,b) = (d,d)
        g = d = Int
        average :: Fractional Int => [Int] -> Int


        Now you tell Haskell that average :: Float and Haskell tells you that a float is not a function. This is your error.



        If you remove the annotation then you will be told that there is no instance Fractional Int. This is because you can’t represent or reasonably approximate 1 / 2 as an Int.



        How to fix this?



        Step 0 is to not write down a type signature that is wrong (ie average :: Float)



        One thing you could do is have a more general sumlen. Try Num a => [a] -> (a,a) instead. You could even have (Num a, Num b) => [a] -> (a,b). Then you can have the sensible type average :: Fractional a => [a] -> a



        Another thing you could do is convert from integers:



        average xs = fromIntegral s / fromIntegral l where
        (s,l) = sumlen xs





        share|improve this answer















        Summary: Haskell is a strongly typed language.



        You force the type of sumlen to be [Int] -> (Int,Int).



        The type of (/) is Fractional a => a -> a -> a.



        Let us typecheck your function. First we will try to infer its type:



        average = (uncurry (/)) . sumlen 
        uncurry :: (a -> b -> c) -> (a,b) -> c
        (/) :: Fractional d => d -> d -> d
        (.) :: (f -> g) -> (e -> f) -> e -> g
        sumlen :: [Int] -> (Int,Int)
        So:
        e = [Int]
        f = (Int,Int) = (a,b) = (d,d)
        g = d = Int
        average :: Fractional Int => [Int] -> Int


        Now you tell Haskell that average :: Float and Haskell tells you that a float is not a function. This is your error.



        If you remove the annotation then you will be told that there is no instance Fractional Int. This is because you can’t represent or reasonably approximate 1 / 2 as an Int.



        How to fix this?



        Step 0 is to not write down a type signature that is wrong (ie average :: Float)



        One thing you could do is have a more general sumlen. Try Num a => [a] -> (a,a) instead. You could even have (Num a, Num b) => [a] -> (a,b). Then you can have the sensible type average :: Fractional a => [a] -> a



        Another thing you could do is convert from integers:



        average xs = fromIntegral s / fromIntegral l where
        (s,l) = sumlen xs






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 15 '18 at 14:07

























        answered Nov 26 '18 at 11:24









        Dan RobertsonDan Robertson

        3,443716




        3,443716
































            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%2f53479379%2fhaskell-type-errors-with-division%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin