Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 8.87 KB | None | 0 0
  1.  
  2. open System.Text.RegularExpressions
  3.  
  4. let rec policz = function
  5.     | [] -> 0
  6.     | h::t -> 1 + policz t
  7.  
  8. let policz2 lst =
  9.     let rec policzr n = function
  10.         | [] -> n
  11.         | h::t -> policzr (n + 1) t
  12.     policzr 0 lst
  13.  
  14. let rec ntyEl n lst =
  15.     match n, lst with
  16.     | 0, h::t -> h
  17.     | _, h::t -> ntyEl (n - 1) t
  18.     | _, [] -> failwith "Lista za krótka"
  19.  
  20. let maks lst =
  21.     match lst with
  22.     | [] -> failwith "Lista jest pusta"
  23.     | h::t -> let rec subMaks el lst2 =
  24.                   match lst2 with
  25.                   | [] -> el
  26.                   | h::t -> let p = if el > h then el else h
  27.                             subMaks p t
  28.               subMaks h t
  29.  
  30. let rec mieszaj al bl =
  31.     match al, bl with
  32.     | [], _ -> bl
  33.     | _, [] -> al
  34.     | ah::at, bh::bt -> ah::bh::(mieszaj at bt)
  35.  
  36. let rec czyWliscie a lst =
  37.     match lst with
  38.     | [] -> false
  39.     | h::t -> if h = a then true else czyWliscie a t
  40.  
  41. let rec czyWliscie2 a = function
  42.     | [] -> false
  43.     | h::t -> (h = a) || czyWliscie2 a t
  44.  
  45. let rec sumuj1 = function
  46.     | [] -> 0
  47.     | h::t -> h + (sumuj1 t)
  48.  
  49. let sumuj2 lst =
  50.     let rec sumR s = function
  51.         | [] -> s
  52.         | h::t -> sumR (h + s) t
  53.     sumR 0 lst
  54.  
  55. let srednia1 lst =
  56.     float(sumuj2 lst)/float(policz2 lst)
  57.  
  58. let srednia2 lst =
  59.     match lst with
  60.     | [] -> failwith "Lista jest pusta"
  61.     | _ -> let rec sumIzlicz su li = function
  62.                | [] -> (float su)/(float li)
  63.                | h::t -> sumIzlicz (su + h) (li + 1) t
  64.            sumIzlicz 0 0 lst
  65.  
  66. let rec usunElZlist el lst =
  67.     match lst with
  68.     | [] -> []
  69.     | h::t -> if h = el then t else h::(usunElZlist el t)
  70.  
  71. let rec usunWszyElZlist el lst =
  72.     match lst with
  73.     | [] -> []
  74.     | h::t -> if h = el then (usunWszyElZlist el t) else h::(usunWszyElZlist el t)
  75.  
  76. let rec unikatowe l =
  77.     match l with
  78.     | [] -> []
  79.     | h::t -> h::(unikatowe (usunWszyElZlist h t))
  80.  
  81. let rec czescWspolna_ a b =
  82.     match a, b with
  83.     | [], _ | _, [] -> []
  84.     | ah::at, _ -> if czyWliscie ah b
  85.                       then ah::(czescWspolna_ at b)
  86.                       else czescWspolna_ at b
  87.  
  88. let czescWspolna a b = unikatowe <| czescWspolna_ a b
  89.  
  90. let rec podLista k n lst =
  91.     match lst with
  92.     | [] -> if k = 0 && n = 0 then [] else failwith "Lista jest za krótka"
  93.     | h::t -> if k > 0 then podLista (k - 1) n t
  94.               else if n = 0 then [] else h::podLista 0 (n - 1) t
  95.  
  96. let rec zrobZera = function
  97.     | [] -> []
  98.     | h::t -> (0, h)::zrobZera t
  99.  
  100. let rec dodajJeden el = function
  101.     | [] -> failwithf "Element %A nie znaleziony" el
  102.     | (n, k)::t -> if k = el then (n + 1, k)::t
  103.                    else (n, k)::(dodajJeden el t)
  104.  
  105. let rec uruchomZliczanie lst z =
  106.     match lst with
  107.     | [] -> z
  108.     | h::t -> let w = dodajJeden h z
  109.               uruchomZliczanie t w
  110.  
  111. let zliczWystapienia lst =
  112.     let u = unikatowe lst
  113.     let z = zrobZera u
  114.     uruchomZliczanie lst z
  115.  
  116. let czyLiczbaRzeczywista (s:string) =
  117.     let r = Regex "^(\+|-)?\d+\.?(\d*)((E|e)(\+|-)?\d+)?$"
  118. //    r.IsMatch (s)
  119.     let m = r.Match(s)
  120.     (m.Success, m.Value)
  121.  
  122. let znajdzEmaile (s:string) =
  123.     let r = Regex "(\w|\.|-|_)+@(\w|\.|-|_)+"
  124.     r.Matches(s) |> Seq.cast |> List.ofSeq |> List.map (fun (m : Match) -> m.Value)
  125.  
  126. let rec wyswietlPlik (p : System.IO.TextReader) =
  127.     let line = p.ReadLine()
  128.     if line <> null then printfn "%s" line ; wyswietlPlik p else p.Close()
  129.  
  130. let listaPlikow (s:string) =
  131.     System.IO.DirectoryInfo(s).GetFiles() |> List.ofArray
  132.  
  133. let znajdzMaksRozmiar (p: System.IO.FileInfo list) =
  134.     match p with
  135.     | [] -> failwith "W katalogu nie ma plików"
  136.     | h::t -> let rec znMaks (r: System.IO.FileInfo list) (t: System.IO.FileInfo) =
  137.                   match r with
  138.                   | [] -> t
  139.                   | a::b -> let f = if a.Length > t.Length then a else t in znMaks b f
  140.               znMaks t h
  141.  
  142. type EntryIni =
  143.     | Comment of string
  144.     | Section of string
  145.     | Pair of (string * string)
  146.  
  147. let classifyLine (s:string) =
  148.     if System.String.IsNullOrWhiteSpace(s) then Comment s
  149.     else let r = s.Trim()
  150.          if r.StartsWith("[") then Section r
  151.          elif r.StartsWith(";") then Comment s
  152.          else let i = r.IndexOf('=')
  153.               if i = -1 then failwith "Invalid INI format"
  154.               else Pair (r.Substring(0, i), r.Substring(i + 1))
  155.  
  156. let rec parseIniFile (p : System.IO.TextReader) =
  157.     let line = p.ReadLine()
  158.     if line <> null then classifyLine line :: parseIniFile p else p.Close(); []
  159.  
  160. let rec zwrocdoNastSekcji (l : EntryIni list) =
  161.     match l with
  162.     | [] -> []
  163.     | Section n :: t -> []
  164.     | h::t -> h::zwrocdoNastSekcji t
  165.  
  166. let rec znajdzSekcje (l : EntryIni list) (nazwa: string)=
  167.     match l with
  168.     | [] -> []
  169.     | Section n::t  when n = nazwa -> (Section n)::zwrocdoNastSekcji t
  170.     | h::t -> znajdzSekcje t nazwa
  171.  
  172. let rec podmienWartoscWSekcji (l: EntryIni list) (klucz:string) (wartosc:string) =
  173.     match l with
  174.     | [] -> failwithf "Nie znaleziono klucza %s" klucz
  175.     | Pair (k, v)::t -> if k = klucz then Pair(k, wartosc)::t
  176.                         else Pair(k, v)::podmienWartoscWSekcji t klucz wartosc
  177.     | h::t -> h::podmienWartoscWSekcji t klucz wartosc
  178.  
  179. let rec usundoNastSekcji (l : EntryIni list) =
  180.     match l with
  181.     | [] -> []
  182.     | Section n :: t -> l
  183.     | h::t -> usundoNastSekcji t
  184.  
  185.  
  186. let rec zastapSekcje (l : EntryIni list) (nazwa: string) (nowa: EntryIni list) =
  187.     match l with
  188.     | [] -> []
  189.     | Section n::t  when n = nazwa -> nowa @ usundoNastSekcji t
  190.     | h::t -> h::zastapSekcje t nazwa nowa
  191.  
  192. let rec saveIniFIle (l:EntryIni list) (w : System.IO.TextWriter) =
  193.     match l with
  194.     | [] -> w.Close()
  195.     | Comment c::t -> w.WriteLine(c) ; saveIniFIle t w
  196.     | Section n::t -> w.WriteLine(n) ; saveIniFIle t w
  197.     | Pair(k,v)::t -> w.WriteLine(sprintf "%s=%s" k v) ; saveIniFIle t w
  198.  
  199. let updateIniFile (inputFileName:string) (section:string) (key:string) (value:string) (outputFileName : string) =
  200.     let listaIni = parseIniFile (System.IO.File.OpenText(inputFileName))
  201.     //printfn "%A" listaIni
  202.     let sekcja = znajdzSekcje listaIni section
  203.     //printfn "Znajdź sekcje: %A" <| sekcja
  204.     let podmiana = podmienWartoscWSekcji sekcja key value
  205.     //printfn "Podmień w sekcji: %A" <| podmiana
  206.     let zamienione = zastapSekcje listaIni section podmiana
  207.     //printfn "\n\n-----\nZamienione: %A" zamienione
  208.     saveIniFIle zamienione (System.IO.File.CreateText(outputFileName))
  209.  
  210. [<EntryPoint>]
  211. let main argv =
  212.     (* let listaIni = parseIniFile (System.IO.File.OpenText("/Users/jan/zad6.ini"))
  213.     printfn "%A" listaIni
  214.     let sekcja = znajdzSekcje listaIni "[SERVER]"
  215.     printfn "Znajdź sekcje: %A" <| sekcja
  216.     let podmiana = podmienWartoscWSekcji sekcja "address" "192.168.1.1"
  217.     printfn "Podmień w sekcji: %A" <| podmiana
  218.     let zamienione = zastapSekcje listaIni "[SERVER]" podmiana
  219.     printfn "\n\n-----\nZamienione: %A" zamienione
  220.     saveIniFIle zamienione (System.IO.File.CreateText("/Users/jan/zad6a.ini")) *)
  221.  
  222.     updateIniFile "/Users/jan/zad6.ini" "[SERVER]" "address" "192.168.1.1" "/Users/jan/zad6a.ini"
  223.     (* let we = ['a' .. 'd']
  224.     printfn "%A" <| policz we
  225.     printfn "%A" <| policz2 we
  226.     printfn "nty 1: %A" <| ntyEl 1 we
  227.     //printfn "nty 0: %A" <| ntyEl 0 []
  228.     printfn "maks: %A" <| maks [3; 4; 2; 1; -4; 5; 3;  2; 1]
  229.     printfn "mieszaj: %A" <| mieszaj [-3;
  230.                                       -4;
  231.                                       -2;] [1; 4; 5; 3;  2; 1]
  232.     printfn "czyWliscie1: %A" <| czyWliscie 3 [1; 4; 5; 3;  2; 1]
  233.     printfn "czyWliscie2: %A" <| czyWliscie2 3 [1; 4; 5; 3;  2; 1]
  234.     printfn "usunElZlist: %A" <| (usunWszyElZlist 'b' <| we @ ['b'; 'a' ; 'e'] )
  235.     printfn "unikatowe: %A" <| unikatowe [3; 4; 2; 1; -4; 5; 3;  2; 1]
  236.     printfn "czescWspolna: %A" <| czescWspolna [3; 4; 2; 1; -4; 5; 3; 2; 1] [-4; -2; 1; 0; 8]
  237.     printfn "podLista: %A" <| podLista 2 3 [3; 4; 2; 1; -4; 5; 3;  2; 1]
  238.     printfn "zliczWystapienia: %A" <| zliczWystapienia ['b'; 'a' ; 'e'; 'b'; 'e'; 'e'; 'a'; 'a'] *)
  239.     0 // return an integer exit code
  240.  
  241. (*
  242.     let dane = ["1"; "2.0"; "-3"; "ala" ; "3e-1"; "0.e-1"; "000"; "abc123abc"]
  243.     let wy = List.map czyLiczbaRzeczywista dane
  244.     List.iter (fun (a,b) -> printfn "%s => %A" a b) <| List.zip dane wy
  245.     printf "ZnajdzEmaile: %A" <| znajdzEmaile "asdhjjdj js@prz-rzeszow.pl asbda das daj . 2; @ adsda 2@
  246.     bla sad asd jsad@prz.edu.pl"
  247.  
  248.     wyswietlPlik (System.IO.File.OpenText("/Users/jan/myports.txt"))
  249.     printfn "Pliki:"
  250.     listaPlikow "/Users/jan" |> List.map (fun (f:System.IO.FileInfo) -> f.Name) |> printfn "%A"
  251.     printfn "Katalogi:"
  252.     System.IO.DirectoryInfo("/Users/jan").GetDirectories() |> List.ofArray |> List.map (fun (f:System.IO.DirectoryInfo) -> f.Name) |> printfn "%A"
  253.     printfn "Maksimum:"
  254.     printfn "%A" <| (listaPlikow "/Users/jan" |> znajdzMaksRozmiar)
  255.     *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement