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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:28:28+00:00 2026-06-12T12:28:28+00:00

How would you go about defining a function that takes two strings, say string

  • 0

How would you go about defining a function that takes two strings, say string x and string y and return the number of elements at the end of the first string (string x) which overlap with the beginning of the second string (second y).

I’m thinking that it would be a good idea to use isPrefixOf from the prelude to do this since it checks if a string is in another string and returns True if it does or False otherwise. But I’m a little confused at how to return the count of how many elements overlap. I wrote some pseudocode based off how I think you would approach the problem.

countElements :: Eq a => (Str a, Str a) -> Int
countElements (x,y) = 
     if x `isPrefixOf` y == True
         then return the count of how many elements overlap
     otherwise 0

A sample output would be:

countElements (board, directors) = 1
countElements (bend, ending) = 3

Any help here? I’m not very good at writing Haskell code.

  • 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-12T12:28:29+00:00Added an answer on June 12, 2026 at 12:28 pm

    You have exactly the right idea, but your pseudocode misses the fact that you’ll have to iterate on all of the possible tails of the first string passed to the function.

    countElements :: String -> String -> Int
    countElements s t = length $ head $ filter (`isPrefixOf` t) (tails s)
    
    > countElements "board" "directors"
    1
    > countElements "bend" "endings"
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one go about defining something like this in an xsd? <start> <request
I would like to know how I would go about altering the HTML that
I am wondering how I would go about performing a certain function if a
I made the following function in SQL Server 2008 earlier this week that takes
Let's say I have a function def odd_or_even n if n%2 == 0 return
This problem is in defining and declaring a function template in a namespace that
I have a question about the images displaying using a function getImage_w($image,$dst_w), which takes
I recently read about the fact that there is a possibility of defining getters/setters
I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>
Any ideas how I would go about writing a javascript method to insert an

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.