CSCI 4500/6500 Programming Languages

Project 4: SML

First Program in SML

 

 

Assigned: Tuesday February 28, 2006
Due: Thursday March 09, 2006

Collaboration Policy - Read Carefully

You must work on this project individually, but you may discuss this assignment with other students in the class and ask and provide help in useful ways, preferable over our email list so we can all benefit from your great ideas. You may consult any outside resources you wish including books, papers, web sites and people (but no penguins or sea urchins).

If you use resources other than the class materials, indicate what you used along with your answer.

Objective:

The main objective for this assignment, is of-course for you to familiarize yourself with another functional language, i.e. SML and what it entails (e.g. high-level functions). This time the programming environment will not be as nice as Dr. Scheme )-: Here are some hints on getting SML onto your system:

  1. Go to URL: http://www.smlnj.org/dist/working/110.57/index.html (click on link)
  2. Pick a distribution under either "Unix", "MacOS X" or Microsoft Windows and read the corresponding instructions.
  3. Install the software according to instructions
  4. If you have a Unix system, set up your PATH variable where you installed it
  5. Run it by typing
    {saffron:ingrid:219} sml
    Standard ML of New Jersey v110.57 [built: Mon Nov 21 09:35:29 2005]
    -
  6. Prompt is a '-' sign.

 

Runs on Microsoft Windows, MacOS X (yay!), UNIX.

Some tutorials on ML and SML:

SML/NJ Literature and Resources :

A Short & Comprehensive Tutorial:

Gentle Introduction to ML:

Some Scheme/ML comparisons:

Robert Harper's:

Peter Lee's:

Background:

Write an ML program to find prime numbers. According to Webster's dictionary, "an integer greater than one is prime if its only positive divisors are itself and one. Here is an interesting article from science news on detecting primes (not quite the assignment but something worth reading).

http://www.sciencenews.org/20021026/bob9.asp

 

Description:

Your assignment is to create a "driver" function named "go()", so that the interaction between a person or user and your program will look as follows (the user input is denoted in red):

Author: Airam Ugalator
Program: #4, Find prime numbers.
Please enter a positive integer to check: 1
Limit for checking factors is: 1
Potential factors:
1 is prime.
val it = () : unit
- go();
Author: Airam Ugalator
Program: #4, Find prime numbers.
Please enter a positive integer to check: 17
Limit for checking factors is: 5
Potential factors: 2 3 5
17 is prime.
val it = () : unit
- go();
Author: Airam Ugalator
Program: #4, Find prime numbers.
Please enter a positive integer to check: 25
Limit for checking factors is: 5
Potential factors: 2 3 5
5 is a factor.
val it = () : unit
- go();
Author: Airam Ugalator
Program: #4, Find prime numbers.
Please enter a positive integer to check: 32761
Limit for checking factors is: 181
Potential factors: 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43
45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97
99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137
139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177
179 181
181 is a factor.
val it = () : unit
-

 

Details:

Your program should test whether a number is prime. A number is prime if there are no factors of the number other than 1 and itself. You should test different factors and see if they divide your number. You should try 2 as a potential factor first, next you should try odd numbers starting with 3, until you reach the first potential factor greater than sqrt( number ). As your program runs it should display the potential factors it tries.

You should only need to use "Statement" lists (sequence of statements enclosed in parenthesis) in cases where you have print statements that need to be grouped.

An additional requirement is that you read the input number into a string, then write your own function to convert the string into an integer. You will likely need to write additional functions to convert an integer into a string (so that you can use it with print), and your own function to calculate ceil( sqrt( number)).


The main driver function of your program should look like:

(* -----------------------------------------------------------------

Main driver function

-------------------------------------------------------------------*)
fun go() = (

print( "\n");
print( "Author: Airam Ugalator\n");
print( "Program: #4, Find prime numbers. \n");
print( "Please enter a positive integer to check: " );


isPrime( TextIO.input(TextIO.stdIn) )

);
Note that this function must be named "go( )" and must have 0 parameters as shown. It accepts input from the keyboard, which it sends on to the isPrime( ...) function, which you will write.

You will likely also need to write functions to convert an integer into a string (for use with the "print" command), as well as a function to estimate the square root of a number.

The constraints given will help you to focus on a truly functional programming style.

If you are struggling with writing recursive functions in SML, I suggest you first write the recursive functions in Schem under the Dr Scheme environment. You can then translate them into SML, or you can write them in C and then and gradually adjust your methods of using recursion to eliminate any local variables.

Note that a sequence of steps can always be transformed into a sequence of function calls. This illustrates the fact that statement lists (surrounded by parenthesis) are not theoretically needed, though you may use them for this program. Note also that a parameter can often be used in a recursive function where you might otherwise seem to need a local variable. The first time you call such a recursive function you can just send it a dummy value to stand in for that parameter

 

Other Requirements:

Please do not use the below:

 

Submitting

  1. Create a directory x500_program4
  2. Put all the materials needed (all sml files) in the above directory
  3. Submit via the 'submit' command (while on atlas.cs.uga.edu)
{atlas:maria} submit x500_program4








*Adapted from: http://logos.cs.uic.edu/476/Programs/prog1Primes.html