Printing an ASCII table

Viewed 95

So I wrote this code in C to print the ASCII table, but I was told to use SML for this specific task

Here is my current code in C:

// Program to print ASCII table.

#include <stdio.h>
int main()
{
    unsigned char count;
    for(count=32; count< 255; count+=1)
    {
        printf("  %3d - %c",count,count);
        if(count % 6==0)
            printf("\n");
    }
    return 0;
}

How would I go about doing this in SML? I scoured the internet but with no luck!

1 Answers

How would I go about doing this in SML?

First off, you would learn SML. ;-)

  • Instead of for(count=32; count< 255; count+=1) you might use List.tabulate.
  • In SML printf is considered a big gun, so perhaps stick to print.
  • Instead of %c you can use str and chr, e.g. str (chr 65) = "A"
  • Instead of %03d you do get Int.toString, but you have to make your own pad function, yay!
  • For applying a function f to every element of a list and discard the return values (which makes sense if you're interested in only the side-effects of f), you can use List.app rather than List.map.

A template for getting started is:

val table = List.tabulate (256 - 32, fn count => str (chr (count + 32)))
val _ = List.app (fn cstr => print "Ceci n'est pas une ASCII table\n") table

Try and make a reasonable attempt to solve this task and provide an answer to your own StackOverflow question.

Related