Number each list element and format text in haskell
I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.
numberL :: [String] -> [String]
numberL =
numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))
Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?
haskell numbers element
add a comment |
I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.
numberL :: [String] -> [String]
numberL =
numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))
Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?
haskell numbers element
add a comment |
I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.
numberL :: [String] -> [String]
numberL =
numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))
Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?
haskell numbers element
I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.
numberL :: [String] -> [String]
numberL =
numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))
Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?
haskell numbers element
haskell numbers element
asked Nov 23 '18 at 16:02
Akos FajsziAkos Fajszi
445
445
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are multiple built-in Haskell functions in Prelude
that are good to learn and use them. zip
and zipWith
are two of them, when you think about something to be done using two different lists into one result list:
[1..]
will generate the list of indices for you, it's an infinite list
appendIndex :: String -> Int -> String
appendIndex s i = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = zipWith appendIndex l [1..]
if you wanted to use zip
, which is more basic but a little more verbose:
appendIndex :: (String,Int) -> String
appendIndex (s,i) = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = fmap appendIndex $ zip l [1..]
-- if you dont know about Functors yet, `fmap` is the generic way of doing `map`
add a comment |
To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:
numberL (x:xs) = ... ++ numberL xs
So you calculate numberL xs
and then put something in front of it. If numberL xs
were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:...
. So you could never build numberL (x:xs)
from numberL xs
just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs
.
The problem therefore is that it's not very useful to know numberL xs
in order to calculate numberL (x:xs)
, due to the fact numberL
always starts numbering from 1.
So lift that restriction. Build a function that numbers starting at n
,
numberLFrom :: Int -> [String] -> [String]
numberLFrom n = ...
numberLFrom n (x:xs) = ...
Now the question you have to ask yourself is, in order to number (x:xs)
starting at n
you need to number xs
starting at which number? And then how do you introduced the numbered x
to that result?
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%2f53449776%2fnumber-each-list-element-and-format-text-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are multiple built-in Haskell functions in Prelude
that are good to learn and use them. zip
and zipWith
are two of them, when you think about something to be done using two different lists into one result list:
[1..]
will generate the list of indices for you, it's an infinite list
appendIndex :: String -> Int -> String
appendIndex s i = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = zipWith appendIndex l [1..]
if you wanted to use zip
, which is more basic but a little more verbose:
appendIndex :: (String,Int) -> String
appendIndex (s,i) = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = fmap appendIndex $ zip l [1..]
-- if you dont know about Functors yet, `fmap` is the generic way of doing `map`
add a comment |
There are multiple built-in Haskell functions in Prelude
that are good to learn and use them. zip
and zipWith
are two of them, when you think about something to be done using two different lists into one result list:
[1..]
will generate the list of indices for you, it's an infinite list
appendIndex :: String -> Int -> String
appendIndex s i = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = zipWith appendIndex l [1..]
if you wanted to use zip
, which is more basic but a little more verbose:
appendIndex :: (String,Int) -> String
appendIndex (s,i) = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = fmap appendIndex $ zip l [1..]
-- if you dont know about Functors yet, `fmap` is the generic way of doing `map`
add a comment |
There are multiple built-in Haskell functions in Prelude
that are good to learn and use them. zip
and zipWith
are two of them, when you think about something to be done using two different lists into one result list:
[1..]
will generate the list of indices for you, it's an infinite list
appendIndex :: String -> Int -> String
appendIndex s i = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = zipWith appendIndex l [1..]
if you wanted to use zip
, which is more basic but a little more verbose:
appendIndex :: (String,Int) -> String
appendIndex (s,i) = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = fmap appendIndex $ zip l [1..]
-- if you dont know about Functors yet, `fmap` is the generic way of doing `map`
There are multiple built-in Haskell functions in Prelude
that are good to learn and use them. zip
and zipWith
are two of them, when you think about something to be done using two different lists into one result list:
[1..]
will generate the list of indices for you, it's an infinite list
appendIndex :: String -> Int -> String
appendIndex s i = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = zipWith appendIndex l [1..]
if you wanted to use zip
, which is more basic but a little more verbose:
appendIndex :: (String,Int) -> String
appendIndex (s,i) = (show i) ++ " :" ++ s
indexThem :: [String] -> [String]
indexThem l = fmap appendIndex $ zip l [1..]
-- if you dont know about Functors yet, `fmap` is the generic way of doing `map`
edited Nov 24 '18 at 5:48
answered Nov 23 '18 at 16:30
Stephane RollandStephane Rolland
19.1k2698146
19.1k2698146
add a comment |
add a comment |
To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:
numberL (x:xs) = ... ++ numberL xs
So you calculate numberL xs
and then put something in front of it. If numberL xs
were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:...
. So you could never build numberL (x:xs)
from numberL xs
just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs
.
The problem therefore is that it's not very useful to know numberL xs
in order to calculate numberL (x:xs)
, due to the fact numberL
always starts numbering from 1.
So lift that restriction. Build a function that numbers starting at n
,
numberLFrom :: Int -> [String] -> [String]
numberLFrom n = ...
numberLFrom n (x:xs) = ...
Now the question you have to ask yourself is, in order to number (x:xs)
starting at n
you need to number xs
starting at which number? And then how do you introduced the numbered x
to that result?
add a comment |
To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:
numberL (x:xs) = ... ++ numberL xs
So you calculate numberL xs
and then put something in front of it. If numberL xs
were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:...
. So you could never build numberL (x:xs)
from numberL xs
just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs
.
The problem therefore is that it's not very useful to know numberL xs
in order to calculate numberL (x:xs)
, due to the fact numberL
always starts numbering from 1.
So lift that restriction. Build a function that numbers starting at n
,
numberLFrom :: Int -> [String] -> [String]
numberLFrom n = ...
numberLFrom n (x:xs) = ...
Now the question you have to ask yourself is, in order to number (x:xs)
starting at n
you need to number xs
starting at which number? And then how do you introduced the numbered x
to that result?
add a comment |
To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:
numberL (x:xs) = ... ++ numberL xs
So you calculate numberL xs
and then put something in front of it. If numberL xs
were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:...
. So you could never build numberL (x:xs)
from numberL xs
just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs
.
The problem therefore is that it's not very useful to know numberL xs
in order to calculate numberL (x:xs)
, due to the fact numberL
always starts numbering from 1.
So lift that restriction. Build a function that numbers starting at n
,
numberLFrom :: Int -> [String] -> [String]
numberLFrom n = ...
numberLFrom n (x:xs) = ...
Now the question you have to ask yourself is, in order to number (x:xs)
starting at n
you need to number xs
starting at which number? And then how do you introduced the numbered x
to that result?
To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:
numberL (x:xs) = ... ++ numberL xs
So you calculate numberL xs
and then put something in front of it. If numberL xs
were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:...
. So you could never build numberL (x:xs)
from numberL xs
just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs
.
The problem therefore is that it's not very useful to know numberL xs
in order to calculate numberL (x:xs)
, due to the fact numberL
always starts numbering from 1.
So lift that restriction. Build a function that numbers starting at n
,
numberLFrom :: Int -> [String] -> [String]
numberLFrom n = ...
numberLFrom n (x:xs) = ...
Now the question you have to ask yourself is, in order to number (x:xs)
starting at n
you need to number xs
starting at which number? And then how do you introduced the numbered x
to that result?
edited Nov 23 '18 at 16:45
answered Nov 23 '18 at 16:36
Jorge AdrianoJorge Adriano
2,210918
2,210918
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%2f53449776%2fnumber-each-list-element-and-format-text-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