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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:25:02+00:00 2026-05-30T04:25:02+00:00

I am a complete newbie to sml and am having trouble with the syntax

  • 0

I am a complete newbie to sml and am having trouble with the syntax for inner functions. What I need to do is take a list of a list of ints, average each list, and return a list of reals. This is the psuedo-ish code I have so far.

 fun listAvg [] = 0  
    else (sum (x) div size (x))

        fun sum[] = 0
        | sum(head::rest)= head + sum rest;


        fun size [] = 0
        | size(head::rest) = 1 + size rest;

    listAvg([[1,3,6,8,9], [4,2,6,5,1], [9,5,9,7], [5,4], [3,6,4,8]]); 

any advice would be greatly appreciated. Thanks!

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

    Use a let, as in

    fun listAvg [] = 0  
      | listAvg x =
        let
            fun sum[] = 0
              | sum(head::tail)= head + sum tail;
            fun size [] = 0
              | size(head::tail) = 1 + size tail;
         in
             (sum x) div (size x)
         end
    

    You have to pass an int list to this function e.g.

    listAvg [1, 2, 3, 4];
    

    This is no change to your code, except to rearrange the order and putting the keywords let, in, and end. If this is not homework, I would recommend using a few built-in standard library functions in the List structure that could reduce this function to two lines, including the pattern match on the empty list.

    EDIT:

    There are two possible meanings to “average a list of a list of ints.” The first is to average each list and then take the average of the averages, and the second is to join the lists together into one long list and take the average over the entire list. The two methods are equivalent (except for rounding errors) when all the lists of ints are of the same length, but as your example shows, they don’t have to be the same length.

    Since this is homework, I’m not going to give you the answer directly, but consider the following, which might be helpful:

    • If you’re using the first interpretation of “average a list of a list of ints:” there is a built-in SML function that will let you apply another function to each element of a list, and get the resulting list. This might be helpful for getting the individual averages, which you can then combine together into an overall average.
    • If you’re using the second interpretation: there is a built-in SML function (it looks like an operator, but many of the things that look like operators in SML are just infix functions) to join two lists together, and a built-in SML function to apply a function to elements going down the list, along with an accumulator value, to generate a single value. You might be able to use those two functions to create one long list of all the numbers, which you can then average.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a complete newbie to programming and am having a lot of trouble with
I'm a complete newbie when it comes to servers, so I need some help.
Complete newbie working with jQuery and am having a bit of a problem. I
I'm a complete newbie to programming and am having problems implementing a SELECT query
I'll need to preface this with the fact that I am a complete newbie
I am a complete newbie at powershell. What I need is a powershell script
I'm a complete newbie to VB.NET, how can I test if a dataset has
I am complete newbie for programming and this is my first real program I
I am a complete newbie to iOS development but not to programming. I am
I'm a complete newbie when it comes to OCaml. I've only recently started using

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.