dimanche 25 octobre 2015

F# Use result of boolean function in if statement

I'm trying to write a pig latin translator in F#. To translate, I need to know if a word starts with a vowel or not. To do that, I'm trying to use this function which I wrote...

(*Tests if an element is in a list*)
let isInList elementToFind listToCheck = 
    List.fold(fun a b -> a || b = elementToFind) false listToCheck;

to test if the first character in a word is in a list of all vowels. Here is what my attempt looks like

(*Takes a word and translates it to pig latin*)
let translateWord wordToTranslate : string = 
    let startsWithVowel = isInList(wordToTranslate.[0], ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']);
    if startsWithVowel then
        translateWordStartingWithVowel(wordToTranslate)
    else
        translateWordStartingWithConsenant(wordToTranslate);

Which is giving several errors. It's saying wordToTranslate.[0] doesn't have enough type constrants and startsWithVowel is of the wrong type. The full error texts are

Severity    Code    Description Project File    Line
Error       The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding further type constraints   Pig Latin FSharp

Severity    Code    Description Project File    Line
Error       This expression was expected to have type
    bool    
but here has type
    ('a * (char * char * char * char * char * char * char * char * char * char) list) list -> bool  Pig Latin FSharp

How can I fix this approach so that it does what I want it to do? I'm relatively new to F# so any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire