Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ex.1
- let convertStringToWords s =
- List.foldBack (fun c acc -> if c = ' ' then [] :: acc else [c :: acc.[0]] @ acc.[1..acc.Length-1]) [for ch in s -> ch] [[]]
- let printWordsToLines lineLength (ws:char list list) =
- if ws |> List.map (fun w -> w.Length) |> List.max > lineLength then
- failwith "Can not fit"
- let rec wordsToLines (ws:char list list) offset =
- match ws with
- | [] -> []
- | head::tail -> if head.Length + offset < lineLength
- then head @ [' '] @ wordsToLines tail (offset+head.Length+1)
- else ['\n'] @ head @ [' '] @ wordsToLines tail (head.Length+1)
- wordsToLines ws 0
- //Test
- convertStringToWords "Functional programming Fsharp" |> printWordsToLines 20
- (*Prints ['F'; 'u'; 'n'; 'c'; 't'; 'i'; 'o'; 'n'; 'a'; 'l'; ' '; '\010'; 'p'; 'r';
- 'o'; 'g'; 'r'; 'a'; 'm'; 'm'; 'i'; 'n'; 'g'; ' '; 'F'; 's'; 'h'; 'a'; 'r';
- 'p'; ' ']*)
- convertStringToWords "Functional programming Fsharp" |> printWordsToLines 8
- //Shows an error - longest word is longer then 8
- //Ex.2
- let maximumWordsOnLine s =
- let splitString s del =
- List.foldBack (fun c acc -> if c = del then [] :: acc else [c :: acc.[0]] @ acc.[1..acc.Length-1]) [for ch in s -> ch] [[]]
- splitString s '\n' |> List.map (fun line -> (splitString line ' ').Length) |> List.max
- //Test
- maximumWordsOnLine "one two three\none two three four\none\none two\none two three four five\none"
- //Returns 5
- //Ex.3
- let countNumOfVowels s =
- let countVovel v =
- List.fold (fun acc c -> acc + (if List.contains c [v; System.Char.ToUpper v] then 1 else 0)) 0 [for ch in s -> ch]
- (countVovel 'a', countVovel 'e', countVovel 'i', countVovel 'o', countVovel 'u')
- //Test
- countNumOfVowels "paradigm"
- //returns (2, 0, 1, 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement