Homework 2 is ready#5
Conversation
yurii-litvinov
left a comment
There was a problem hiding this comment.
1 и 4 надо немного поправить, 2 и 3 зачтены
| // Использование рекурсии не допускается, зато нужен FsCheck для проверки функций | ||
| // на эквивалентность | ||
|
|
||
| /// <summary>Counts even numbers in the list using map.</summary> |
There was a problem hiding this comment.
Вообще, в 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.
Не-а, в пустом списке ровно 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)) |
| let infiniteSeqOfPrimes = | ||
| let rec isPrime n acc = | ||
| if n = 2 then true | ||
| elif float acc > sqrt (float n) then true |
|
|
||
| Seq.initInfinite(fun index -> | ||
| let n = index + 1 | ||
| findNPrime n 2) No newline at end of file |
There was a problem hiding this comment.
Да ну, слишком сложно. Можно сгенерить последовательность всех чисел вообще и выбрать (через Seq.filter) из них те, которые удовлетворяют предикату isPrime. Думайте в терминах ленивых вычислений :)
| // let myTree = Node(5, Node(2, Leaf, Leaf), Leaf) | ||
| // let sq x = x * x | ||
|
|
||
| // mapTree myTree sq |
There was a problem hiding this comment.
Закомментированный код нехорошо, да и вообще, есть тесты и 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) |
|
|
||
| [<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.
Мне кажется, проще через Seq.nth
| 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.
Зачем 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.
Что-то с пробелами беда. Это же не вызов функции filter от аргумента (fun x -> x % 2 = 0), это вызов от двух аргументов, первый из которых в скобках, чтобы приоритет операций ничего не сломал. Перед "(" надо пробел.
| Seq.initInfinite(fun index -> | ||
| let n = index + 1 | ||
| findNPrime n 2) | ||
| *) |
There was a problem hiding this comment.
Закомментированный код не нужен. Если хочется иметь возможность к нему вернуться, сделайте тэг в репозитории :)
No description provided.