Answered step by step
Verified Expert Solution
Link Copied!

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!

  1. let testA = 2
  2. let testB x = 2 + x
  3. let testC x = 2.0 + x
  4. let testD = "hello"
  5. let testE = printfn "hello"
  6. let testF () = 42
  7. let testG () = printfn "hello"
  8. let testH x = String.length x
  9. let testI x = sprintf "%i" x
  10. let testJ x = printfn "%i" x
  11. let testK x = printfn "x is %f" x x // return x
  12. let testL (f:int -> string) x = f x
  13. let testM f (x:int) :string = f x
  14. let testN x :string = x 1 // hint: what does :string modify?
  15. let testO x = 1
  16. let testP x = x 1 // hint: what kind of thing is x?
  17. let testQ x y = x
  18. let testR x y z = z
  19. let testS x = x=x
  20. let testT f = (f 1) + 2
  21. let testU f = sprintf "%i" (f 1)
  22. let testV f = f() + 2
  23. let testW x = fun y -> y * x
  24. let testX x y = y * x
  25. let testY = fun x y -> y * x
  26. let testZ f x = (f 1) + x
  27. 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!

  1. val sigA = int -> int
  2. val sigB = int -> unit
  3. val sigC = int -> string
  4. val sigD = unit -> string
  5. val sigE = string -> string
  6. val sigF = int -> bool -> float -> string
  7. val sigG = x:int -> y:int -> int // 2 different implementation styles, please
  8. val sigH = x:int -> (int -> int) // Hint: define a nested function
  9. val sigI = f:(int -> int) -> int
  10. val sigJ = x:int -> y:int -> z:int -> int
  11. val sigK = f:(int -> int) -> (int -> int)
  12. val sigL = x:int -> f:(int -> int) -> int
  13. val sigM = f:(int -> int -> int) -> int
  14. val sigN = f:('a -> int) -> x:'a -> string // Hint: use sprintf
  15. val sigO = x:int -> ('a -> int)

Q3. Below are four generic signatures. Try to create four functions that are inferred to have these signatures.

  1. val sigW = 'a -> int
  2. val sigX = int -> 'a
  3. val sigY = 'a -> 'a
  4. val sigZ = 'a -> 'b

Thanks in advance!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions