Skip to content

InitusNovus/fs_exercise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fs_exercise

The exercise problems for F# practice suggested by ChatGPT

Question

I am trying F#. I just read the basic syntax. Then what is it good to do for me?

Answer from ChatGPT

F# is a versatile language that can be employed in a variety of domains, thanks to its functional-first programming paradigm, strong static typing, and .NET framework integration. Here are some areas where F# can be particularly beneficial:

  1. Data Analysis and Science: F# can help you perform complex data transformations, statistical analyses, and visualization tasks. The language's strong typing and functional features make it easier to work with large datasets and implement complex algorithms.
  2. Financial Applications: F# is a popular choice for quantitative finance and risk management applications. Its functional-first approach allows for more concise and expressive code, which is critical when working with financial models, pricing algorithms, and simulations.
  3. Web Development: F# can be used for web development through libraries like Giraffe or Saturn, which are built on top of ASP.NET Core. These libraries allow you to create web applications using functional programming concepts, resulting in more maintainable and robust code.
  4. Domain-Specific Languages (DSLs): F# excels in creating internal and external DSLs, thanks to its concise syntax, pattern matching, and algebraic data types. You can create languages tailored to specific problem domains, making it easier to model and solve problems in those areas.
  5. Concurrency and Parallelism: F#'s built-in support for asynchronous programming and parallelism allows you to create highly performant and scalable applications. The language's focus on immutability and functional programming can help you write more predictable and robust concurrent code.
  6. Interoperability with .NET: Since F# is part of the .NET ecosystem, you can use it alongside other .NET languages such as C# and VB.NET. This means you can leverage existing .NET libraries, components, and tooling, as well as integrate F# code into existing .NET projects.

As you continue learning F#, consider exploring these areas based on your interests and the problems you'd like to solve. The language's flexibility, expressiveness, and robustness make it a powerful tool for a wide range of applications.

Useful Links

TODOs

  • TorchSharp and DiffSharp for ML
  • P/Invoke to use native C/C++ codes for fast data processing

Problem 1. Fibonacci Sequence

Subject

Recursion, Tail Recursion

Task

Write a function that takes a positive integer n as input and returns the nth number in the Fibonacci sequence. The Fibonacci sequence is defined as follows:

$$F(0) = 0$$

$$F(1) = 1$$

$$F\left(n\right) = \left(n-1\right) + F\left(n-2\right)\quad \mathsf{for} \quad n > 1$$

Here's a template to get you started:

let rec fibonacci n =
    match n with
    | // Your code here

// Test your implementation
printfn "%d" (fibonacci 10) // Should print 55

To solve this problem, you'll need to use pattern matching to handle the base cases (n = 0 and n = 1) and the recursive case (n > 1). When you've completed your implementation, test it with various input values to ensure it produces the correct Fibonacci numbers.

Rewiew

We should a technique called "tail recurssion", which is a form of recursion where the recursive call is the last operation in the function, to avoid stack overflow.

Problem 2. Squares of Odd Numbers

Subject

Higher-Order Functions, List Processing, Pattern Matching

Task

Write a function that takes a list of integers and returns a new list containing the squares of the odd numbers in the input list.

Here's a template to get you started:

let squaresOfOdds numbers =
    // Your code here

// Test your implementation
let testNumbers = [1; 2; 3; 4; 5; 6; 7; 8; 9]
printfn "%A" (squaresOfOdds testNumbers) // Should print [1; 9; 25; 49; 81]

To solve this problem, consider using the List.filter and List.map functions. The List.filter function filters elements from a list based on a predicate (a function that returns true or false), and the List.map function applies a given function to each element of a list and returns a new list with the transformed elements.

Additionally, you can use pattern matching to identify odd numbers. Once you've completed your implementation, test it with various input lists to ensure it produces the correct output.

Problem 3. Palindrome Checker

Subject

String Manipulation, Pattern Matching

Task

Write a function called isPalindrome that takes a string as an argument and returns true if the input string is a palindrome, and false otherwise. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

Write a function called isPalindrome that takes a string as an argument and returns true if the input string is a palindrome, and false otherwise. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

Here's a template to get you started:

let isPalindrome (input: string) =
    // Your code here

// Test your implementation
printfn "%b" (isPalindrome "racecar") // Should print true
printfn "%b" (isPalindrome "hello") // Should print false

To solve this problem, consider using F#'s string manipulation functions to remove spaces, punctuation, and convert the string to lowercase. Then, you can compare the original cleaned string with its reverse. If they are the same, the input string is a palindrome. Once you've completed your implementation, test it with various input strings to ensure it produces the correct output.

Problem 4. Group Elements

Subject

List Processing, Recursion

Task

Write a function called groupElements that takes a list of integers and an integer n as arguments, and returns a list of lists, where each inner list contains n elements from the input list. If the input list does not divide evenly by n, the last inner list should contain the remaining elements. Use list processing functions and recursion to implement this function.

Here's a template to get you started:

let rec groupElements lst n =
    // Your code here

// Test your implementation
let testList = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
printfn "%A" (groupElements testList 3) // Should print [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]; [10]]

To solve this problem, use pattern matching to handle the base case (an empty list) and the recursive case (a non-empty list). In the recursive case, create a new list with the first n elements of the input list, and concatenate this list with the result of calling groupElements on the remaining elements of the input list. Test your implementation with various input lists and group sizes to ensure it produces the correct output.

Problem 5. Person Type and Functions

Subject

Record Types, Function Composition

Task

Define a Person record type with properties FirstName, LastName, and Age. Create the following functions:

  1. createPerson: Takes a first name, last name, and age as arguments, and returns a new Person record.
  2. fullName: Takes a Person record as an argument and returns a string containing the person's full name (i.e., first name and last name separated by a space).
  3. isAdult: Takes a Person record as an argument and returns true if the person's age is 18 or older, and false otherwise.

Here's a template to get you started:

 type Person = {
    FirstName: string
    LastName: string
    Age: int
}

let createPerson firstName lastName age =
    // Your code hear

let fullName person =
    // Your code hear

let isAdult person =
    // Your code hear

// Test your implementation
let person = createPerson "John" "Doe" 25
printfn "Full name: %s" (fullName person)
printfn "Is adult: %b" (isAdult person)

This code defines a Person record type with FirstName, LastName, and Age properties. It also defines the createPerson, fullName, and isAdult functions. Finally, it tests the implementation with a sample person.

Problem 6. File I/O with Simple Text Database

Subject

File I/O, List Processing

Task

