Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Zaliczenie
- open System
- let numberOfCharsInString (letter: char, text: string) =
- let rec iterate (letter: char, text: string, i: int, number: int) =
- if text.Length > i then
- if text.[i] = letter then
- iterate(letter, text, i + 1, number + 1)
- else
- iterate(letter, text, i + 1, number)
- else
- number
- iterate(letter, text, 0, 0)
- let sumOfListItems (list: float list) =
- let rec sum (list: float list, actualSum: float, i: int) =
- if list.Length > i then
- sum(list, actualSum + list.[i], i + 1)
- else
- actualSum
- sum(list, 0.0, 0)
- let sumEven (lst: int list) =
- let rec sum(lst: int list, actualSum: int, index: int) =
- if index < lst.Length then
- if lst.[index] % 2 = 0 then
- sum(lst, actualSum + lst.[index], index + 1)
- else
- sum(lst, actualSum, index + 1)
- else
- actualSum
- sum(lst, 0, 0)
- let rec doubleEvenElements lst =
- match lst with
- | x::xs::xy ->
- x::xs::xs::(doubleEvenElements xy)
- | x::xs ->
- x::(doubleEvenElements xs)
- | [] ->
- lst
- let rec sumEvenWithMatch (lst: int list, sum: int) =
- match lst with
- | x::xs ->
- if x % 2 = 0 then sumEvenWithMatch (xs, sum + x)
- else sumEvenWithMatch (xs, sum)
- | [] -> sum
- let rec sortList lst: int list =
- match lst with
- | x::xs::xy ->
- if x < xs then
- xs::x::(sortList xy)
- else
- x::xs::(sortList xy)
- | x ->
- lst
- let rec howManyChars(c: char, text: char list, numberOfChars: int) =
- match text with
- | x::xs ->
- if x = c then
- howManyChars(c, xs, numberOfChars + 1)
- else
- howManyChars(c, xs, numberOfChars)
- | [] -> numberOfChars
- let howManyCharsInString (c: char, text: string) = howManyChars(c, text |> Seq.toList, 0)
- let rec sumFloatList (lst: float list) =
- match lst with
- | x::xs -> x + sumFloatList(xs)
- | [] -> 0.0
- let rec listWithoutEven(lst: int list) =
- match lst with
- | x::xs ->
- if x % 2 <> 0 then
- x :: (listWithoutEven xs)
- else
- listWithoutEven xs
- | [] -> lst
- let rec onlyEven(lst: int list) =
- match lst with
- | x::xs ->
- if x % 2 = 0 then
- x :: (onlyEven xs)
- else
- onlyEven xs
- | [] -> lst
- let sequenceToList param = param |> Seq.toList
- let sortDescending(lst) = List.sort lst
- let rec createList(x: int, n: int) =
- match n with
- | 0 -> []
- | 1 -> [x]
- | _ -> if n > 1 then x::(createList (x+1, n-1))
- else []
- let rec intersectionWithMatch (list1, list2) =
- let rec checkIfTheSame(element, list2) =
- match list2 with
- | [] -> false
- | head::tail -> if head = element then true
- else checkIfTheSame(element, tail)
- match list1 with
- | [] -> []
- | head::tail -> if checkIfTheSame(head, list2) = true then head::intersectionWithMatch(tail, list2)
- else intersectionWithMatch(tail, list2)
- let rec sumListItemsWithMatch (list) =
- match list with
- | [] -> 0
- | head::tail -> if head > 0 then head + sumListItemsWithMatch(tail)
- else sumListItemsWithMatch(tail)
- let rec howManyCharsInStringWithMatch (list: char list, character: char) =
- match list with
- | [] -> 0
- | head::tail -> if head.ToString().ToLower() = character.ToString().ToLower() then 1 + howManyCharsInStringWithMatch(tail, character)
- else howManyCharsInStringWithMatch(tail, character)
- let rec deleteEvenFromList list =
- match list with
- | [] -> []
- | head::tail -> if head % 2 = 0 then deleteEvenFromList tail
- else head :: deleteEvenFromList tail
- let rec generateFirstNumbers (start: int, howMany: int) =
- let rec checkIfFirst (number: int, index: int) =
- if index <= number then
- if number % index = 0 then
- 1 + checkIfFirst(number, index + 1)
- else
- checkIfFirst(number, index + 1)
- else
- 0
- match howMany with
- | 0 -> []
- | _ ->
- let isFirst = checkIfFirst (start, 1)
- if isFirst > 2 then
- generateFirstNumbers(start + 1, howMany)
- else
- start::generateFirstNumbers(start + 1, howMany - 1)
- type Book = {
- Author: string;
- Name: string;
- ISBN: string
- }
- let rec mRn (m, n) =
- match (m, n) with
- | m, 0 -> 0
- | m, 1 -> m
- | m, n -> if n > 0 then
- m + mRn(m, n - 1)
- else
- -m + mRn(m, n + 1)
- let rec summarizeFloatList (list: float list) =
- List.sum(list)
- let rec sortThreeElements(list) =
- match list with
- | head1::head2::head3::tail ->
- if head1 < head2 then
- if head1 < head3 then
- if head3 < head2 then
- head1::head3::head2::(sortThreeElements tail)
- else
- head1::head2::head3::(sortThreeElements tail)
- else
- if head3 < head2 then
- head3::head1::head2::(sortThreeElements tail)
- else
- head2::head1::head3::(sortThreeElements tail)
- else
- if head2 < head3 then
- if head3 < head1 then
- head2::head3::head1::(sortThreeElements tail)
- else
- head2::head1::head3::(sortThreeElements tail)
- else
- if head3 < head1 then
- head3::head2::head1::(sortThreeElements tail)
- else
- head2::head1::head3::(sortThreeElements tail)
- | [] -> []
- let rec createListOfEvenInts(howMany: int, start: int) =
- match howMany with
- | 0 -> []
- | howMany when howMany > 0 ->
- if start % 2 = 0 then
- start::(createListOfEvenInts(howMany - 1, start + 2))
- else
- (start + 1)::(createListOfEvenInts(howMany - 1, start + 3))
- | howMany when howMany < 0 -> []
- let rec listLength(list: int list) =
- match list with
- | [] -> 0
- | head::tail ->
- 1 + (listLength(tail))
- let rec deleteSelectedElementsFromList(list, element) =
- match list with
- | [] -> []
- | head::tail ->
- if head = element then
- deleteSelectedElementsFromList(tail, element)
- else
- head::(deleteSelectedElementsFromList(tail, element))
- let rec deleteDuplicatesFromList(list) =
- match list with
- | [] -> [] // sytuacja, w której obecnie rozpatrywana lista nie zawiera żadnego elementu
- | head::tail -> // rozdziela aktualną listę na dwa elementy; head -> zawiera tylko jeden element - pierwszy z danej listy; tail -> zawiera pozostałą część listy
- if List.contains head tail then // następuje sprawdzenie, czy w dalszej części listy (tail) znajduje się obecnie rozpatrywany element (tail)
- deleteDuplicatesFromList(tail) // jeżeli tak, to przechodzimy do następnego wywołania naszej funkcji
- else
- head::(deleteDuplicatesFromList(tail)) // w przeciwnym przypadku - bierze obecnie rozpatrywany element i dodaje go do naszej listy wyjściowej
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement