Haskell - counting the number of occurrences of a value in a list
So still working through Haskell tutorial...
One problem posed, is to write a function using:
count :: Eq a => [a] -> a -> Int
That can take a list of numbers & a value, and tell you how many times the value you specify occurs in the list.
It says to see if you can write it using List Comprehension, and again using Explicit Recursion...
AND, to use it to not just count occurrences of numbers -- but of letters, for instance, how many times does 's' occur in 'she sells sea shells'.
So I got:
countListComp :: Eq a => [a] -> a -> Int
countListComp find = 0
countListComp ys find = length xs
where xs = [xs | xs <- ys, xs == find]
and:
countRecursion :: Eq a => [a] -> a -> Int
countRecursion find = 0
countRecursion (x:xs) find
| find == x = 1 + (countRecursion xs find)
| otherwise = countRecursion xs find
So it's counting the occurrences of numbers in a list just fine, like so:
ghci > countListComp [1,3,2,3,4,3] 3
3
ghci > countRecursion [6,9,7,9,8,9] 9
3
but when i look for a specific letter, it does this:
ghci > countListComp ["she sells sea shells"] "s"
0
ghci > countRecursion ["she sells sea shells"] "s"
0
it also said to try to count something else 'countable', like how many lists are there... so I tried:
ghci > countListComp [[1,2,3],[3,2,1],[4,5,6]]
0
is there something wrong with my code, or am I not specifying what to look for correctly? I'm thinking it's the latter... because the following works:
For example, looking for how many times 's' occurs in 'she sells sea shells'... do I really have to put each individual letter in quotes with a comma between?? Like:
ghci > countRecursion ['s','h','e',' ','s','e','l','l','s',' ','s','e','a',' ','s','h','e','l','l','s'] 's'
6
And do I have to look for a specific list? Or is there a way to look for just a list with anything in it?
haskell
add a comment |
So still working through Haskell tutorial...
One problem posed, is to write a function using:
count :: Eq a => [a] -> a -> Int
That can take a list of numbers & a value, and tell you how many times the value you specify occurs in the list.
It says to see if you can write it using List Comprehension, and again using Explicit Recursion...
AND, to use it to not just count occurrences of numbers -- but of letters, for instance, how many times does 's' occur in 'she sells sea shells'.
So I got:
countListComp :: Eq a => [a] -> a -> Int
countListComp find = 0
countListComp ys find = length xs
where xs = [xs | xs <- ys, xs == find]
and:
countRecursion :: Eq a => [a] -> a -> Int
countRecursion find = 0
countRecursion (x:xs) find
| find == x = 1 + (countRecursion xs find)
| otherwise = countRecursion xs find
So it's counting the occurrences of numbers in a list just fine, like so:
ghci > countListComp [1,3,2,3,4,3] 3
3
ghci > countRecursion [6,9,7,9,8,9] 9
3
but when i look for a specific letter, it does this:
ghci > countListComp ["she sells sea shells"] "s"
0
ghci > countRecursion ["she sells sea shells"] "s"
0
it also said to try to count something else 'countable', like how many lists are there... so I tried:
ghci > countListComp [[1,2,3],[3,2,1],[4,5,6]]
0
is there something wrong with my code, or am I not specifying what to look for correctly? I'm thinking it's the latter... because the following works:
For example, looking for how many times 's' occurs in 'she sells sea shells'... do I really have to put each individual letter in quotes with a comma between?? Like:
ghci > countRecursion ['s','h','e',' ','s','e','l','l','s',' ','s','e','a',' ','s','h','e','l','l','s'] 's'
6
And do I have to look for a specific list? Or is there a way to look for just a list with anything in it?
haskell
1
"foo"
and["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e.,"foo"
and['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; theOverloadedStrings
extension, if you choose to use it, generalizes"..."
from typeString
to typeIsString p => p
.)
– chepner
Nov 16 '18 at 14:10
1
A side exercise for you: suppose you leave off the base case ofcountListComp
(that is, suppose you delete the linecountListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?
– Daniel Wagner
Nov 16 '18 at 15:17
add a comment |
So still working through Haskell tutorial...
One problem posed, is to write a function using:
count :: Eq a => [a] -> a -> Int
That can take a list of numbers & a value, and tell you how many times the value you specify occurs in the list.
It says to see if you can write it using List Comprehension, and again using Explicit Recursion...
AND, to use it to not just count occurrences of numbers -- but of letters, for instance, how many times does 's' occur in 'she sells sea shells'.
So I got:
countListComp :: Eq a => [a] -> a -> Int
countListComp find = 0
countListComp ys find = length xs
where xs = [xs | xs <- ys, xs == find]
and:
countRecursion :: Eq a => [a] -> a -> Int
countRecursion find = 0
countRecursion (x:xs) find
| find == x = 1 + (countRecursion xs find)
| otherwise = countRecursion xs find
So it's counting the occurrences of numbers in a list just fine, like so:
ghci > countListComp [1,3,2,3,4,3] 3
3
ghci > countRecursion [6,9,7,9,8,9] 9
3
but when i look for a specific letter, it does this:
ghci > countListComp ["she sells sea shells"] "s"
0
ghci > countRecursion ["she sells sea shells"] "s"
0
it also said to try to count something else 'countable', like how many lists are there... so I tried:
ghci > countListComp [[1,2,3],[3,2,1],[4,5,6]]
0
is there something wrong with my code, or am I not specifying what to look for correctly? I'm thinking it's the latter... because the following works:
For example, looking for how many times 's' occurs in 'she sells sea shells'... do I really have to put each individual letter in quotes with a comma between?? Like:
ghci > countRecursion ['s','h','e',' ','s','e','l','l','s',' ','s','e','a',' ','s','h','e','l','l','s'] 's'
6
And do I have to look for a specific list? Or is there a way to look for just a list with anything in it?
haskell
So still working through Haskell tutorial...
One problem posed, is to write a function using:
count :: Eq a => [a] -> a -> Int
That can take a list of numbers & a value, and tell you how many times the value you specify occurs in the list.
It says to see if you can write it using List Comprehension, and again using Explicit Recursion...
AND, to use it to not just count occurrences of numbers -- but of letters, for instance, how many times does 's' occur in 'she sells sea shells'.
So I got:
countListComp :: Eq a => [a] -> a -> Int
countListComp find = 0
countListComp ys find = length xs
where xs = [xs | xs <- ys, xs == find]
and:
countRecursion :: Eq a => [a] -> a -> Int
countRecursion find = 0
countRecursion (x:xs) find
| find == x = 1 + (countRecursion xs find)
| otherwise = countRecursion xs find
So it's counting the occurrences of numbers in a list just fine, like so:
ghci > countListComp [1,3,2,3,4,3] 3
3
ghci > countRecursion [6,9,7,9,8,9] 9
3
but when i look for a specific letter, it does this:
ghci > countListComp ["she sells sea shells"] "s"
0
ghci > countRecursion ["she sells sea shells"] "s"
0
it also said to try to count something else 'countable', like how many lists are there... so I tried:
ghci > countListComp [[1,2,3],[3,2,1],[4,5,6]]
0
is there something wrong with my code, or am I not specifying what to look for correctly? I'm thinking it's the latter... because the following works:
For example, looking for how many times 's' occurs in 'she sells sea shells'... do I really have to put each individual letter in quotes with a comma between?? Like:
ghci > countRecursion ['s','h','e',' ','s','e','l','l','s',' ','s','e','a',' ','s','h','e','l','l','s'] 's'
6
And do I have to look for a specific list? Or is there a way to look for just a list with anything in it?
haskell
haskell
asked Nov 16 '18 at 7:39
StormyStormy
673
673
1
"foo"
and["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e.,"foo"
and['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; theOverloadedStrings
extension, if you choose to use it, generalizes"..."
from typeString
to typeIsString p => p
.)
– chepner
Nov 16 '18 at 14:10
1
A side exercise for you: suppose you leave off the base case ofcountListComp
(that is, suppose you delete the linecountListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?
– Daniel Wagner
Nov 16 '18 at 15:17
add a comment |
1
"foo"
and["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e.,"foo"
and['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; theOverloadedStrings
extension, if you choose to use it, generalizes"..."
from typeString
to typeIsString p => p
.)
– chepner
Nov 16 '18 at 14:10
1
A side exercise for you: suppose you leave off the base case ofcountListComp
(that is, suppose you delete the linecountListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?
– Daniel Wagner
Nov 16 '18 at 15:17
1
1
"foo"
and ["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e., "foo"
and ['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; the OverloadedStrings
extension, if you choose to use it, generalizes "..."
from type String
to type IsString p => p
.)– chepner
Nov 16 '18 at 14:10
"foo"
and ["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e., "foo"
and ['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; the OverloadedStrings
extension, if you choose to use it, generalizes "..."
from type String
to type IsString p => p
.)– chepner
Nov 16 '18 at 14:10
1
1
A side exercise for you: suppose you leave off the base case of
countListComp
(that is, suppose you delete the line countListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?– Daniel Wagner
Nov 16 '18 at 15:17
A side exercise for you: suppose you leave off the base case of
countListComp
(that is, suppose you delete the line countListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?– Daniel Wagner
Nov 16 '18 at 15:17
add a comment |
3 Answers
3
active
oldest
votes
Problem with countListComp ["she sells sea shells"] "s"
is you have list of string.
You probably mean countListComp "she sells sea shells" 's'
Sting is just alias to list of character.
With countListComp [[1,2,3],[3,2,1],[4,5,6]]
is different problem. It doesn't count how many list you have. It count how many list equals to you have.
If you try countListComp [[1,2,3],,[4,5,6]]
or countListComp [[1,2,3],[3,2,1],[4,5,6]] [3,2,1]
you get 1
.
add a comment |
Try seeing what the first item in "she sells sea shells"
is:
ghci> head "she sells sea shells"
=> 's'
's' is a Char, while "s" is a single-item [Char].
add a comment |
In my opinion, you have two mistakes here.
First, when you pass ["she sells sea shells"]
to your function you actually pas a list of list of chars to your function. So function call should be as the following.
countListComp "she sells sea shells" <second_parameter>
Second problem in the function call is, String is a list of chars, and list in Haskell consists of a head and tail list. So when you pass "s"
as string, instead of char, you actually pass ['s',]
. So the right function call should be:
countListComp "she sells sea shells" 's'
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%2f53333388%2fhaskell-counting-the-number-of-occurrences-of-a-value-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Problem with countListComp ["she sells sea shells"] "s"
is you have list of string.
You probably mean countListComp "she sells sea shells" 's'
Sting is just alias to list of character.
With countListComp [[1,2,3],[3,2,1],[4,5,6]]
is different problem. It doesn't count how many list you have. It count how many list equals to you have.
If you try countListComp [[1,2,3],,[4,5,6]]
or countListComp [[1,2,3],[3,2,1],[4,5,6]] [3,2,1]
you get 1
.
add a comment |
Problem with countListComp ["she sells sea shells"] "s"
is you have list of string.
You probably mean countListComp "she sells sea shells" 's'
Sting is just alias to list of character.
With countListComp [[1,2,3],[3,2,1],[4,5,6]]
is different problem. It doesn't count how many list you have. It count how many list equals to you have.
If you try countListComp [[1,2,3],,[4,5,6]]
or countListComp [[1,2,3],[3,2,1],[4,5,6]] [3,2,1]
you get 1
.
add a comment |
Problem with countListComp ["she sells sea shells"] "s"
is you have list of string.
You probably mean countListComp "she sells sea shells" 's'
Sting is just alias to list of character.
With countListComp [[1,2,3],[3,2,1],[4,5,6]]
is different problem. It doesn't count how many list you have. It count how many list equals to you have.
If you try countListComp [[1,2,3],,[4,5,6]]
or countListComp [[1,2,3],[3,2,1],[4,5,6]] [3,2,1]
you get 1
.
Problem with countListComp ["she sells sea shells"] "s"
is you have list of string.
You probably mean countListComp "she sells sea shells" 's'
Sting is just alias to list of character.
With countListComp [[1,2,3],[3,2,1],[4,5,6]]
is different problem. It doesn't count how many list you have. It count how many list equals to you have.
If you try countListComp [[1,2,3],,[4,5,6]]
or countListComp [[1,2,3],[3,2,1],[4,5,6]] [3,2,1]
you get 1
.
answered Nov 16 '18 at 7:47
talextalex
11.9k11749
11.9k11749
add a comment |
add a comment |
Try seeing what the first item in "she sells sea shells"
is:
ghci> head "she sells sea shells"
=> 's'
's' is a Char, while "s" is a single-item [Char].
add a comment |
Try seeing what the first item in "she sells sea shells"
is:
ghci> head "she sells sea shells"
=> 's'
's' is a Char, while "s" is a single-item [Char].
add a comment |
Try seeing what the first item in "she sells sea shells"
is:
ghci> head "she sells sea shells"
=> 's'
's' is a Char, while "s" is a single-item [Char].
Try seeing what the first item in "she sells sea shells"
is:
ghci> head "she sells sea shells"
=> 's'
's' is a Char, while "s" is a single-item [Char].
answered Nov 16 '18 at 7:48
amalloyamalloy
60.3k6106158
60.3k6106158
add a comment |
add a comment |
In my opinion, you have two mistakes here.
First, when you pass ["she sells sea shells"]
to your function you actually pas a list of list of chars to your function. So function call should be as the following.
countListComp "she sells sea shells" <second_parameter>
Second problem in the function call is, String is a list of chars, and list in Haskell consists of a head and tail list. So when you pass "s"
as string, instead of char, you actually pass ['s',]
. So the right function call should be:
countListComp "she sells sea shells" 's'
add a comment |
In my opinion, you have two mistakes here.
First, when you pass ["she sells sea shells"]
to your function you actually pas a list of list of chars to your function. So function call should be as the following.
countListComp "she sells sea shells" <second_parameter>
Second problem in the function call is, String is a list of chars, and list in Haskell consists of a head and tail list. So when you pass "s"
as string, instead of char, you actually pass ['s',]
. So the right function call should be:
countListComp "she sells sea shells" 's'
add a comment |
In my opinion, you have two mistakes here.
First, when you pass ["she sells sea shells"]
to your function you actually pas a list of list of chars to your function. So function call should be as the following.
countListComp "she sells sea shells" <second_parameter>
Second problem in the function call is, String is a list of chars, and list in Haskell consists of a head and tail list. So when you pass "s"
as string, instead of char, you actually pass ['s',]
. So the right function call should be:
countListComp "she sells sea shells" 's'
In my opinion, you have two mistakes here.
First, when you pass ["she sells sea shells"]
to your function you actually pas a list of list of chars to your function. So function call should be as the following.
countListComp "she sells sea shells" <second_parameter>
Second problem in the function call is, String is a list of chars, and list in Haskell consists of a head and tail list. So when you pass "s"
as string, instead of char, you actually pass ['s',]
. So the right function call should be:
countListComp "she sells sea shells" 's'
answered Nov 16 '18 at 8:10
ozataozata
4101515
4101515
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.
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%2f53333388%2fhaskell-counting-the-number-of-occurrences-of-a-value-in-a-list%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
1
"foo"
and["foo"]
are completely different values. A double-quoted string of characters is just syntactic sugar for the list, i.e.,"foo"
and['f', 'o', 'o']
are exactly the same thing. (At least, in standard Haskell; theOverloadedStrings
extension, if you choose to use it, generalizes"..."
from typeString
to typeIsString p => p
.)– chepner
Nov 16 '18 at 14:10
1
A side exercise for you: suppose you leave off the base case of
countListComp
(that is, suppose you delete the linecountListComp find = 0
). What do you predict will go wrong? Does ghc agree with your prediction?– Daniel Wagner
Nov 16 '18 at 15:17