1
votes

Can someone explain why compiler is giving me this error

Type mismatch. Expecting a 'a [] -> string
but given a 'a [] -> 'a []
The type 'string' does not match the type ''a []'

on this code snippet:

let rotate s: string = 
  [|for c in s -> c|] 
  |> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)

while the one below compiles just fine:

let s = "string"
[|for c in s -> c|] 
|> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)
1

1 Answers

5
votes

Your first snippet defines function rotate with return type of string.

Try to change it to:

let rotate (s: string) = 
  [|for c in s -> c|] 
  |> Array.permute (function | 0 -> (s.Length-1) | i -> i-1)

In this form you define a function with one string argument (I suppose that's what you wanted) and inferred return type.