Over on his blog, Andrei Formiga has a series of post on implementing ROT13 in F# and Haskell.<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />/o:p

http://codemiscellany.blogspot.com/2006/12/rot13-in-f.html/o:p

http://codemiscellany.blogspot.com/2006/12/rot13-in-haskell.html/o:p

http://codemiscellany.blogspot.com/2006/12/rot13-in-f-revisited.html/o:p

http://codemiscellany.blogspot.com/2006/12/still-more-rot13.html/o:p

 /o:p

I like is implementation using library functions that don’t yet exist in F#, such as drop and zip. Here is the implementation itself, stripped of the extra library functions he had to implement, for the original see the above links. /o:p

 /o:p

#light/o:p

let rot13 s =
    let letters = [‘a’ .. ‘z’]
    let transp = zip letters ((drop 13 letters) @ (take 13 letters))
    let rotchar c = List.assoc c transp
    strmap rotchar s/o:p

 /o:p

The clever bit of this implementation is on the 4th line where he uses the functions drop and take to create a rotated list and the function zip to a map between that and the original letter list. While this is an interesting way of implementing this I think there is a way to do this without the need to define extra library functions. So here is my implementation, the same number of lines but more characters!/o:p

 /o:p

#light/o:p

let rot13 (s : string) =     /o:p

    let letters = [‘a’ .. ‘z’]/o:p

    let transp = letters |> List.mapi (fun i l -> l, List.nth letters ((i + 13) % 26))     /o:p

    let rotchar c = List.assoc c transp/o:p

    new string(s.ToCharArray() |> Array.map rotchar)/o:p

 /o:p