iMPaCT try f#‎ > ‎

Old fsharp tutorial

Getting started

Using the computer as a calculator
This command asks the computer to add 3+4.  
3.0 + 4.0
Try it by:
  • Type (or cut&paste) this command (actually a complete program) into the upper "Script Window."   
  • Highlight the program (by selecting it with your mouse.
  • Press run.
  • Notice the result (called it) in the lower Output Window.

When typing numbers, be sure to type decimal point. Otherwise strange things will happen (try removing one of the decimal points and notice the complaint in the Output Window).

Other arithmetic operators (don't memorize these now, we'll come back to them later):

  • - (dash) for subtraction.
  • * (asterisk) for multiply.
  • / (slash) for divide
  • ** (two asterisks) for power. For example, 24 is written 2.0**4.0.
  • ( ) (parentheses) for grouping, just like in mathematics.

Using variables as value holders

Below we store the result of 3+4 in a variable called a. When reading these commands aloud, we pronounce <- as "gets" and say the entire command as the following sentence "a gets 3 + 4".

a <- 3.0 + 4.0
(try it)

The following just computes a + 1, which is 8, but doesn't change a. (try it)

a + 1.0

Now, let's check that a has not changed (try it)

a

If we want to change a, we can just specify the command a gets a plus 1 as follows (try it)

a <- a + 1.

Let's check a's new value (try it).

a

Drawing a dot

In iMPaCT projects, students write tiny programs to represent the problems that they need to solve. The most basic element of this is drawing a dot.

The following program draws a single dot at position (50.0, 25.0). 

open Impact
dot (50.0, 25.0) blue 
(try it)

A few notes:

  • Every program must begin with the statement open Impact. Otherwise the graphics won't work.
  • Other colors are available including red, green, black, gray, and orange).

Erasing the graph

The clear() command erases everything on the graph.  (try it)

open Impact
clear() 

Drawing a line (part 1)

The following progam draws a line of length 20 as a sequence of dots. Before you try running it, answer for yourself: What will the program draw?

open Impact
clear()
dot (15.,20.) blue
dot (20.,20.) blue
dot (25.,20.) blue
dot (30.,20.) blue
dot (35.,20.) blue

Using variables and arithmetic to simplify

Since we have copy-and-paste, it would be far simpler if we could use the exact same statement to draw each dot. However, to do this, the dot commands can't explicity specify the column number. Todo do this, we will intead store the column number in variable x as follows: (try it)

open Impact
clear()
x <- 15.0
dot (x,20.) green
x <- x + 5.0
dot (x,20.) green
x <- x + 5.0
dot (x,20.) green
x <- x + 5.0
dot (x,20.) green
x <- x + 5.0
dot (x,20.) green
x <- x + 5.0

Thinkers challenge: Try changing the program to draw a sequence of six blue dots in a horizontal row starting from (80, 22) at interval 10.

Using a while statement to control range

Our last step is to exploit the while statement to conveniently repeat dot and addition statements until all the desired dots are drawn. This time, we will draw one dot per column and start from (15,20) and end at (15,40). (try it)

open Impact
clear()
x <- 15.0
while x < 35. do 
    dot (x,20.) black
    x <- x + 1.

Thinker's challenge: modify this program to draw a line from (30,50) to (70, 50).

Hinting at vectors

We won't tell you what this program does. Please draw what you think it will do before you try it.

open Impact clear() x <- 15.0 y <- 20.0 while x < 35. do dot (x,20.) green x <- x + 1. y <- y + 1.

Comments