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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:19:23+00:00 2026-05-27T05:19:23+00:00

I am in need of a function, which can do the following: prefixes ::

  • 0

I am in need of a function, which can do the following:

prefixes :: String -> [String] -> [(Int,String)]
prefixes "apples" ["ap","appl","le"] == [(0, "ap"), (1, "appl")] :: [(Int, String)]

So far I have managed to make this:

prefixes xs (y:ys) = filter ((isPrefixOf xs).snd) a where
a=(zip [0..] (y:ys))

But the result of this is an empty list, and I can not figure out a way to make it work.
(Yes, this was a homework, which I failed to complete on time, but I am still curious about the way to do it properly)

  • 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-27T05:19:24+00:00Added an answer on May 27, 2026 at 5:19 am

    The naming of isPrefixOf can be slightly confusing at times, as it’s intended to be used infix with backticks, e.g.

    > "ap" `isPrefixOf` "apples"
    True
    

    However, this means that when we write it without the backticks, the argument order is

    isPrefixOf "ap" "apples"
    

    so the partial application isPrefixOf xs is the function that checks if xs is a prefix of its argument, not the other way round. This is why you get an empty list, as you’re checking if "apples" is a prefix of any of the shorter strings, which obviously returns False for all of them.

    There are three simple ways of fixing this. One is to use flip, which swaps the order of the arguments for a two-parameter function:

    flip isPrefixOf xs
    

    The second is to use backticks in an operator section:

    (`isPrefixOf` xs)
    

    The third is to be explicit and use a lambda:

    \y -> y `isPrefixOf` xs 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
Is there any function like int() which can convert a string to float value?
Hi have the following function which works fine: jQuery('.btnRemoveItem').click(function(obj){ jQuery(this).closest('li').fadeOut(400, function() { $(this).remove(); });
i have one jquery function which i need to work in following way 1.
I am writing a function in which I need to read a string contains
So deep inside a library I need a function which allocates a string, writes
I have the following function which works great, i used JSONP to overcome cross-domain,
I have the following function which is for ajaxing in a page and the
I have the following MyType::Is_Inst () function which is throwing an invalid memory access
So: I have the following function, adapted from a formula found online, which takes

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.