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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:40:35+00:00 2026-06-12T23:40:35+00:00

I am trying to learn some Haskell and I find it difficult. I am

  • 0

I am trying to learn some Haskell and I find it difficult. I am having some issues with my
current project. The idea is that I have to go through a String and substitute certain chars
with new substrings. For instance if I have a String “FLXF” and I want to replace every F
with a substring called “FLF” the result should be “FLFLXFLF”. Now I have been working on this
specific problem for hours. I have been reading up on types, different functions that might come in handy (map, fold, etc) and yet I have not been able to solve this problem.

The code below is some of the different tries I have had:

apply :: String -> String
apply []     = []
apply (x:xs) = if (x == 'F')
               then do show "Hello"
                       apply xs
               else (apply (xs))

This example here I was just trying to show hello every time I encountered a ‘F’, but all it shows is “”, so this clearly does not work. I am really not sure an if else statement is the way to go here. I was also thinking the function map might do the trick. Here the code I was thinking about could look something like this:

map (\x y -> if y == 'F' then "FLD" else y) "FLF"

but that gives me a type error. So as you can see I am lost. Excuse me my poor knowledge to Haskell, but I am still new to it. I really hope some of you can help me out here or give me a push in the right direction. Feel free to ask questions if I have been unclear about something.

Thank you in advance!

John

  • 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-12T23:40:37+00:00Added an answer on June 12, 2026 at 11:40 pm
    map (\x y -> if y == 'F' then "FLD" else y) "FLF"
    

    This is nearly right.

    First… why does the function take two arguments?

    map (\y -> if y == 'F' then "FLD" else y) "FLF"
    

    The remaining type error is because the then branch gives a String, but the else branch gives a Char (the two branches must each give a value of the same type). So we’ll make the else branch give a String instead (recall that String is a synonym for [Char]):

    map (\y -> if y == 'F' then "FLD" else [y]) "FLF"
    

    Now the problem is that this gives you a [String] value instead of a String. So we’ll concatenate all those strings together:

    concat (map (\y -> if y == 'F' then "FLD" else [y]) "FLF")
    

    This combination of concat and map is common enough that there’s a standard function that combines them.

    concatMap (\y -> if y == 'F' then "FLD" else [y]) "FLF"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn some programming with jQuery. I have a div that has
I'm trying to learn some ant for a Struts 1.x project that I was
I am just trying to learn some Lisp, so I am going through project
I am trying to learn some DirectX API and, for now, I have just
I am trying to learn some VB.NET for my coo-op that starts next week,
I'm trying to do a little PDO CRUD to learn some PDO. I have
I've been trying to learn some programming on my own by working through the
I am just trying to learn some Perl, so I am going through one
I'm trying to learn some basic regular expression and having a hard time getting
Trying to learn some R after doing mostly Haskell for rather a long time

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.