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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:21:28+00:00 2026-06-17T09:21:28+00:00

I am having two lists in SML, lets say list A [(a,b,c),(d,e,f)] and list

  • 0

I am having two lists in SML, lets say list A [(a,b,c),(d,e,f)] and list B [b,e]. I want to count how many occurrence of each item in B that matches the second element of each triple in A. The output should be 2. Because b and e each occurs once in A.

This is my code so far but my counter is always set to 0 when I move from one element to another in B. I know in Java this will just be a simple double for loop.

fun number_in_months (d : (int * int * int ) list, m : (int) list) = 
    if null m then 0 
    else if null d then number_in_months(d, tl m)
    else if (#2(hd d)) = (hd m) then 1 + number_in_months (tl d, m)
    else number_in_months(tl d, m)
  • 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-17T09:21:29+00:00Added an answer on June 17, 2026 at 9:21 am

    As you mention, it would be a nested/double loop in any imperative programming language. What you are actually missing is the second loop.

    Your “inner” loop goes through all elements of d, and when this is done, your “outer” loop tries to pop the top element of m and start all over, as seen from this line of your code:

    else if null d then number_in_months(d, tl m)
    

    However as you can see, you have just tested the list d to be empty and you supply this (exact same list) to your recursive call on the tail of m, which will then fall in this same case for each successive call until m is also empty and you return 0.

    Thus what you are missing is to “keep a copy” of the original input list m. This can be done in various ways, but an inner (helper) function is properly the most used one and it even “looks” like a nested loop

    fun number_in_months (d, m) =
        let
          fun nim' ([], y::ys) = nim (d, ys)                 (* 1 *)
            | nim' (_, []) = 0                               (* 2 *)
            | nim' ((_, x2, _) :: xs, yss as (y::ys)) = ...  (* 3 *)
        in
          nim'(d, m)
        end
    

    Using pattern matching the above code gets much simpler and less error prone. In case 1, the “inner” loop has gone through all elements in d, thus the recursive call using d from the outer function which is not changed at any time. In case 2, the “outer” loop has gone through all elements of m and we return 0 (the neutral element of addition). In case 3 we do the actual work. Here pattern matching is used such that we don’t need to enforce the type of the argument and we don’t need to pull out the 2nd element of the triple, we already have it in the variable x2. All that is needed is to do the computation and make a recursive call with xs and yss.

    When doing it this way, the inner (helper) function is using a “copy” of the original input list d and stepping through its elements (potentially modifying it), but we always got a reference to the original input list, which we can use if/when needed.

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

Sidebar

Related Questions

I have two ordered lists of the same element type, each list having at
I'm having a dropdown list in my web page. And it contains two lists.
I have two list of different objects : List<Report> List<Newsletter> each having a 'created
Having two lists [A,B,C,D] and [1,2,3,4] , I want to provide a user interface
Having two lists of same object type. I want to join them using an
I'm having two lists. List A List B a 10 b 5 c 20
I'm having two sortable lists where I can add items from the first list
Having two lists, I want to get all the possible couples. (a couple can
I'm having two lists of objects, users and products users own products, and each
Here i am having a unordered list displaying two columns code and name .I

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.