-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Homework 2 is ready #5
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 и 4 надо немного поправить, 2 и 3 зачтены
// Использование рекурсии не допускается, зато нужен FsCheck для проверки функций | ||
// на эквивалентность | ||
|
||
/// <summary>Counts even numbers in the list using map.</summary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вообще, в F# не принято писать summary, пишут просто ///. Тулы про это знают и могут понять, что это и есть summary
|
||
/// <summary>Counts even numbers in the list using map.</summary> | ||
let countEvenInListMap list = | ||
if list = [] then None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не-а, в пустом списке ровно 0 чётных чисел
/// <summary>Counts even numbers in the list using map.</summary> | ||
let countEvenInListMap list = | ||
if list = [] then None | ||
else Some(List.sum(List.map(fun x -> if x % 2 = 0 then 1 else 0) list)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это было бы красивее через |>
let infiniteSeqOfPrimes = | ||
let rec isPrime n acc = | ||
if n = 2 then true | ||
elif float acc > sqrt (float n) then true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Проще было бы acc * acc > n
|
||
Seq.initInfinite(fun index -> | ||
let n = index + 1 | ||
findNPrime n 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да ну, слишком сложно. Можно сгенерить последовательность всех чисел вообще и выбрать (через Seq.filter) из них те, которые удовлетворяют предикату isPrime. Думайте в терминах ленивых вычислений :)
hw2/FSharpHW2/FSharpHW2/Program.fs
Outdated
// let myTree = Node(5, Node(2, Leaf, Leaf), Leaf) | ||
// let sq x = x * x | ||
|
||
// mapTree myTree sq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Закомментированный код нехорошо, да и вообще, есть тесты и F# Interactive, main не очень полезен
|
||
[<Test>] | ||
let ``CountEvenInListMap for the list [2; 2; 2; 2] should return Some(4)`` () = | ||
countEvenInListMap[2; 2; 2; 2] |> should equal (Some 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут просили FsCheck
|
||
[<Test>] | ||
let ``Infinite sequence of primes should return 0`` () = | ||
Seq.findIndex(fun x -> x = 2) infiniteSeqOfPrimes |> should equal 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне кажется, проще через Seq.nth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В целом всё так, зачтены
if list = [] then 0 | ||
else List.sum <| List.map(fun x -> if x % 2 = 0 then 1 else 0) list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем if, List.map и для пустого списка работает
/// Counts even numbers in the list using filter. | ||
let countEvenInListFilter list = | ||
if list = [] then 0 | ||
else List.filter(fun x -> x % 2 = 0) list |> List.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Что-то с пробелами беда. Это же не вызов функции filter от аргумента (fun x -> x % 2 = 0)
, это вызов от двух аргументов, первый из которых в скобках, чтобы приоритет операций ничего не сломал. Перед "(" надо пробел.
Seq.initInfinite(fun index -> | ||
let n = index + 1 | ||
findNPrime n 2) | ||
*) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Закомментированный код не нужен. Если хочется иметь возможность к нему вернуться, сделайте тэг в репозитории :)
No description provided.