Answered step by step
Verified Expert Solution
Question
1 Approved Answer
F# Programming: Type Inference Q1. For each of the following definitions, say whether it is a simple value or a function, and if a function,
F# Programming: Type Inference
Q1. For each of the following definitions, say whether it is a simple value or a function, and if a function, what is the function signature? If in doubt, run them in the F# Interactive Tool to find out!
- let testA = 2
- let testB x = 2 + x
- let testC x = 2.0 + x
- let testD = "hello"
- let testE = printfn "hello"
- let testF () = 42
- let testG () = printfn "hello"
- let testH x = String.length x
- let testI x = sprintf "%i" x
- let testJ x = printfn "%i" x
- let testK x = printfn "x is %f" x x // return x
- let testL (f:int -> string) x = f x
- let testM f (x:int) :string = f x
- let testN x :string = x 1 // hint: what does :string modify?
- let testO x = 1
- let testP x = x 1 // hint: what kind of thing is x?
- let testQ x y = x
- let testR x y z = z
- let testS x = x=x
- let testT f = (f 1) + 2
- let testU f = sprintf "%i" (f 1)
- let testV f = f() + 2
- let testW x = fun y -> y * x
- let testX x y = y * x
- let testY = fun x y -> y * x
- let testZ f x = (f 1) + x
- let testZZZ f g x = g (f x) // apply f to x, then apply g to the result
Q2. For each of the following signatures, create a function that will be inferred to have that signature. Avoid using explicit type annotations!
- val sigA = int -> int
- val sigB = int -> unit
- val sigC = int -> string
- val sigD = unit -> string
- val sigE = string -> string
- val sigF = int -> bool -> float -> string
- val sigG = x:int -> y:int -> int // 2 different implementation styles, please
- val sigH = x:int -> (int -> int) // Hint: define a nested function
- val sigI = f:(int -> int) -> int
- val sigJ = x:int -> y:int -> z:int -> int
- val sigK = f:(int -> int) -> (int -> int)
- val sigL = x:int -> f:(int -> int) -> int
- val sigM = f:(int -> int -> int) -> int
- val sigN = f:('a -> int) -> x:'a -> string // Hint: use sprintf
- val sigO = x:int -> ('a -> int)
Q3. Below are four generic signatures. Try to create four functions that are inferred to have these signatures.
- val sigW = 'a -> int
- val sigX = int -> 'a
- val sigY = 'a -> 'a
- val sigZ = 'a -> 'b
Thanks in advance!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started