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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:08:22+00:00 2026-05-23T04:08:22+00:00

I know what the following function does I would just like an explanation of

  • 0

I know what the following function does I would just like an explanation of how it works and the calculations that take place:

sponge :: Int -> [a] -> [a]
sponge 0 xs = xs
sponge n [] = []
sponge n (x:xs) = sponge (n-1) xs

I just seem to have lost the plot with it all now 🙁

Any help to get me back on track would be much appreciated! 🙂

  • 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-23T04:08:23+00:00Added an answer on May 23, 2026 at 4:08 am

    It’s a recursive function over two variables. You can break it apart line-by-line to understand it:

    sponge :: Int -> [a] -> [a]
    

    Two arguments, one an Int, one a list of some elements.

    sponge 0 xs = xs
    

    The base case. If the Int argument is zero, just return the list argument unmodified.

    sponge n [] = []    
    

    Another base case, if the list is empty, immediately return the empty list.

    sponge n (x:xs) = sponge (n-1) xs
    

    Finally, the inductive step. If the list is non-empty (i.e. made up of at least one element and a tail, denoted by x:xs), then the result is sponge called on n-1 and the tail of the list.

    So what will this function do? It will return the tail of the list after dropping n elements. It is the same as the drop function:

    > drop 10 [1..20]
    [11,12,13,14,15,16,17,18,19,20]
    

    And

    > sponge 10 [1..20]
    [11,12,13,14,15,16,17,18,19,20]
    

    In fact, we can ask QuickCheck to confirm:

    > quickCheck $ \n xs -> sponge n xs == drop n xs
    *** Failed! Falsifiable (after 7 tests and 5 shrinks):    
    -1
    [()]
    

    Ah! They’re different. When n is negative! So we can modify the property relating the two functions:

    > quickCheck $ \n xs -> n >= 0 ==> sponge n xs == drop n xs
    +++ OK, passed 100 tests.
    

    So your function behaves like drop, for cases when n is positive.


    Here’s a trace of the intermediate values of n and xs, obtained via the hood debugger:

    enter image description here

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

Sidebar

Related Questions

I would like to know if I can use the following JQuery function on
Does anybody know why the following code only has effect in FF? $(document).ready(function() {
I would like to know following: I added the folder Graphics into my project
I just would like to know how can I dynamically change a label's values
I have a section of code like the following: ---- file.php ---- require_once(mylib.php); function($a,$b)
I know that there is the possibility of Matlab of pausing a Matlab function
I have a couple of functions that I know work correctly. One just counts
suppose that there is given following matlab code function dp = derp(p) n =
I know following is the way to use unicode in C# string unicodeString =
I know the following libraries for drawing charts in an SWT/Eclipse RCP application: Eclipse

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.