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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:29:17+00:00 2026-05-26T11:29:17+00:00

My assignment problem is to find all non-overlapping occurences of a substring in a

  • 0

My assignment problem is to find all non-overlapping occurences of a substring in a string and return the a list of starting indices of occurences found.

My code so far is:

-- return True when `e` (haystack) starts with `s` (needle)
_startsWith :: String -> String -> Bool
-- Anything starts with the empty string
_startsWith _ "" = True
-- An empty string cannot start with a nonempty string 
_startsWith "" (h:t) = False 
_startsWith (eh:et) (sh:st) = (eh==sh) && (_startsWith et st)

-- look for all occurences of `s` (needle) in `e` (haystack), accumulating 
-- start indices of occurences in `is`
_findAll :: [Integer] -> Integer -> String -> String -> [Integer]
_findAll is i e s
    | null e = is -- end of haystack reached, return indices found so far
    | _startsWith e s = i:(rest $ length s) -- needle found, skip its length
    | otherwise = rest (1::Int) -- needle not found, advance by one character
          -- search the rest of the string after advancing by `skip` characters
    where rest skip = _findAll is (i+skip) (drop skip e) s

GHCi and Hugs give me different type errors. The GHC one is:

Couldn't match expected type `Int' with actual type `Integer'
In the first argument of `drop', namely `skip'
In the third argument of `_findAll', namely `(drop skip e)'
In the expression: _findAll is (i + skip) (drop skip e) s

The Hugs one is:

Type error in application
*** Expression     : i + skip
*** Term           : i
*** Type           : Integer
*** Does not match : Int

From this I’m guessing the problem is that skip is used in a context that expects an Int, and a context that expects an Integral, and this can’t be resolved because there is no typeclass that would allow this.

Assuming homework restrictions – the signature of _findAll may not be changed, preferrably importing no libraries except Prelude, definitely not importing any libraries not available on a basic apt-get installation of Hugs – how can I resolve this type error? I tried randomly sprinkling fromInteger and fromIntegral around to little success, and I’m not familiar enough with ugliness that is Haskell numeric typeclasses to figure this out.

  • 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-26T11:29:18+00:00Added an answer on May 26, 2026 at 11:29 am

    One simple solution is to use the generic versions genericDrop and genericLength found in Data.List. These will work with any Integral type, not just Int.

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

Sidebar

Related Questions

Okay I have an assignment to find the Kth smallest element in the list
I'm having a problem with this home work assignment. I can handle the syntax,
I have assignment to work on producer and consumer problem by use thread and
Does an assignment of a real to a variable starting with I convert to
This is a homework assignment and my first experience using RegEx. I am starting
I'm working on assignment 2 problem 3 for saas class. I'm totally a rails
My goal is: first, find a specific key in an XML file. Second, return
Compact description I'm having a problem to find out what's wrong, for some obscure
This is a homework assignment, just for all that want to know. I'm writing
I have an assignment to create an algorithm to find duplicates in an array

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.