Combining map and length in Haskell
I have two problems both of them with using map and length
The first one should give me back the word count but instead it only count the elements in the list.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "thello world "] => 4--instead it have six words
The second is trickier because it should give back one int for the whole list. I can only count the characters of the individual elements, not the whole.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
list haskell map-function
add a comment |
I have two problems both of them with using map and length
The first one should give me back the word count but instead it only count the elements in the list.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "thello world "] => 4--instead it have six words
The second is trickier because it should give back one int for the whole list. I can only count the characters of the individual elements, not the whole.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
list haskell map-function
1
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
1
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
1
1. Find out whatmap words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result ofcountChars
related to the actual result? What is missing to transform one to the other?
– n.m.
Nov 25 '18 at 8:29
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
1
1.Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, whatlength 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot usemap
for that.
– n.m.
Nov 25 '18 at 9:22
add a comment |
I have two problems both of them with using map and length
The first one should give me back the word count but instead it only count the elements in the list.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "thello world "] => 4--instead it have six words
The second is trickier because it should give back one int for the whole list. I can only count the characters of the individual elements, not the whole.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
list haskell map-function
I have two problems both of them with using map and length
The first one should give me back the word count but instead it only count the elements in the list.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "thello world "] => 4--instead it have six words
The second is trickier because it should give back one int for the whole list. I can only count the characters of the individual elements, not the whole.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
list haskell map-function
list haskell map-function
edited Nov 25 '18 at 8:24
Robin Green
22.6k876156
22.6k876156
asked Nov 25 '18 at 7:59
Akos FajsziAkos Fajszi
445
445
1
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
1
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
1
1. Find out whatmap words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result ofcountChars
related to the actual result? What is missing to transform one to the other?
– n.m.
Nov 25 '18 at 8:29
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
1
1.Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, whatlength 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot usemap
for that.
– n.m.
Nov 25 '18 at 9:22
add a comment |
1
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
1
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
1
1. Find out whatmap words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result ofcountChars
related to the actual result? What is missing to transform one to the other?
– n.m.
Nov 25 '18 at 8:29
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
1
1.Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, whatlength 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot usemap
for that.
– n.m.
Nov 25 '18 at 9:22
1
1
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
1
1
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
1
1
1. Find out what
map words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result of countChars
related to the actual result? What is missing to transform one to the other?– n.m.
Nov 25 '18 at 8:29
1. Find out what
map words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result of countChars
related to the actual result? What is missing to transform one to the other?– n.m.
Nov 25 '18 at 8:29
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
1
1
1.
Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, what length 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot use map
for that.– n.m.
Nov 25 '18 at 9:22
1.
Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, what length 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot use map
for that.– n.m.
Nov 25 '18 at 9:22
add a comment |
1 Answer
1
active
oldest
votes
For the second one, you just need to call sum
on the result.
countChars xs = sum (map (w -> length (w)) xs)
which can also be rewritten as
countChars xs = sum $ map length xs
For the first one, we'll have to calculate the number of words in each element, and finally sum the result.
words
will give you a list of the words, so after doing map (words) xs
(no need to have parentheses around words btw), you'll get the following:
map words ["asd qwe", "-- Foo", "", "thello world "]
=>
[["asd","qwe"],["--","Foo"],,["hello","world"]]
The first thing you want to do is to get the length of each sublist, which you could fit into your map
map (x -> length (words x)) xs
Now, the result is:
[2,2,0,2]
And by running sum on the result, you get 6. So the end result is
countWords :: [String]-> Int
countWords xs = sum $ map (x -> length (words x)) xs
With some syntax sugar, you could do the following, but I find that most beginners are confused by it:
countWords xs = sum $ map (length . words) xs
or even better
countWords = sum . map (length . words)
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%2f53465667%2fcombining-map-and-length-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
For the second one, you just need to call sum
on the result.
countChars xs = sum (map (w -> length (w)) xs)
which can also be rewritten as
countChars xs = sum $ map length xs
For the first one, we'll have to calculate the number of words in each element, and finally sum the result.
words
will give you a list of the words, so after doing map (words) xs
(no need to have parentheses around words btw), you'll get the following:
map words ["asd qwe", "-- Foo", "", "thello world "]
=>
[["asd","qwe"],["--","Foo"],,["hello","world"]]
The first thing you want to do is to get the length of each sublist, which you could fit into your map
map (x -> length (words x)) xs
Now, the result is:
[2,2,0,2]
And by running sum on the result, you get 6. So the end result is
countWords :: [String]-> Int
countWords xs = sum $ map (x -> length (words x)) xs
With some syntax sugar, you could do the following, but I find that most beginners are confused by it:
countWords xs = sum $ map (length . words) xs
or even better
countWords = sum . map (length . words)
add a comment |
For the second one, you just need to call sum
on the result.
countChars xs = sum (map (w -> length (w)) xs)
which can also be rewritten as
countChars xs = sum $ map length xs
For the first one, we'll have to calculate the number of words in each element, and finally sum the result.
words
will give you a list of the words, so after doing map (words) xs
(no need to have parentheses around words btw), you'll get the following:
map words ["asd qwe", "-- Foo", "", "thello world "]
=>
[["asd","qwe"],["--","Foo"],,["hello","world"]]
The first thing you want to do is to get the length of each sublist, which you could fit into your map
map (x -> length (words x)) xs
Now, the result is:
[2,2,0,2]
And by running sum on the result, you get 6. So the end result is
countWords :: [String]-> Int
countWords xs = sum $ map (x -> length (words x)) xs
With some syntax sugar, you could do the following, but I find that most beginners are confused by it:
countWords xs = sum $ map (length . words) xs
or even better
countWords = sum . map (length . words)
add a comment |
For the second one, you just need to call sum
on the result.
countChars xs = sum (map (w -> length (w)) xs)
which can also be rewritten as
countChars xs = sum $ map length xs
For the first one, we'll have to calculate the number of words in each element, and finally sum the result.
words
will give you a list of the words, so after doing map (words) xs
(no need to have parentheses around words btw), you'll get the following:
map words ["asd qwe", "-- Foo", "", "thello world "]
=>
[["asd","qwe"],["--","Foo"],,["hello","world"]]
The first thing you want to do is to get the length of each sublist, which you could fit into your map
map (x -> length (words x)) xs
Now, the result is:
[2,2,0,2]
And by running sum on the result, you get 6. So the end result is
countWords :: [String]-> Int
countWords xs = sum $ map (x -> length (words x)) xs
With some syntax sugar, you could do the following, but I find that most beginners are confused by it:
countWords xs = sum $ map (length . words) xs
or even better
countWords = sum . map (length . words)
For the second one, you just need to call sum
on the result.
countChars xs = sum (map (w -> length (w)) xs)
which can also be rewritten as
countChars xs = sum $ map length xs
For the first one, we'll have to calculate the number of words in each element, and finally sum the result.
words
will give you a list of the words, so after doing map (words) xs
(no need to have parentheses around words btw), you'll get the following:
map words ["asd qwe", "-- Foo", "", "thello world "]
=>
[["asd","qwe"],["--","Foo"],,["hello","world"]]
The first thing you want to do is to get the length of each sublist, which you could fit into your map
map (x -> length (words x)) xs
Now, the result is:
[2,2,0,2]
And by running sum on the result, you get 6. So the end result is
countWords :: [String]-> Int
countWords xs = sum $ map (x -> length (words x)) xs
With some syntax sugar, you could do the following, but I find that most beginners are confused by it:
countWords xs = sum $ map (length . words) xs
or even better
countWords = sum . map (length . words)
edited Nov 25 '18 at 17:01
dfeuer
33.1k349131
33.1k349131
answered Nov 25 '18 at 9:13
ShamPooShamShamPooSham
520416
520416
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%2f53465667%2fcombining-map-and-length-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
1
Can you give some example data, what you expect to get, and why it doesn't work as expected?
– ShamPooSham
Nov 25 '18 at 8:14
1
For the second one, you could simply call sum on the result
– ShamPooSham
Nov 25 '18 at 8:28
1
1. Find out what
map words ["asd qwe"]
returns. Is this what you want? Is the type of the result OK? 2. How is the desired result ofcountChars
related to the actual result? What is missing to transform one to the other?– n.m.
Nov 25 '18 at 8:29
Words ["asd qwe"] should give back 2. My problem is that I can't iterate trough the list to count the words in each element and at the end sum these numbers. CountChars already knew this but I want give back one integer which is the sum of the resulting numbers.
– Akos Fajszi
Nov 25 '18 at 8:34
1
1.
Words ["asd qwe"] should give back 2
I have asked you to figure out what it does, not what it should do. (And if it should return 2, whatlength 2
is supposed to do then?) 2. Of course you can find a sum of numbers in a list. You just cannot usemap
for that.– n.m.
Nov 25 '18 at 9:22