Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:04:56+00:00 2026-06-18T11:04:56+00:00

I have a string something like this 3,4\r\n , and I want to convert

  • 0

I have a string something like this "3,4\r\n", and I want to convert them into a tuple i.e (3,4).

How can we achieve this in SML ?

The reason why I’m getting a string value is because I’m reading a file which returns strings like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:04:58+00:00Added an answer on June 18, 2026 at 11:04 am

    You need a simple parser to achieve that. An appropriate function to parse integers is already available in the library as Int.scan (along with friends for other types), but you have to write the rest yourself. For example:

    (* scanLine : (char, 's) StringCvt.reader -> (int * int, 's) StringCvt.reader *)
    fun scanLine getc stream =
        case Int.scan StringCvt.DEC getc stream
          of NONE => NONE
           | SOME (x1, stream') =>
        case getc stream'
          of NONE => NONE
           | SOME (c1, stream'') =>
        if c1 <> #"," then NONE else
        case Int.scan StringCvt.DEC getc stream''
          of NONE => NONE
           | SOME (x2, stream''') => 
        case getc stream'''
          of NONE => NONE
           | SOME (c2, stream'''') =>
        if c2 <> #"\n" then NONE else
        SOME ((x1, x2), stream'''')
    

    And then, to parse all lines:

    (* scanList : ((char, 's) StringCvt.reader -> ('a, 's) StringCvt.reader) -> (char, 's)  StringCvt.reader -> ('a list, 's) StringCvt.reader *)
    fun scanList scanElem getc stream =
        case scanElem getc stream
          of NONE => SOME ([], stream)
           | SOME (x, stream') =>
        case scanList scanElem getc stream'
          of NONE => NONE
           | SOME (xs, stream'') => SOME (x::xs, stream'')
    

    To use it, for example:

    val test = "4,5\n2,3\n"
    val result = StringCvt.scanString (scanList scanLine) test
    (* val result : (int * int) list = [(4, 5), (2, 3)] *)
    

    As you can see, the code is a bit repetitive. To get rid of all the matching of option types you could write a few basic parser combinators:

    (* scanCharExpect : char -> (char, 's) StringCvt.reader -> (char, 's) StringCvt.reader *)
    fun scanCharExpect expect getc stream =
        case getc stream
          of NONE => NONE
           | SOME (c, stream') =>
             if c = expect then SOME (c, stream') else NONE
    
    (* scanSeq : ((char, 's) StringCvt.reader -> ('a, 's) StringCvt.reader) * ((char, 's) StringCvt.reader -> ('b, 's) StringCvt.reader) -> (char, 's) StringCvt.reader -> ('a * 'b, 's) StringCvt.reader *)
    fun scanSeq (scan1, scan2) getc stream =
        case scan1 getc stream
          of NONE => NONE
           | SOME (x1, stream') =>
        case scan2 getc stream'
          of NONE => NONE
           | SOME (x2, stream'') => SOME ((x1, x2), stream'')
    
    fun scanSeqL (scan1, scan2) getc stream =
        Option.map (fn ((x, _), stream) => (x, stream)) (scanSeq (scan1, scan2) getc stream)
    fun scanSeqR (scan1, scan2) getc stream =
        Option.map (fn ((_, x), stream) => (x, stream)) (scanSeq (scan1, scan2) getc stream)
    
    (* scanLine : (char, 's) StringCvt.reader -> (int * int, 's) StringCvt.reader *)
    fun scanLine getc stream =
        scanSeq (
            scanSeqL (Int.scan StringCvt.DEC, scanCharExpect #","),
            scanSeqL (Int.scan StringCvt.DEC, scanCharExpect #"\n")
        ) getc stream
    

    There are a lot more cool abstractions you can build along these lines, especially when defining your own infix operators. But I’ll leave it at that.

    You might also want to handle white space between tokens. The StringCvt.skipWS reader is readily available in the lib for that, just insert it in the right places.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have something like a List[Option[A]] and I want to convert this into
I have a string containing something like this Hello bla bla bla bla ok,
I have something like this hard-coded: private string[,] m_RolesForUser = new string[,] { {John,President,Chair},
I have something like this: Map<String, String> myMap = ...; for(String key : myMap.keySet())
I have written something like this pretty easily in C# ( string GetUrl(new {
I have two tables which looks something like this Table Queue int ID; string
I have a List of String objects that are pipe delimited. something like this:
I have a string like 1.5% and want to convert it to double value.
I have something like this in my Spring Application: public class Book{ public Book(){
I have string: text and something and something like f213 @@ -1,9 +1,11 @@

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.