Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 7.48 KB | None | 0 0
  1. module Zaliczenie
  2.  
  3. open System
  4.  
  5. let numberOfCharsInString (letter: char, text: string) =
  6.     let rec iterate (letter: char, text: string, i: int, number: int) =
  7.         if text.Length > i then
  8.             if text.[i] = letter then
  9.                 iterate(letter, text, i + 1, number + 1)
  10.             else
  11.                 iterate(letter, text, i + 1, number)
  12.            
  13.         else
  14.             number
  15.     iterate(letter, text, 0, 0)
  16.  
  17. let sumOfListItems (list: float list) =
  18.     let rec sum (list: float list, actualSum: float, i: int) =
  19.         if list.Length > i then
  20.             sum(list, actualSum + list.[i], i + 1)
  21.         else
  22.             actualSum
  23.     sum(list, 0.0, 0)
  24.  
  25. let sumEven (lst: int list) =
  26.     let rec sum(lst: int list, actualSum: int, index: int) =
  27.         if index < lst.Length then
  28.             if lst.[index] % 2 = 0 then
  29.                 sum(lst, actualSum + lst.[index], index + 1)
  30.             else
  31.                 sum(lst, actualSum, index + 1)
  32.         else
  33.             actualSum
  34.     sum(lst, 0, 0)
  35.    
  36. let rec doubleEvenElements lst =
  37.     match lst with
  38.     | x::xs::xy ->
  39.         x::xs::xs::(doubleEvenElements xy)
  40.     | x::xs ->
  41.         x::(doubleEvenElements xs)
  42.     | [] ->
  43.         lst
  44.  
  45. let rec sumEvenWithMatch (lst: int list, sum: int) =
  46.     match lst with
  47.     | x::xs ->
  48.         if x % 2 = 0 then sumEvenWithMatch (xs, sum + x)
  49.         else sumEvenWithMatch (xs, sum)
  50.     | [] -> sum
  51.  
  52. let rec sortList lst: int list =
  53.     match lst with
  54.     | x::xs::xy ->
  55.         if x < xs then
  56.             xs::x::(sortList xy)
  57.         else
  58.             x::xs::(sortList xy)
  59.     | x ->
  60.         lst
  61.  
  62. let rec howManyChars(c: char, text: char list, numberOfChars: int) =
  63.     match text with
  64.     | x::xs ->
  65.         if x = c then
  66.             howManyChars(c, xs, numberOfChars + 1)
  67.         else
  68.             howManyChars(c, xs, numberOfChars)
  69.     | [] -> numberOfChars
  70.            
  71.  
  72. let howManyCharsInString (c: char, text: string) = howManyChars(c, text |> Seq.toList, 0)
  73.  
  74. let rec sumFloatList (lst: float list) =
  75.     match lst with
  76.     | x::xs -> x + sumFloatList(xs)
  77.     | [] -> 0.0
  78.  
  79. let rec listWithoutEven(lst: int list) =
  80.     match lst with
  81.     | x::xs ->
  82.         if x % 2 <> 0 then
  83.             x :: (listWithoutEven xs)
  84.         else
  85.             listWithoutEven xs
  86.     | [] -> lst
  87.  
  88. let rec onlyEven(lst: int list) =
  89.     match lst with
  90.     | x::xs ->
  91.         if x % 2 = 0 then
  92.             x :: (onlyEven xs)
  93.         else
  94.             onlyEven xs
  95.     | [] -> lst
  96.  
  97. let sequenceToList param = param |> Seq.toList
  98.  
  99. let sortDescending(lst) = List.sort lst
  100.  
  101. let rec createList(x: int, n: int) =
  102.     match n with
  103.     | 0 -> []
  104.     | 1 -> [x]
  105.     | _ -> if n > 1 then x::(createList (x+1, n-1))
  106.             else []
  107.  
  108. let rec intersectionWithMatch (list1, list2) =
  109.     let rec checkIfTheSame(element, list2) =
  110.         match list2 with
  111.         | [] -> false
  112.         | head::tail -> if head = element then true
  113.                         else checkIfTheSame(element, tail)
  114.     match list1 with
  115.     | [] -> []
  116.     | head::tail ->  if checkIfTheSame(head, list2) = true then head::intersectionWithMatch(tail, list2)
  117.                      else intersectionWithMatch(tail, list2)
  118.  
  119. let rec sumListItemsWithMatch (list) =
  120.     match list with
  121.     | [] -> 0
  122.     | head::tail -> if head > 0 then head + sumListItemsWithMatch(tail)
  123.                     else sumListItemsWithMatch(tail)
  124.  
  125. let rec howManyCharsInStringWithMatch (list: char list, character: char) =
  126.     match list with
  127.     | [] -> 0
  128.     | head::tail -> if head.ToString().ToLower() = character.ToString().ToLower() then 1 + howManyCharsInStringWithMatch(tail, character)
  129.                     else howManyCharsInStringWithMatch(tail, character)
  130.  
  131. let rec deleteEvenFromList list =
  132.     match list with
  133.     | [] -> []
  134.     | head::tail -> if head % 2 = 0 then deleteEvenFromList tail
  135.                     else head :: deleteEvenFromList tail
  136.  
  137. let rec generateFirstNumbers (start: int, howMany: int) =
  138.     let rec checkIfFirst (number: int, index: int) =
  139.         if index <= number then
  140.             if number % index = 0 then
  141.                 1 + checkIfFirst(number, index + 1)
  142.             else
  143.                 checkIfFirst(number, index + 1)
  144.         else
  145.             0
  146.     match howMany with
  147.     | 0 -> []
  148.     | _ ->
  149.         let isFirst = checkIfFirst (start, 1)
  150.         if  isFirst > 2 then                
  151.                 generateFirstNumbers(start + 1, howMany)
  152.            else
  153.             start::generateFirstNumbers(start + 1, howMany - 1)
  154.  
  155. type Book = {
  156.     Author: string;
  157.     Name: string;
  158.     ISBN: string
  159. }
  160.  
  161. let rec mRn (m, n) =
  162.     match (m, n) with
  163.     | m, 0 -> 0
  164.     | m, 1 -> m
  165.     | m, n -> if n > 0 then
  166.                     m + mRn(m, n - 1)
  167.               else
  168.                     -m + mRn(m, n + 1)
  169.  
  170. let rec summarizeFloatList (list: float list) =
  171.     List.sum(list)
  172.  
  173. let rec sortThreeElements(list) =
  174.     match list with
  175.     | head1::head2::head3::tail ->
  176.         if head1 < head2 then
  177.             if head1 < head3 then
  178.                 if head3 < head2 then
  179.                     head1::head3::head2::(sortThreeElements tail)
  180.                 else
  181.                     head1::head2::head3::(sortThreeElements tail)
  182.             else
  183.                 if head3 < head2 then
  184.                     head3::head1::head2::(sortThreeElements tail)
  185.                 else
  186.                     head2::head1::head3::(sortThreeElements tail)
  187.         else
  188.             if head2 < head3 then
  189.                 if head3 < head1 then
  190.                     head2::head3::head1::(sortThreeElements tail)
  191.                 else
  192.                     head2::head1::head3::(sortThreeElements tail)
  193.             else
  194.                 if head3 < head1 then
  195.                     head3::head2::head1::(sortThreeElements tail)
  196.                 else
  197.                     head2::head1::head3::(sortThreeElements tail)
  198.     | [] -> []
  199.  
  200. let rec createListOfEvenInts(howMany: int, start: int) =
  201.     match howMany with
  202.     | 0 -> []
  203.     | howMany when howMany > 0 ->
  204.         if start % 2 = 0 then
  205.             start::(createListOfEvenInts(howMany - 1, start + 2))
  206.         else
  207.             (start + 1)::(createListOfEvenInts(howMany - 1, start + 3))
  208.     | howMany when howMany < 0 -> []
  209.  
  210. let rec listLength(list: int list) =
  211.     match list with
  212.     | [] -> 0
  213.     | head::tail ->
  214.         1 + (listLength(tail))
  215.  
  216. let rec deleteSelectedElementsFromList(list, element) =
  217.     match list with
  218.     | [] -> []
  219.     | head::tail ->
  220.         if head = element then
  221.             deleteSelectedElementsFromList(tail, element)
  222.         else
  223.             head::(deleteSelectedElementsFromList(tail, element))
  224.  
  225. let rec deleteDuplicatesFromList(list) =
  226.     match list with
  227.     | [] -> []              // sytuacja, w której obecnie rozpatrywana lista nie zawiera żadnego elementu
  228.     | head::tail ->         // rozdziela aktualną listę na dwa elementy; head -> zawiera tylko jeden element - pierwszy z danej listy; tail -> zawiera pozostałą część listy
  229.         if List.contains head tail then        // następuje sprawdzenie, czy w dalszej części listy (tail) znajduje się obecnie rozpatrywany element (tail)
  230.             deleteDuplicatesFromList(tail)      // jeżeli tak, to przechodzimy do następnego wywołania naszej funkcji
  231.         else
  232.             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