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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:00:25+00:00 2026-06-13T14:00:25+00:00

I am new to Haskell. I am trying to write a program which given

  • 0

I am new to Haskell.
I am trying to write a program which given a list as an input replicates each element of list k times, where k = position of element in list.

e.g. replic[5,6,7] gives [[5],[6,6],[7,7,7]].

Another condition is solution has to use map function.

Till now code I have written is :

replic [] = [] 
replic (x:xs) =  map (replicate 2 ) [x] ++ replic xs 

This replicates every element twice as replicate has input parameter 2.

What I need is replicate function should be given input as 1 ,2 ,3 in consecutive calls. So I need a counter. How can I use the counter there or do anything else that will give me position of element?

  • 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-13T14:00:27+00:00Added an answer on June 13, 2026 at 2:00 pm

    Expanding on Satvik, the notation

    [1..]
    

    gives you an infinite list of numbers counting up.

    The function zip associates allows you to merge two lists into a list of tuples

    zip :: [a] -> [b] -> [(a,b)]
    

    for example

    > zip [1..] [5,6,7] 
    [(1,5),(2,6),(3,7)]
    

    this code associates each value in the list with its position in the list

    now

    replicate :: Int -> a -> [a]
    

    repeats a value an arbitrary number of times. Given these two components, we can design a simple function

    replic xs = map (\(a,b) -> replicate a b) (zip [1..] xs)
    

    which I would write pointfree as

    replic :: [a] -> [[a]]
    replic = map (uncurry replicate) . zip [1..]
    

    this does exactly what you want

    > replic [5,6,7]
    [[5],[6,6],[7,7,7]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Haskell and have a stumbling block. I'm trying to filter a list
I'm new to haskell guys. I'm trying to write a gcd executable file. ghc
I'm trying to write a function searching for a given element in a rose
I am new to Haskell and trying to fiddle with some test cases I
Hello I am new at Haskell and i'm having problems trying to get this
I'm new to Haskell and Snap and I want to write a simple bug-tracking
I am new to Haskell and I am trying to understand why one needs
I am new to Haskell, and programming in general. I am trying to define
Hi i'm new to haskell and i'm trying to implement the following and I
I'm new to haskell and I'm trying to do my exercise for my class,

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.