CS 341Programming Languages
Spring 2023
Homework #04
Complete By:
Policy:
Assignment:
Submission:
Thursday, March 9th @ 11:59pm
Individual work only, late work *not* accepted
complete F# program
submit “main.fs” electronically on Gradescope
Pre-requisites: Lecture information
Reading / F# References
Here are some references on F# that prior students have found helpful:
http://dungpa.github.io/fsharp-cheatsheet/
https://fsharp.org/learn.html
https://www.tutorialspoint.com/fsharp/index.htm
Programming environments for F#
The quickest way to get started is using replit.com: there is a project setup for this homework named
“Homework 04” which is configured with F# and starter code. Alternatively, you can install Visual Studio
Community Edition for Windows or Mac, both offer F#; create a new F# Console project to start programming.
If you decide to work outside replit.com, be sure to download the starter code of “main.fs”.
[ NOTE: Visual Studio Code is a different product, and is an editor, not an IDE — you can use VS Code if you
want, but you’ll also need to install the necessary F# libraries and compilers. ]
Homework Exercise
The exercise is to input a string from the user, and then output the following information:
•
•
•
Length of string (# of characters, including spaces and punctuation)
# of vowels in the string (a, e, i, o, u)
# of each vowel
Page 1 of 5
•
•
# of digraphs in the string (ai, ch, ea, ie, ou, ph, sh, th, wh)
# of each digraph
For simplicity, only count lower-case vowels and digraphs. To help you get started, we are providing starter
code that inputs a string from the user and calls a few functions that currently return 0:
When your program is complete, here’s an example of how it should behave given the input: “shush!” said
the person on the train as i used my phone
The most natural way to solve this problem in a functional language is to convert the input string to a list of
characters, and then work with the resulting list. Two functions are provided for converting strings: explode s
takes a string and returns a list of characters, and implode L takes a list of characters and returns a string.
Here’s the starter code here for your reference:
Page 2 of 5
//
// F# program to input a string and print out information
// about the # of vowels and digraphs in that string.
//
// Name:
???
// UIC NetID: ???
//
// Original author:
//
Prof. Joe Hummel
//
U. of Illinois, Chicago
//
CS 341, Spring 2022
//
//
// explode:
//
// Given a string s, explodes the string into a list of characters.
// Example: explode “apple” => [‘a’;’p’;’p’;’l’;’e’]
//
let explode (S:string) =
List.ofArray (S.ToCharArray())
//
// implode
//
// The opposite of explode — given a list of characters, returns
// the list as a string. Example: implode [‘t’;’h’;’e’] => “the”
//
let implode (L:char list) =
new string(List.toArray L)
let rec length L =
0
let rec numVowels L =
0
[]
let main argv =
printfn “Starting”
printfn “”
//
// input string, output length and # of vowels:
//
printf(“input> “)
let input = System.Console.ReadLine()
let L = explode input
printfn “exploded: %A” L
Page 3 of 5
let len = length L
printfn “length: %A” len
let num = numVowels L
printfn “vowels: %A” num
//
// TODO: print count of each vowel:
//
//
// TODO: print number of digraphs, count of each:
//
//
// done: implode list, print, and return
//
let S = implode L
printfn “imploded: %A” S
printfn “”
printfn “Done”
0 // return 0 => success, much like C++
When producing output, use printfn with the %A option as shown above. This is the easiest way to output
values in F#, and will match the required output when submitting to Gradescope.
Requirements
In order to earn full points for this homework, your code must meet the following requirements:
•
•
•
No variables (i.e. no use of the keyword mutable).
No loops. Instead, use recursion or higher-order programming via List.map or List.iter.
Compute the length of the list yourself, do not call List.length; recall we wrote the length function
in class.
Challenge: it’s easy to fall into the trap of relying on copy-paste to solve this exercise. For example, you can
write a function to count the number of ‘a’, then copy-paste and modify to count the number of ‘e’, then
copy-paste again to count the number of ‘i’. And so on. Instead, write a single function that is parameterized
by the vowel, and just call this same function 5 times. Then, the next step would be abstract those 5 calls into
one call to List.iter / List.map.
Grading and Electronic Submission
A few days before the assignment is due, login to Gradescope.com and look for the assignment “HW04”.
Page 4 of 5