Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use F#: Start by creating a text file on your system that contains integers, one per line. For example, create data.txt with the following

Please use F#: Start by creating a text file on your system that contains integers, one per line. For example, create data.txt with the following five values: 15 8 25 17 12 Your program must read from the data.txt file and not operate only on these hard coded values. Your exercise is to write a program that inputs the name of a file, then outputs the contents of the file (as a list of integers), the length, the sum, and the average (as a double). For the file above, heres how the program should behave:

image text in transcribed

Heres the main program, your job is to write the functions length, sum, and average:

[]

let main argv =

printf "filename> "

let filename = System.Console.ReadLine()

let data_array = System.IO.File.ReadAllLines(filename)

let data_list = Array.toList data_array

//

// convert strings to integers:

//

let values = List.map (fun s -> int s) data_list

printfn "%A" values

//

let len = length values

printfn "%A" len

//

let total = sum values

printfn "%A" total

//

let avg = average values Page 5 of 5

printfn "%A" avg

//

0

0

Write length, sum and average recursively; define above main. Note that avg should return a double value, there is no default casting up from integers to doubles so you will need to construct a double from the integer values.

Time Elapsed 00:00:03.08 (base) Yazhinis-MacBook-Pro: program yazhinipriyadharshini$ dotnet build Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core Copyright (c) Microsoft Corporation. All rights reserved. Restore completed in 24.89 ms for /Users/yazhinipriyadharshini/Desktop/program/program.fsproj. program -> /Users/yazhinipriyadharshini/Desktop/program/bin/Debugetcoreapp3.1/program.dll Build succeeded. Warning(s) Error(s) n Time Elapsed 00:00:02.90 (base) Yazhinis-MacBook-Pro:program yazhinipriyadharshini$ dotnet run filename> data.txt [15; 8; 25; 17; 12] 77 15.4 (base) Yazhinis-MacBook-Pro:program yazhinipriyadharshini$ Time Elapsed 00:00:03.08 (base) Yazhinis-MacBook-Pro: program yazhinipriyadharshini$ dotnet build Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core Copyright (c) Microsoft Corporation. All rights reserved. Restore completed in 24.89 ms for /Users/yazhinipriyadharshini/Desktop/program/program.fsproj. program -> /Users/yazhinipriyadharshini/Desktop/program/bin/Debugetcoreapp3.1/program.dll Build succeeded. Warning(s) Error(s) n Time Elapsed 00:00:02.90 (base) Yazhinis-MacBook-Pro:program yazhinipriyadharshini$ dotnet run filename> data.txt [15; 8; 25; 17; 12] 77 15.4 (base) Yazhinis-MacBook-Pro:program yazhinipriyadharshini$

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+25-3 Identify the depressants, and describe their effects.

Answered: 1 week ago

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago

Question

Draft a proposal for a risk assessment exercise.

Answered: 1 week ago