Question

I want to read a text File and count the number of vowels in it. I want to know how to convert "ParseFile" content in to a string then pass as a variable to countVowels

Obviously, the way I am trying (passing the function) is not working.

open System.IO
open System


let ParseFile = File.ReadAllLines("C:\\Users\sid\Desktop\AmazonProductAdvertisingAPI\Documents\File3.txt")


\\let countVowels(str : string)

let countVowels (ParseFile) =
    let charList = List.ofSeq str

    let accFunc(As, Es, Is, Os, Us) letter =
        if letter = 'a' then (As+1, Es, Is, Os, Us)
        elif letter = 'e' then (As, Es+1, Is, Os, Us)
        elif letter = 'i' then (As, Es, Is+1, Os, Us)
        elif letter = 'o' then (As, Es, Is, Os+1, Us)
        elif letter = 'u' then (As, Es, Is, Os, Us+1)
        else                   (As, Es, Is, Os, Us)

    List.fold accFunc (0, 0, 0, 0, 0) charList
Was it helpful?

Solution

I can see a few issues with your code:

The line:

let ParseFile = File.ReadAllLines("C:\\Users\sid\Desktop\AmazonProductAdvertisingAPI\Documents\File3.txt") 

Either this line should be:

let ParseFile = File.ReadAllLines(@"C:\Users\sid\Desktop\AmazonProductAdvertisingAPI\Documents\File3.txt") 

or it should be

let ParseFile = File.ReadAllLines("C:\\Users\\sid\\Desktop\\AmazonProductAdvertisingAPI\\Documents\\File3.txt") 

But either way it's not right as it currently stands because either you either need to escape the string with an "@" or you need to escape the backslashes by doubling them.

Try fixing that one--as I say there are other issues but try fixing that one first and then see if you can figure out the other ones for yourself. As Daniel said you should be able to get this working by yourself with a little work.

EDIT:

Oh what the heck; I've benefitted from the largesse of more experienced developers over the years so I can help a bit more than that.

Change this:

let countVowels (ParseFile) = 

to this:

let countVowels str = 

Then add this to the end of your file:

countVowels ParseFile

And, again, as Daniel said, this is pretty basic so if you're just starting, take some time to read the reference he pointed you at and try to understand what you're reading. If you can't master this, you've got no future in software development :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top