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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:50:49+00:00 2026-05-22T19:50:49+00:00

I am trying to make a function that outputs char*m n times, as such

  • 0

I am trying to make a function that outputs char*m n times, as such as the expected output would be ["ccc","ccc"] for the input 2 3 c. Here is what i have so far:

rectangle :: Int -> Int -> Char -> [string]
rectangle n m c 
    | m > 0 = [concat ([[c]] ++ (rectangle n (m-1) c))] 
    | otherwise = []

I am able to carry out the first part, char*m, so it returns ["ccc"]. Thing is: I also would like to be able to repeat my string n times.

I have tried using replicate but it doesn’t seem to work, yet it works if doing it in the console: replicate 2 (rectangle 2 3 c).

  • 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-22T19:50:50+00:00Added an answer on May 22, 2026 at 7:50 pm

    As an addendum to Refactor’s answer, I think his approach is the correct one. He subdivides the problem until it can be solved trivially using built-in functions. If you want to roll your own solution for learning purposes, I suggest you keep this subdivision, and go from there, implementing your own replicate. Otherwise, you will end up with a single function which does too much.

    So the remaining problem is that of implementing replicate. My first idea would be to look at the source code for replicate. I found it via hoogle, which led me to hackage, which has links to the source code. Excerpted from the source:

    replicate               :: Int -> a -> [a]
    replicate n x           =  take n (repeat x)
    

    which is nice and concise, again using the built-in functions. If you want to completely roll your own replicate, you can do:

    myReplicate                 :: Int -> a -> [a]
    myReplicate n x | n <= 0    = []
                    | otherwise = x : replicate (n-1) x
    

    ———-EDIT—————-

    As a side note, I think your problem requires two rather orthogonal skills. The first is trying not to tackle the whole problem at once, but making some small progress instead. Then you can try to solve that smaller problem, before returning to the larger. In your case, it would likely involve recognizing that you definitely need a way of transforming the character into a series of characters of length n. Experience with functions such as map, filter, foldr and so on will help you here, since they each represent a very distinct transformation, which you might recognize.

    The second skill required for your solution – if you want to roll your own – is recognizing when a function can be expressed recursively. As you can see, your problem – and indeed many common problems – can be solved without explicit recursion, but it is a nice skill to have, when the need arises. Recursive solutions do not always come easily mind, so I think the best way to gain familiarity with them are to read and practice.

    For further study, I’m sure you have already been pointed to the excellent Learn You a Haskell and Real World Haskell, but just in case you haven’t, here they are.

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

Sidebar

Related Questions

I am trying to make a timestamp function that checks which displays a time
Hi there i'm trying to make a function in C++ that takes a number,
I'm trying to make a autoscrolling/carousel like function for an unordered list of images.
Trying to make a make generic select control that I can dynamically add elements
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into
I am trying to make a JTable that has column spans available. Specifically, I
I'm trying to make a little client-server script like many others that I've done
I want to make some C++ program and I'm using function popen here to
In drupal 6 I'm trying to execute a function on every page and output

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.