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

  • Home
  • SEARCH
  • 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 7973947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:14:36+00:00 2026-06-04T08:14:36+00:00

I’m teaching myself to program in Haskell, and I’m working on a find function.

  • 0

I’m teaching myself to program in Haskell, and I’m working on a find function. What it does is it takes two strings, such as "hello" and "he", and it counts how many times "he" appears in "hello".

The find function needs to keep track of several things, such as how many times the word has been found in the list, that do not need to be entered at the start of the function. So, i broke the function up into two smaller functions: one that the user originally enters the data into, which then directs the data to a second function that does the work.

Here’s my code:

search :: (Eq a) => [a] -> [a] -> Integer
search [] _ = 0
search _ [] = 0
search x y = search1 x y y 0

search1 :: (Eq a) => [a] -> [a] -> [a] -> Integer -> Integer
search1 _ _ [] n = n
search1 x [] z n = search1 x z z (n+1)
search1 [] _ _ n = n
search1 (x:xs) (y:ys) z n
  | x == y = search1 xs ys z n
  | otherwise = search1 xs (y:ys) z n

In it, instead of the user starting out with the function search1, which needs some data that would be redundant for the user to enter, I created the function search to “plug in” the data to search1 for the user.

My question is, is creating a function to “plug in” redundant data a good practice in Haskell? Or should I be doing something different?

  • 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-04T08:14:38+00:00Added an answer on June 4, 2026 at 8:14 am

    Yes, it is good practice. However, it is often better to make the second function local to the first, if it is not useful on its own and only serves as the worker for the other one.

    If you create a worker local to the wrapper, you can refer to the wrapper’s arguments in the body of the worker without passing them as arguments to the worker, which often helps performance.

    For your example, the function with a local worker could look like

    search :: (Eq a) => [a] -> [a] -> Integer
    search [] _ = 0
    search _ [] = 0
    search hay needle = search1 hay needle 0
      where
        search1 x [] n = search1 x needle (n+1)
        search1 [] _ n = n
        search1 (x:xs) (y:ys) n
          | x == y = search1 xs ys n
          | otherwise = search1 xs (y:ys) n
    

    where I’ve eliminated one argument of the worker that was never changed in the recursive call. Also, making the worker local eliminates the need to check for an empty needle.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.