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

  • Home
  • SEARCH
  • 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 6889761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:11:48+00:00 2026-05-27T06:11:48+00:00

I needed a String tokenizer in Haskell but there is apparently nothing already defined

  • 0

I needed a String tokenizer in Haskell but there is apparently nothing already defined in the Prelude or other modules. There is splitOn in Data.Text, but that’s a pain to use because you need to wrap the String to Text.

The tokenizer is not too hard to do so I wrote one (it doesn’t handle multiple adjacent delimiters, but it worked well for what I needed it). I feel something like this should be already in the modules somewhere..

This is my version

tokenizer :: Char -> String -> [String]
tokenizer delim str = tokHelper delim str []

tokHelper :: Char -> String -> [String] -> [String]
tokHelper d s acc 
    | null pos  = reverse (pre:acc)
    | otherwise = tokenizer d (tail pos) (pre:acc)
        where (pre, pos) = span (/=d) s

I searched the internet for more solutions and found some discussions, like this blog post.

The last comment (by Mahee on June 10, 2011) is particularly interesting. Why not make a version of the words function more generic to handle this? I tried searching for such a function but found none..

Is there a simpler way to this or is ‘tokenizing’ a string not a very recurring problem? 🙂

  • 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-05-27T06:11:49+00:00Added an answer on May 27, 2026 at 6:11 am

    The split library is what you need. Install with cabal install split, then you have access to a lot of split/tokenizer style functions.

    Some examples from the library:

     > import Data.List.Split
     > splitOn "x" "axbxc"
     ["a","b","c"]
     > splitOn "x" "axbxcx"
     ["a","b","c",""]
     > endBy ";" "foo;bar;baz;"
     ["foo","bar","baz"]
     > splitWhen (<0) [1,3,-4,5,7,-9,0,2]
     [[1,3],[5,7],[0,2]]
     > splitOneOf ";.," "foo,bar;baz.glurk"
     ["foo","bar","baz","glurk"]
     > splitEvery 3 ['a'..'z']
     ["abc","def","ghi","jkl","mno","pqr","stu","vwx","yz"]
    

    The wordsBy function from the same library is a generic version of words like you wanted:

    wordsBy (=='x') "dogxxxcatxbirdxx" == ["dog","cat","bird"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I needed to re-install my computer but I didn't think about exporting the data
i needed to match a string like the following: XXX But both 1. and
I needed some simple string encryption, so I wrote the following code (with a
What is needed to make String.IsNullOrEmpty() count whitespace strings as empty? Eg. I want
I needed to find all the files that contained a specific string pattern. The
I needed a function that simply checks if a string can be converted to
What's the difference between std::string and std::basic_string ? And why are both needed?
Needed Finfo but deleted msi package, so uninstalled php 5.3.0, downloaded 5.3.2 and installed.
I would like to use the WebClient (or there is another better option?) but
Let's say I needed to make a series of String[] objects. I know that

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.