Create a simple program that reads and writes records (e.g., people's names and ages) from a text file. Implement the following functions:

  1. readRecords: Takes a file name as an argument and returns a list of records read from the file.
  2. writeRecords: Takes a file name and a list of records as arguments and writes the records to the file.
  3. addRecord: Takes a list of records and a new record as arguments, and returns a new list with the new record added.

Problem 7. Reverse a List

Subject

List processing and recursion

Task

Implement a function that reverses a list using recursion. The function should take a list as input and return a new list with the elements in reverse order.

Here's a template to get you started:

let rec reverseList lst =
    match lst with
    | // Your code here

// Test your implementation
let testList = [1; 2; 3; 4; 5]
printfn "%A" (reverseList testList) // Should print [5; 4; 3; 2; 1]

To solve this problem, use pattern matching to handle the base case (an empty list) and the recursive case (a non-empty list). In the recursive case, you will need to append the head of the input list to the result of reversing the tail of the list. Test your implementation with various input lists to ensure it produces the correct output.

Review

This function is List.rev in F# Core library.

Problem 8. Flatten Nested Lists

Subject

List processing and recursion

Task

Implement a function that flattens a nested list structure. The function should take a nested list as input and return a new list containing all the elements from the input list, in the same order but without any nesting.

Here's a template to get you started:

let rec flatten lst =
    match lst with
    | // Your code here

// Test your implementation
let testNestedList = [[1; 2]; [3; 4; 5]; [6]; []; [7; 8; 9]]
printfn "%A" (flatten testNestedList) // Should print [1; 2; 3; 4; 5; 6; 7; 8; 9]

To solve this problem, use pattern matching to handle the base case (an empty list) and the recursive case (a non-empty list). In the recursive case, you will need to concatenate the head of the input list (which is itself a list) with the result of flattening the tail of the input list. Test your implementation with various input lists to ensure it produces the correct output.

Review

This function is List.collect in F# Core library.

Problem 9. CSV Average Calculator (upcoming)

Subject

File I/O, CSV Processing

Task

Create a function that reads a CSV file and calculates the average of numbers in a specified column. Assume that the CSV file has a header row and the data rows contain only numbers. The CSV file could look like this:

A,B,C
1,2,3
4,5,6
7,8,9

Implement a function averageOfColumn that takes a file name and a column name as arguments, reads the CSV file, and calculates the average of the specified column. Use F#'s file I/O functions and string manipulation functions to process the CSV file. When you've completed your implementation, test it with various CSV files and column names to ensure it produces the correct output.

Here's a template to get you started:

let averageOfColumn fileName columnName =
    // Your code here

// Test your implementation
let testFileName = "test.csv"
let testColumnName = "B"
printfn "%.2f" (averageOfColumn testFileName testColumnName) // Should print 5.00

These problem statements and tasks should provide more guidance for completing the remaining problems.

Problem 10. Contact List Manager (upcoming)

Subject

Command-line interface, Data Manipulation

Task

Create a command-line application that allows users to perform various operations on a list of contacts. The application should support the following operations:

  1. Add a new contact
  2. Delete a contact
  3. Search for a contact by name
  4. Update contact details
  5. List all contacts
  6. Save contacts to a file
  7. Load contacts from a file

To implement this project, consider the following suggestions:

  1. Create a Contact type to represent a contact with properties such as Id, FirstName, LastName, Email, and PhoneNumber.
  2. Use a list or other collection to store contacts in memory.
  3. Create functions to handle each operation (e.g., addContact, deleteContact, searchContact, updateContact, listContacts, saveContacts, and loadContacts).
  4. Use pattern matching and/or if-then-else expressions to process user input and call the appropriate functions.
  5. Use F# file I/O functions to save and load contacts from a file (e.g., using System.IO.File methods).

Your command-line application should be interactive and present a menu to the user. The menu should display the available operations and prompt the user for input. Based on the user's input, the application should perform the corresponding operation and display the result. The application should continue running until the user decides to exit.

Here's a rough outline of how your application might look:

type Contact = {
    Id: int
    FirstName: string
    LastName: string
    Email: string
    PhoneNumber: string
}

// Functions for contact management (addContact, deleteContact, searchContact, updateContact, listContacts, saveContacts, and loadContacts)

let rec mainLoop contacts =
    // Display the menu and prompt the user for input
    // Perform the selected operation
    // Update the contacts list if necessary
    // Call mainLoop with the updated contacts list

// Load contacts from a file (if available) and start the main loop
let initialContacts = loadContacts "contacts.txt"
mainLoop initialContacts

Make sure to test your implementation with various operations and scenarios to ensure it works correctly.

Project 1. Simple Command-Line Todo List Application (upcoming)

Subject

Application Structure, File I/O, Command-Line Interface

Task

Create a simple command-line todo list application that allows users to manage their tasks. The application should support the following operations:

  1. Add a new task.
  2. List all tasks.
  3. Mark a task as complete.
  4. Remove a task.
  5. Save tasks to a file.
  6. Load tasks from a file.

To implement this project, consider the following suggestions:

  1. Create a Task type to represent a task with properties such as Id, Description, and IsCompleted.
  2. Use a list or other collection to store tasks in memory.
  3. Create functions to handle each operation (e.g., addTask, listTasks, markTaskComplete, removeTask, saveTasks, and loadTasks).
  4. Use pattern matching and/or if-then-else expressions to process user input and call the appropriate functions.
  5. Use F# file I/O functions to save and load tasks from a file (e.g., using System.IO.File methods).

Problem 11. Poker Hand Evaluator

Subject

Custom types and pattern matching

Task

Create a simple poker hand evaluator using custom types and pattern matching.

Problem 12. Text Data Analyzer

Subject

Higher-order functions and map-reduce

Task

Implement a map-reduce algorithm to analyze a collection of text data using higher-order functions.

Problem 13. Library System Modeling

Subject

Domain modeling and custom types

Task

Model a library system using F# types and functions for domain modeling.

Problem 14. Custom Record Sorter

Subject

Recursion and custom types

Task

Implement the merge sort algorithm to sort a list of custom records.

Problem 15. Prime Number Summation

Subject

List manipulation and filtering

Task

Find prime numbers within a range and return their sum.

Problem 16. Team Combination Generator

Subject

Combinatorics and list manipulation

Task

Generate possible team combinations for a group project.

Problem 17. Arithmetic Expression Evaluator

Subject

Parsing and expression evaluation

Task

Evaluate arithmetic expressions entered by a user.

Problem 18. Sales Data Analyzer

Subject

Sequence operations and data manipulation

Task

Analyze a list of sales data using sequence operations (filter, map, reduce).

Problem 19. Social Network Friend Recommendation

Subject

List manipulation and algorithms

Task

Implement a simple social network's friend recommendation system using list manipulation functions.

Problem 20. Data Processing Pipeline

Subject

List, array, and sequence conversion

Task

Convert between lists, arrays, and sequences in a simple data processing pipeline.

Problem 21. Infinite Prime Number Generator

Subject

Lazy evaluation and sequence generation

Task

Generate an infinite sequence of prime numbers and find the Nth prime using lazy evaluation.

Project 2. Basic Weather Forecast App

Subject

API interaction and data processing

Task

Create an F# application that fetches weather data from an API and displays it.

Problem 22. Web Scraper

Subject

Async programming and web scraping

Task

Write a web scraper that fetches data from multiple sources asynchronously using async programming.

Problem 23. Login System with Error Handling

Subject

Result type and error handling

Task

Implement a login system using the Result type for error handling in F# functions.

Problem 24. Structured Text Data Parser

Subject

Parsing and data extraction

Task

Build a parser to extract information from structured text data.

Problem 25. Custom Markdown Interpreter

Subject

Interpreters and parsing

Task

Design an interpreter for a custom markdown language.

Problem 26. Bank Account Simulation

Subject

State monad and functional programming

Task

Implement a state monad for a simple simulation of a bank account system.

Problem 27. Basic UI Components

Subject

GUI programming and Avalonia.FuncUI

Task

Create a simple GUI application with basic UI components (buttons, labels, text inputs).

Problem 28. Event Handling and State Management

Subject

Event handling and state management in GUI applications

Task

Implement event handling and state management in a GUI application.

Problem 29. Data Binding

Subject

Data binding in GUI applications

Task

Bind data to UI components in a GUI application.

Problem 30. Custom Styling

Subject

Styling and customization in GUI applications

Task

Apply custom styles to UI components in a GUI application.

Project 3. Simple Note-Taking Application

Subject

GUI programming, event handling, and data management

Task

Build a simple note-taking application using Avalonia.FuncUI.

Problem 31. Simple Web Server

Subject

Web framework and server setup

Task

Build a basic web server using the Giraffe web framework.

Problem 32. RESTful Endpoint

Subject

RESTful API and web framework

Task

Create a RESTful endpoint for managing a resource using F# and the Giraffe web framework.

Project 4. Build a Simple REST API

Subject

Web development, API design, and Giraffe web framework

Task

Develop a simple RESTful API using F# and the Giraffe web framework.

Project 5. Functional Event Sourcing

Subject

Event sourcing and functional programming

Task

Implement event sourcing in a functional way using F# for a simple ### Task management system.

Problem 33. Tree View and Data Hierarchy

Subject

GUI programming and hierarchical data

Task

Implement a tree view to display hierarchical data in a GUI application.

Problem 34. Custom UI Components

Subject

GUI programming and custom components

Task

Create custom UI components and reuse them in a GUI application.

Problem 35. Data Validation

Subject

GUI programming and data validation

Task

Implement data validation for user input in a GUI application.

Problem 36. Navigation and Routing

Subject

GUI programming and navigation

Task

Set up navigation and routing between different views in a GUI application.

Problem 37. Asynchronous Data Fetching

Subject

GUI programming and async programming

Task

Fetch data asynchronously and update the UI in a GUI application.

Project 6. Personal Finance Manager

Subject

GUI programming, data management, and Avalonia.FuncUI

Task

Build a personal finance manager application using Avalonia.FuncUI, where users can add, edit, and track their expenses and incomes.

Problem 38. Domain Modeling

Subject

Domain modeling and custom types

Task

Model a simple domain using F# types and functions.

Problem 39. Domain Modeling for an E-commerce System

Subject

Domain modeling and functional programming

Task

Design a domain model for a simple e-commerce system using F# and functional programming concepts.

Problem 40. Authentication and Authorization

Subject

Web development and security

Task

Implement authentication and authorization in a web application using F# and the Giraffe web framework.

Problem 41. Server-side Rendering

Subject

Web development and server-side rendering

Task

Set up server-side rendering for a web application using F# and the Giraffe web framework.

Problem 42. Form Handling and Validation

Subject

Web development and form handling

Task

Handle form submissions and perform validation on the server-side in a web application using F# and the Giraffe web framework.

Problem 43. Websockets

Subject

Web development and real-time communication

Task

Implement real-time communication using websockets in a web application with F# and the Giraffe web framework.

Project 7. Real-Time Chat Application

Subject

Web development, real-time communication, and websockets

Task

Develop a real-time chat application using F#, the Giraffe web framework, and websockets.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages