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 3441938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:36:46+00:00 2026-05-18T08:36:46+00:00

Am still new to Haskell, so apologize if there is an obvious answer to

  • 0

Am still new to Haskell, so apologize if there is an obvious answer to this…

I would like to make a function that splits up the all following lists of strings i.e. [String]:

["int x = 1", "y := x + 123"]
["int   x=   1", "y:=   x+123"] 
["int x=1", "y:=x+123"] 

All into the same string of strings i.e. [[String]]:

[["int", "x", "=", "1"], ["y", ":=", "x", "+", "123"]]

You can use map words.lines for the first [String].

But I do not know any really neat ways to also take into account the others – where you would be using the various sub-strings "=", ":=", "+" etc. to break up the main string.

Thank you for taking the time to enlighten me on Haskell 🙂

  • 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-18T08:36:47+00:00Added an answer on May 18, 2026 at 8:36 am

    The Prelude comes with a little-known handy function called lex, which is a lexer for Haskell expressions. These match the form you need.

    lex :: String -> [(String,String)]
    

    What a weird type though! The list is there for interfacing with a standard type of parser, but I’m pretty sure lex always returns either 1 or 0 elements (0 indicating a parse failure). The tuple is (token-lexed, rest-of-input), so lex only pulls off one token. So a simple way to lex a whole string would be:

    lexStr :: String -> [String]
    lexStr "" = []
    lexStr s = 
        case lex s of
            [(tok,rest)] -> tok : lexStr rest
            []           -> error "Failed lex"
    

    To appease the pedants, this code is in terrible form. An explicit call to error instead of returning a reasonable error using Maybe, assuming lex only returns 1 or 0 elements, etc. The code that does this reliably is about the same length, but is significantly more abstract, so I spared your beginner eyes.

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

Sidebar

Related Questions

I am still new to sharepoint and would like to know if it is
Still new at this ... Sorry! I have a template for multiple pages that
Still quite new to Haskell.. I want to read the contents of a file,
Still new to Objective C, and I'm having some trouble that I just can't
Sorry if the question is very elementary, I am still very new to Haskell.
I'm still new top rspec, so please excuse if this is an easy one
I am still new to this Memory Management stuff (Garbage Collector took care of
I'm still new to the Ada programming world so forgive me if this question
Still new to python and django, though learning ;-) I have a view that
I am still new to objective-c and am trying to figure out what this

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.