2

Hey one of the requirements of my task is that I do not use type annotation.

Currently my code looks like this

let (currentSeq: string) = 
            specie 
            |> Map.tryFind geneId 
            |> Option.get
let seq1 = currentSeq.[0..pos - 1]
let seq2 = currentSeq.[pos..String.length currentSeq - 1]`

I have been racking my brain for a while now, but I can not figure out how to index a 'chunk' of the string currentSeq, without type annotating it.

rmunn
  • 34,942
  • 10
  • 74
  • 105
CS Dude
  • 421
  • 4
  • 14
  • Somethimes you are stuck with type annotations - this is probably one of them – John Palmer Apr 14 '16 at 05:55
  • Of interest: [F# Ways to help type inference?](http://stackoverflow.com/questions/13821811/f-ways-to-help-type-inference) – Guy Coder Apr 14 '16 at 11:14
  • It might be considered cheating, but you can technically tell the compiler you have a string without using type annotation by passing the value to any function that expects a string before you use that value in your code, even if you throw away the result. For example, `String.length specie |> ignore` would establish for the compiler that `specie` must be a string, and you'll be allowed to treat it that way after that point. – Joel Mueller Apr 16 '16 at 01:28

2 Answers2

3

IIUYC, you need to split a string into two without using type annotations. Here's a way:

let splitAt s pos = 
  let length = s |> String.length
  [ s.[0..pos - 1]; s.[pos..length - 1] ]
yuyoyuppe
  • 1,582
  • 2
  • 20
  • 33
  • It doesn't seem like the nicest way to go about it but it definitely works :) I like it! – CS Dude Apr 14 '16 at 06:26
  • 4
    I think this is probably the nicest and cleanest solution possible. Generally, if you want to avoid type annotations, keep in mind that the compiler processes expressions in the order it seems them. Put some expression first from which the compiler can immediately infer the types - this is the call to String.length in the code by @yuyoyupe. From that moment on, all is good. – Anton Schwaighofer Apr 14 '16 at 09:13
  • I realized that I could simply move the `String.length currentSeq` to above the line in which I attempt to index the string and it no longer needs a type inference. I.e. the result: `let strLength = String.length currentSeq let newSeq = currentSeq.[0..pos - 1] + inSeq + currentSeq.[pos..strLength - 1]` I don't fully understand the compiler but I assume this is because it now has a point of reference to the `currentSeq` being treated as a String. – CS Dude Apr 14 '16 at 22:53
  • 1
    @OliverGiess yes, exactly. don't forget to accept the answer so it doesn't clutter SO – yuyoyuppe Apr 15 '16 at 03:46
-1

you don't need to type annotate it:

let currentSeq = "ojdjdsajdsa"
let seq1 = currentSeq.[0..3 - 1]

your last two seqs are too idented

rmunn
  • 34,942
  • 10
  • 74
  • 105
s952163
  • 6,276
  • 4
  • 23
  • 47
  • The whole problem is that the compiler doesn't know the object is a string by making it a literal you are basically cheating. Also, proposing an edit change to OP's code in the question which changes his code like that is really poor form – John Palmer Apr 14 '16 at 05:55
  • I suspected that might be the case. But once we extract the geneID from the map, the compiler should know it is a string, no? For example: `let rnd = System.Random()` `let abc = ['a'..'z']` `let abc' = abc |> List.map (fun x -> (x,rnd.Next(26).ToString())) |> Map.ofList` `let x = abc'.TryFind 'a' |> Option.get` `let z = x.[0]` – s952163 Apr 14 '16 at 06:02
  • @JohnPalmer indeed. Probably should've confirmed it in comment first. – s952163 Apr 14 '16 at 06:07
  • @s952163 that was my bad, in copying the code across to stack I'll fix it now – CS Dude Apr 14 '16 at 06:12
  • Edited your answer to fix code formatting -- for multi-line code blocks, it's better to use indentation rather than tildes, as using tildes doesn't give you syntax highlighting. The `{}` button in the edit box is for exactly that purpose: highlight the code you want to indent and press the `{}` button, and it will all be neatly indented four spaces, leading to good-looking code in your answer. – rmunn Apr 14 '16 at 06:23