return a list from a complex string
up vote
-2
down vote
favorite
I start with haskell.
I was wondering if I can convert a string to a list of different element's type like that : "1*30%4" -> ['1', '*', '30', '%', '4']
without Parsec
I have already found answers but none really help me...
like map (:) "1*30%4"
with GHCI
or with the help of intersperse
.
but I do not know how to keep the right formats, I can not, for example, have numbers or float/double in my list because everything is cut one by one : "1*30%4" -> ['1', '*', '3', '0', '%', '4']
or "1*30.4%4" -> ['1', '*', '3', '0', '.', '4', '%', '4']
Someone can help me ?
parsing haskell operators
add a comment |
up vote
-2
down vote
favorite
I start with haskell.
I was wondering if I can convert a string to a list of different element's type like that : "1*30%4" -> ['1', '*', '30', '%', '4']
without Parsec
I have already found answers but none really help me...
like map (:) "1*30%4"
with GHCI
or with the help of intersperse
.
but I do not know how to keep the right formats, I can not, for example, have numbers or float/double in my list because everything is cut one by one : "1*30%4" -> ['1', '*', '3', '0', '%', '4']
or "1*30.4%4" -> ['1', '*', '3', '0', '.', '4', '%', '4']
Someone can help me ?
parsing haskell operators
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
3
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
3
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I start with haskell.
I was wondering if I can convert a string to a list of different element's type like that : "1*30%4" -> ['1', '*', '30', '%', '4']
without Parsec
I have already found answers but none really help me...
like map (:) "1*30%4"
with GHCI
or with the help of intersperse
.
but I do not know how to keep the right formats, I can not, for example, have numbers or float/double in my list because everything is cut one by one : "1*30%4" -> ['1', '*', '3', '0', '%', '4']
or "1*30.4%4" -> ['1', '*', '3', '0', '.', '4', '%', '4']
Someone can help me ?
parsing haskell operators
I start with haskell.
I was wondering if I can convert a string to a list of different element's type like that : "1*30%4" -> ['1', '*', '30', '%', '4']
without Parsec
I have already found answers but none really help me...
like map (:) "1*30%4"
with GHCI
or with the help of intersperse
.
but I do not know how to keep the right formats, I can not, for example, have numbers or float/double in my list because everything is cut one by one : "1*30%4" -> ['1', '*', '3', '0', '%', '4']
or "1*30.4%4" -> ['1', '*', '3', '0', '.', '4', '%', '4']
Someone can help me ?
parsing haskell operators
parsing haskell operators
edited Nov 8 at 11:40
asked Nov 8 at 11:29
astrocurieux
115
115
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
3
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
3
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19
add a comment |
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
3
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
3
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
3
3
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
3
3
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
As some users have pointed out, your returning type is [String]
instead of [Char]
. You can easily achieve this by the following:
import Data.Char
import Data.List
expresionToList :: String -> [String]
expresionToList = groupBy readAsNumber
where readAsNumber c d = pred c && pred d
pred x = isDigit x || x == '.'
pred
function returns True when its input is a digit or a dot, False otherwisereadAsNumber
takes two returns True if both are a digit or a dot- finally you group your string by
readAsNumber
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieuxpred
function is defined in the where clause. It is not built-in function.
– Luis Morillo
Nov 12 at 7:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
As some users have pointed out, your returning type is [String]
instead of [Char]
. You can easily achieve this by the following:
import Data.Char
import Data.List
expresionToList :: String -> [String]
expresionToList = groupBy readAsNumber
where readAsNumber c d = pred c && pred d
pred x = isDigit x || x == '.'
pred
function returns True when its input is a digit or a dot, False otherwisereadAsNumber
takes two returns True if both are a digit or a dot- finally you group your string by
readAsNumber
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieuxpred
function is defined in the where clause. It is not built-in function.
– Luis Morillo
Nov 12 at 7:41
add a comment |
up vote
3
down vote
accepted
As some users have pointed out, your returning type is [String]
instead of [Char]
. You can easily achieve this by the following:
import Data.Char
import Data.List
expresionToList :: String -> [String]
expresionToList = groupBy readAsNumber
where readAsNumber c d = pred c && pred d
pred x = isDigit x || x == '.'
pred
function returns True when its input is a digit or a dot, False otherwisereadAsNumber
takes two returns True if both are a digit or a dot- finally you group your string by
readAsNumber
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieuxpred
function is defined in the where clause. It is not built-in function.
– Luis Morillo
Nov 12 at 7:41
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
As some users have pointed out, your returning type is [String]
instead of [Char]
. You can easily achieve this by the following:
import Data.Char
import Data.List
expresionToList :: String -> [String]
expresionToList = groupBy readAsNumber
where readAsNumber c d = pred c && pred d
pred x = isDigit x || x == '.'
pred
function returns True when its input is a digit or a dot, False otherwisereadAsNumber
takes two returns True if both are a digit or a dot- finally you group your string by
readAsNumber
As some users have pointed out, your returning type is [String]
instead of [Char]
. You can easily achieve this by the following:
import Data.Char
import Data.List
expresionToList :: String -> [String]
expresionToList = groupBy readAsNumber
where readAsNumber c d = pred c && pred d
pred x = isDigit x || x == '.'
pred
function returns True when its input is a digit or a dot, False otherwisereadAsNumber
takes two returns True if both are a digit or a dot- finally you group your string by
readAsNumber
edited Nov 8 at 13:02
answered Nov 8 at 12:47
Luis Morillo
70512
70512
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieuxpred
function is defined in the where clause. It is not built-in function.
– Luis Morillo
Nov 12 at 7:41
add a comment |
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieuxpred
function is defined in the where clause. It is not built-in function.
– Luis Morillo
Nov 12 at 7:41
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
first time I hear about 'pred'.
– astrocurieux
Nov 8 at 14:22
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
thank you for your help, with the expresionToList function my problem is solved.
– astrocurieux
Nov 8 at 14:29
@astrocurieux
pred
function is defined in the where clause. It is not built-in function.– Luis Morillo
Nov 12 at 7:41
@astrocurieux
pred
function is defined in the where clause. It is not built-in function.– Luis Morillo
Nov 12 at 7:41
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%2f53206866%2freturn-a-list-from-a-complex-string%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
Would you like to do it with a parsing library like Parsec or just do it with vanilla Haskell?
– cmdv
Nov 8 at 11:34
I actually forgot to mention that I do not want to use Parsec
– astrocurieux
Nov 8 at 11:39
3
List in Haskell cannot hold different type of element. like '30' is not a type of Char, so, it must be broken into '3' and '0' in order to store them in list, say ['1', ...'3', '0' ...]. The thing you need may be list of String, say ["1", ... "30", "%"..] right?
– assembly.jc
Nov 8 at 11:51
yes it's exactly that, with the 'Words' function my program works well, but I have to separate everything with spaces, it's very annoying ...
– astrocurieux
Nov 8 at 12:04
3
What's wrong with parser libraries, it's the right tool for the job?
– Li-yao Xia
Nov 8 at 12:19