Calling a function with user defined datatypes in Haskell











up vote
1
down vote

favorite












I defined a data type in Haskell



data List a=Nil
|Cons a (List a)


I wrote a function using this data type



listLength Nil=0
listLength (Cons x xs)=1+listLength(xs)


I tried to call that function giving arguments like this



listLength (Cons 2 [2,3])


But I got an error:



<interactive>:68:20: error:
* Couldn't match expected type `List Integer'
with actual type `[Integer]'
* In the second argument of `Cons', namely `[2, 3]'
In the first argument of `listLength', namely `(Cons 2 [2, 3])'
In the expression: listLength (Cons 2 [2, 3])


How do call this function?










share|improve this question


















  • 4




    Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
    – bradrn
    Nov 11 at 6:41










  • It worked. Thank you
    – Nishara Kavindi
    Nov 11 at 6:49






  • 2




    To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
    – chi
    Nov 11 at 8:12






  • 1




    @Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
    – bradrn
    Nov 11 at 8:36










  • @bradrn Okey.You can do that.
    – Nishara Kavindi
    Nov 12 at 16:52

















up vote
1
down vote

favorite












I defined a data type in Haskell



data List a=Nil
|Cons a (List a)


I wrote a function using this data type



listLength Nil=0
listLength (Cons x xs)=1+listLength(xs)


I tried to call that function giving arguments like this



listLength (Cons 2 [2,3])


But I got an error:



<interactive>:68:20: error:
* Couldn't match expected type `List Integer'
with actual type `[Integer]'
* In the second argument of `Cons', namely `[2, 3]'
In the first argument of `listLength', namely `(Cons 2 [2, 3])'
In the expression: listLength (Cons 2 [2, 3])


How do call this function?










share|improve this question


















  • 4




    Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
    – bradrn
    Nov 11 at 6:41










  • It worked. Thank you
    – Nishara Kavindi
    Nov 11 at 6:49






  • 2




    To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
    – chi
    Nov 11 at 8:12






  • 1




    @Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
    – bradrn
    Nov 11 at 8:36










  • @bradrn Okey.You can do that.
    – Nishara Kavindi
    Nov 12 at 16:52















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I defined a data type in Haskell



data List a=Nil
|Cons a (List a)


I wrote a function using this data type



listLength Nil=0
listLength (Cons x xs)=1+listLength(xs)


I tried to call that function giving arguments like this



listLength (Cons 2 [2,3])


But I got an error:



<interactive>:68:20: error:
* Couldn't match expected type `List Integer'
with actual type `[Integer]'
* In the second argument of `Cons', namely `[2, 3]'
In the first argument of `listLength', namely `(Cons 2 [2, 3])'
In the expression: listLength (Cons 2 [2, 3])


How do call this function?










share|improve this question













I defined a data type in Haskell



data List a=Nil
|Cons a (List a)


I wrote a function using this data type



listLength Nil=0
listLength (Cons x xs)=1+listLength(xs)


I tried to call that function giving arguments like this



listLength (Cons 2 [2,3])


But I got an error:



<interactive>:68:20: error:
* Couldn't match expected type `List Integer'
with actual type `[Integer]'
* In the second argument of `Cons', namely `[2, 3]'
In the first argument of `listLength', namely `(Cons 2 [2, 3])'
In the expression: listLength (Cons 2 [2, 3])


How do call this function?







haskell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 6:37









Nishara Kavindi

268




268








  • 4




    Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
    – bradrn
    Nov 11 at 6:41










  • It worked. Thank you
    – Nishara Kavindi
    Nov 11 at 6:49






  • 2




    To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
    – chi
    Nov 11 at 8:12






  • 1




    @Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
    – bradrn
    Nov 11 at 8:36










  • @bradrn Okey.You can do that.
    – Nishara Kavindi
    Nov 12 at 16:52
















  • 4




    Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
    – bradrn
    Nov 11 at 6:41










  • It worked. Thank you
    – Nishara Kavindi
    Nov 11 at 6:49






  • 2




    To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
    – chi
    Nov 11 at 8:12






  • 1




    @Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
    – bradrn
    Nov 11 at 8:36










  • @bradrn Okey.You can do that.
    – Nishara Kavindi
    Nov 12 at 16:52










4




4




Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
– bradrn
Nov 11 at 6:41




Try listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!
– bradrn
Nov 11 at 6:41












It worked. Thank you
– Nishara Kavindi
Nov 11 at 6:49




It worked. Thank you
– Nishara Kavindi
Nov 11 at 6:49




2




2




To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
– chi
Nov 11 at 8:12




To ease testing you can define and use some auxiliary conversion function like fromList = foldr Cons Nil and then write listLength (fromList [1,7,2,5])). In this way you can convert standard lists to your lists before testing, and avoid to type all the Conses.
– chi
Nov 11 at 8:12




1




1




@Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
– bradrn
Nov 11 at 8:36




@Nishara Kavindi Do you want me to turn my comment into a proper answer so this question can be marked as 'answered'?
– bradrn
Nov 11 at 8:36












@bradrn Okey.You can do that.
– Nishara Kavindi
Nov 12 at 16:52






@bradrn Okey.You can do that.
– Nishara Kavindi
Nov 12 at 16:52














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










From my comment above:




Try using listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!







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',
    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%2f53246438%2fcalling-a-function-with-user-defined-datatypes-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








    up vote
    0
    down vote



    accepted










    From my comment above:




    Try using listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!







    share|improve this answer

























      up vote
      0
      down vote



      accepted










      From my comment above:




      Try using listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!







      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        From my comment above:




        Try using listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!







        share|improve this answer












        From my comment above:




        Try using listLength (Cons 2 (Cons 2 Nil)). The issue is that [2,3] has type [Integer], but the Cons constructor requires a second argument of type List Integer, and due to Haskell's strong typing the two types are not the same!








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 0:12









        bradrn

        18529




        18529






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246438%2fcalling-a-function-with-user-defined-datatypes-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

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly