Header menu logo Teaching

BinderScriptNotebook

Question 1

Calculate 3.0 to the power of 4.0.

answer

3.0 ** 4.0
val it: float = 81.0

Question 2

Assign the integer 1 to a value called a.

answer

let a = 1
val a: int = 1

Question 3

Write a function named add3 that adds 3.0 to any float input.

answer

let add3 x = x + 3.0
val add3: x: float -> float

Question 4

Take the following two-parameter function:

let multiply (x:float) (y:float) = x * y

Use the above function and partial application to define a new function called multiply2 that multiplies its input by 2.0.

answer

let multiply2 = multiply 2.0
val multiply2: (float -> float)

Question 5

Given a tuple (1.0,2.0), assign the second element to a value named b.

answer

let b = snd (1.0, 2.0)
val b: float = 2.0

or

let (_, b1) = (1.0, 2.0)
val b1: float = 2.0

Question 6

Create a tuple where the first, second, and third elements are "a", 1, and 2.0.

answer

("a", 1, 2.0)
val it: string * int * float = ("a", 1, 2.0)

Question 7

Define a record type named Record1 that has a string Id field and a float Y field.

answer

type Record1 = { Id : string; Y : float }
type Record1 =
  {
    Id: string
    Y: float
  }

Question 8

Given the type signature val a : float = 2.0, what is the type of value a?

answer

float

Question 9

Create a record type named Record2. It should have two integer fields X and Y. Create an instance of the record where X = 4 and Y = 2.

answer

type Record2 = { X : int; Y : int }
{ X = 4; Y = 2}
type Record2 =
  {
    X: int
    Y: int
  }
val it: Record2 = { X = 4
                    Y = 2 }

Question 10

Explain why this expression gives an error when you try to run it: 4 + 7.0

answer

Because 4 is an integer and 7.0 is a float. Addition is defined on values with the same type. The two values need to either both be integers or both be floats.

Note: there are some cases where F# will convert (or "automatically widen") integers into floats when it can tell that the conversion is intended. But with simple addition like this, it cannot tell if you are intending to add integers or floats, so it gives an error.

Question 11

Create a list where the elements are 1, 2, and 3.

answer

[ 1; 2; 3 ]
val it: int list = [1; 2; 3]

or

[ 1 .. 3 ]
val it: int list = [1; 2; 3]

or

[ for i = 1 to 3 do i ]
val it: int list = [1; 2; 3]

Question 12

Given the below list, use printfn to print the whole list to standard output using the structured plaintext formatter.

The list to print:

[1; 2; 3]

answer

[1; 2; 3] |> (printfn "%A")
[1; 2; 3]

Question 13

Given the tuple ("hi", false, 20.321, 4), use printfn and the tuple to print the following string to standard output: "hi teacher, my False knowledge implies that 4%=0020.1"

String formatting documentation will be useful.

answer

let (xString, xBool, xFloat, xInt) = ("hi", false, 20.321, 4)
val xString: string = "hi"
val xInt: int = 4
val xFloat: float = 20.321
val xBool: bool = false

Using string interpolation

printfn $"{xString} teacher, my {xBool} knowledge implies that {xInt}%%=%06.1f{xFloat}"
hi teacher, my False knowledge implies that 4%=0020.3

Using old-style printfn

printfn "%s teacher, my %b knowledge implies that %i%%=%06.1f" xString xBool xInt xFloat
hi teacher, my false knowledge implies that 4%=0020.3

val a: int
val add3: x: float -> float
val x: float
val multiply: x: float -> y: float -> float
Multiple items
val float: value: 'T -> float (requires member op_Explicit)

--------------------
type float = System.Double

--------------------
type float<'Measure> = float
val y: float
val multiply2: (float -> float)
val b: float
val snd: tuple: ('T1 * 'T2) -> 'T2
val b1: float
type Record1 = { Id: string Y: float }
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
Record1.Y: float
type Record2 = { X: int Y: int }
Record2.X: int
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Record2.Y: int
val i: int
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
val xString: string
val xBool: bool
val xFloat: float
val xInt: int

Type something to start searching.