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 ?










share|improve this question
























  • 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















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 ?










share|improve this question
























  • 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













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 ?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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 otherwise


  • readAsNumber takes two returns True if both are a digit or a dot


  • finally you group your string by readAsNumber






share|improve this answer























  • 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










  • @astrocurieux pred function is defined in the where clause. It is not built-in function.
    – Luis Morillo
    Nov 12 at 7:41











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',
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
});


}
});














draft saved

draft discarded


















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

























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 otherwise


  • readAsNumber takes two returns True if both are a digit or a dot


  • finally you group your string by readAsNumber






share|improve this answer























  • 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










  • @astrocurieux pred function is defined in the where clause. It is not built-in function.
    – Luis Morillo
    Nov 12 at 7:41















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 otherwise


  • readAsNumber takes two returns True if both are a digit or a dot


  • finally you group your string by readAsNumber






share|improve this answer























  • 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










  • @astrocurieux pred function is defined in the where clause. It is not built-in function.
    – Luis Morillo
    Nov 12 at 7:41













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 otherwise


  • readAsNumber takes two returns True if both are a digit or a dot


  • finally you group your string by readAsNumber






share|improve this answer














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 otherwise


  • readAsNumber takes two returns True if both are a digit or a dot


  • finally you group your string by readAsNumber







share|improve this answer














share|improve this answer



share|improve this answer








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










  • @astrocurieux pred 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










  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini