Example#17(Array.Iter)

Problem: Make a console application that will convert the following singular nouns to plural. Use Array.Iter:

1. ally
2. army
3. baby
4. lady
5. navy

  1. // Learn more about F# at http://fsharp.net  
  2. open System  
  3. //change the console title  
  4. System.Console.Title<-"Convert to Plural"  
  5. //adds the foreground and background color  
  6. System.Console.ForegroundColor<-ConsoleColor.Blue  
  7. System.Console.BackgroundColor<-ConsoleColor.White  
  8. //clears the screen. This is to apply the background color once the  
  9. //console application is loaded  
  10. System.Console.Clear()  
  11. //assigns the singular nouns to our array variable singularnouns  
  12. let singularnouns=[|"ally";"army";"baby";"lady";"navy"|]  
  13. printfn "Singular words:"  
  14. //iterates through the values of our singularnouns array variable  
  15. singularnouns|>Array.iter(fun singularwords->printfn "%s" singularwords)  
  16. printfn "Plural words:"  
  17. //iterates through the values of our singularnouns array variable  
  18. //replace the word in each array element from "y" to "ies"  
  19. singularnouns|>Array.iter(fun pluralnouns->  
  20. let pluralwords= pluralnouns.Replace("y","ies")  
  21. printfn "%s" pluralwords)  

This will display the following output: