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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:19:02+00:00 2026-05-20T09:19:02+00:00

Struggling to learn Haskell, how does one take the head of a string and

  • 0

Struggling to learn Haskell, how does one take the head of a string and compare it with the next character untill it finds a character thats note true?

In pseudo code I’m trying to:

while x == ‘next char in string’ put in new list to be returned

  • 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-20T09:19:03+00:00Added an answer on May 20, 2026 at 9:19 am

    The general approach would be to create a function that recursively evaluates the head of the string until it finds the false value or reaches the end.

    To do that, you would need to

    • understand recursion (prerequisite: understand recursion) and how to write recursive functions in Haskell
    • know how to use the head function
    • quite possibly know how to use list comprehension in Haskell

    I have notes on Haskell that you may find useful, but you may well find Yet Another Haskell Tutorial more comprehensive (Sections 3.3 Lists; 3.5 Functions; and 7.8 More Lists would probably be good places to start in order to address the bullet points I mention)

    EDIT0:
    An example using guards to test the head element and continue only if it the same as the second element:

    someFun :: String -> String 
    someFun[] = [] 
    someFun [x:y:xs]
        | x == y = someFun(y:xs)
        | otherwise = []
    

    EDIT1:

    I sort of want to say x = (newlist) and then rather than otherwise = [] have otherwise = [newlist] if that makes any sense?
    It makes sense in an imperative programming paradigm (e.g. C or Java), less so for functional approaches

    Here is a concrete example to, hopefully, highlight the different between the if,then, else concept the quote suggests and what is happening in the SomeFun function:

    When we call SomeFun [a,a,b,b] we match this to SomeFun [x:y:xs] and since x is ‘a’, and y is ‘a’, and x==y, then SomeFun [a,a,b,b] = SomeFun [a,b,b], which again matches SomeFun [x:y:xs] but condition x==y is false, so we use the otherwise guard, and so we get SomeFun [a,a,b,b] = SomeFun [a,b,b] = []. Hence, the result of SomeFun [a,a,b,b] is [].

    So where did the data go? .Well, I’ll hold my hands up and admit a bug in the code, which is now a feature I’m using to explain how Haskell functions work.

    I find it helpful to think more in terms of constructing mathematical expressions rather than programming operations. So, the expression on the right of the = is your result, and not an assignment in the imperative (e.g. Java or C sense).

    I hope the concrete example has shown that Haskell evaluates expressions using substitution, so if you don’t want something in your result, then don’t include it in that expression. Conversely, if you do want something in the result, then put it in the expression.

    Since your psuedo code is

    while x == ‘next char in string’ put in new list to be returned

    I’ll modify the SomeFun function to do the opposite and let you figure out how it needs to be modified to work as you desire.

    someFun2 :: String -> String 
    someFun2[] = [] 
    someFun2 [x:y:xs]
        | x == y = []
        | otherwise = x : someFun(y:xs)
    

    Example Output:

    • SomeFun2 [a,a,b,b] = []
    • SomeFun2 [a,b,b,a,b] = [a]
    • SomeFun2 [a,b,a,b,b,a,b] = [a,b,a]
    • SomeFun2 [a,b,a,b] = [a,b,a,b]

    (I’d like to add at this point, that these various code snippets aren’t tested as I don’t have a compiler to hand, so please point out any errors so I can fix them, thanks)

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

Sidebar

Related Questions

Because I'm currently struggling to learn IBM Rational ClearCase, I'd like to hear your
Greets! I'm a noob struggling to learn html and javascript - getting there slowly.
I've been trying to learn cakephp recently but I'm struggling to find any tutorials
Really struggling to figure this out. Just trying to take in data from php
I am just starting to learn C and (of course) struggling with pointers :)
I am trying to learn Spring and Hibernate and i am really struggling to
I am trying to learn lex and yacc. I am struggling to understand how
I'm trying to learn NASM assembly, but I seem to be struggling with what
I am trying to learn JavaScript, but I'm struggling with a very simple example.
I'm trying to learn EF in visual basic. I'm struggling with finding any easy

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.