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?
haskell
add a comment |
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?
haskell
4
TrylistLength (Cons 2 (Cons 2 Nil))
. The issue is that[2,3]
has type[Integer]
, but theCons
constructor requires a second argument of typeList 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 likefromList = foldr Cons Nil
and then writelistLength (fromList [1,7,2,5]))
. In this way you can convert standard lists to your lists before testing, and avoid to type all theCons
es.
– 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
add a comment |
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?
haskell
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
haskell
asked Nov 11 at 6:37
Nishara Kavindi
268
268
4
TrylistLength (Cons 2 (Cons 2 Nil))
. The issue is that[2,3]
has type[Integer]
, but theCons
constructor requires a second argument of typeList 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 likefromList = foldr Cons Nil
and then writelistLength (fromList [1,7,2,5]))
. In this way you can convert standard lists to your lists before testing, and avoid to type all theCons
es.
– 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
add a comment |
4
TrylistLength (Cons 2 (Cons 2 Nil))
. The issue is that[2,3]
has type[Integer]
, but theCons
constructor requires a second argument of typeList 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 likefromList = foldr Cons Nil
and then writelistLength (fromList [1,7,2,5]))
. In this way you can convert standard lists to your lists before testing, and avoid to type all theCons
es.
– 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 Cons
es.– 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 Cons
es.– 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
add a comment |
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 theCons
constructor requires a second argument of typeList Integer
, and due to Haskell's strong typing the two types are not the same!
add a comment |
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 theCons
constructor requires a second argument of typeList Integer
, and due to Haskell's strong typing the two types are not the same!
add a comment |
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 theCons
constructor requires a second argument of typeList Integer
, and due to Haskell's strong typing the two types are not the same!
add a comment |
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 theCons
constructor requires a second argument of typeList Integer
, and due to Haskell's strong typing the two types are not the same!
From my comment above:
Try using
listLength (Cons 2 (Cons 2 Nil))
. The issue is that[2,3]
has type[Integer]
, but theCons
constructor requires a second argument of typeList Integer
, and due to Haskell's strong typing the two types are not the same!
answered Nov 13 at 0:12
bradrn
350310
350310
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53246438%2fcalling-a-function-with-user-defined-datatypes-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
4
Try
listLength (Cons 2 (Cons 2 Nil))
. The issue is that[2,3]
has type[Integer]
, but theCons
constructor requires a second argument of typeList 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 writelistLength (fromList [1,7,2,5]))
. In this way you can convert standard lists to your lists before testing, and avoid to type all theCons
es.– 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