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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:42:07+00:00 2026-06-15T13:42:07+00:00

I’m new to algorithm analysis and SML and got hung up on the average-case

  • 0

I’m new to algorithm analysis and SML and got hung up on the average-case runtime of the following SML function. I would appreciate some feedback on my thinking.

fun app([]) = []
  | app(h::t) = [h] @ app(t)

So after every recursion we will end up with a bunch of single element lists (and one no-element list).

[1]@[2]@[3]@...@[n]@[]

Where n is the number of elements in the original list and 1, 2, 3, ..., n is just to illustrate what elements in the original list we are talking about. L @ R takes time linear in the length of list L. Assuming A is the constant amount of time @ takes for every element, I imagine this as if:

[1,2]@[3]@[4]@...@[n]@[] took 1A
[1,2,3]@[4]@...@[n]@[]   took 2A
[1,2,3,4]@...@[n]@[]     took 3A
...
[1,2,3,4,...,n]@[]       took (n-1)A
[1,2,3,4,...,n]          took nA

I’m therefore thinking that a recurrence for the time would look something like this:

T(0) = C                 (if n = 0)
T(n) = T(n-1) + An + B   (if n > 0)

Where C is just the final matching of the base case app([]) and B is the constant for h::t. Close the recurrence and we will get this (proof omitted):

T(n) = (n²+n)A/2 + Bn + C = (A/2)n² + (A/2)n + Bn + C = Θ(n²)

This is my own conclusion which differs from the answer that was presented to me, namely:

T(0) = B                 (if n = 0)
T(n) = T(n-1) + A        (if n > 0)

Closed form

T(n) = An + B = Θ(n)

Which is quite different. (Θ(n) vs Θ(n²)!) But isn’t this assuming that L @ R takes constant time rather than linear? For example, it would be true for addition

fun add([]) = 0
  | add(h::t) = h + add(t) (* n + ... + 2 + 1 + 0 *)

or even concatenation

fun con([]) = []
  | con(h::t) = h::con(t)  (* n :: ... :: 2 :: 1 :: [] *)

Am I misunderstanding the way that L @ R exists or is my analysis (at least sort of) correct?

  • 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-15T13:42:08+00:00Added an answer on June 15, 2026 at 1:42 pm

    Yes. Running the app [1,2,3] command by hand one function call at a time gives:

    app [1,2,3]
    [1]@(app [2,3])
    [1]@([2]@(app [3]))
    [1]@([2]@([3]@(app [])))
    [1]@([2]@([3]@([])))
    [1]@([2]@[3])
    [1]@([2,3])
    [1,2,3]
    

    This is a consequence of the function call being on the left-side of the @.

    Compare this to a naïve version of rev:

    fun rev [] = []
      | rev (x::xs) = rev xs @ [x]
    

    This one has the running time you expect: Once the recursion has fully expanded into an expression ((([])@[3])@[2])@[1] (taking linear time), it requires n + (n – 1) + (n – 2) + … + 1, or n(n+1)/2, or O(n^2) steps to complete the computation. A more effective rev could look like this:

    local
      fun rev' [] ys = ys
        | rev' (x::xs) ys = rev' xs (x::ys)
    in
      fun rev xs = rev' xs []
    end
    
    • 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'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.