Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Text.RegularExpressions
- let rec policz = function
- | [] -> 0
- | h::t -> 1 + policz t
- let policz2 lst =
- let rec policzr n = function
- | [] -> n
- | h::t -> policzr (n + 1) t
- policzr 0 lst
- let rec ntyEl n lst =
- match n, lst with
- | 0, h::t -> h
- | _, h::t -> ntyEl (n - 1) t
- | _, [] -> failwith "Lista za krótka"
- let maks lst =
- match lst with
- | [] -> failwith "Lista jest pusta"
- | h::t -> let rec subMaks el lst2 =
- match lst2 with
- | [] -> el
- | h::t -> let p = if el > h then el else h
- subMaks p t
- subMaks h t
- let rec mieszaj al bl =
- match al, bl with
- | [], _ -> bl
- | _, [] -> al
- | ah::at, bh::bt -> ah::bh::(mieszaj at bt)
- let rec czyWliscie a lst =
- match lst with
- | [] -> false
- | h::t -> if h = a then true else czyWliscie a t
- let rec czyWliscie2 a = function
- | [] -> false
- | h::t -> (h = a) || czyWliscie2 a t
- let rec sumuj1 = function
- | [] -> 0
- | h::t -> h + (sumuj1 t)
- let sumuj2 lst =
- let rec sumR s = function
- | [] -> s
- | h::t -> sumR (h + s) t
- sumR 0 lst
- let srednia1 lst =
- float(sumuj2 lst)/float(policz2 lst)
- let srednia2 lst =
- match lst with
- | [] -> failwith "Lista jest pusta"
- | _ -> let rec sumIzlicz su li = function
- | [] -> (float su)/(float li)
- | h::t -> sumIzlicz (su + h) (li + 1) t
- sumIzlicz 0 0 lst
- let rec usunElZlist el lst =
- match lst with
- | [] -> []
- | h::t -> if h = el then t else h::(usunElZlist el t)
- let rec usunWszyElZlist el lst =
- match lst with
- | [] -> []
- | h::t -> if h = el then (usunWszyElZlist el t) else h::(usunWszyElZlist el t)
- let rec unikatowe l =
- match l with
- | [] -> []
- | h::t -> h::(unikatowe (usunWszyElZlist h t))
- let rec czescWspolna_ a b =
- match a, b with
- | [], _ | _, [] -> []
- | ah::at, _ -> if czyWliscie ah b
- then ah::(czescWspolna_ at b)
- else czescWspolna_ at b
- let czescWspolna a b = unikatowe <| czescWspolna_ a b
- let rec podLista k n lst =
- match lst with
- | [] -> if k = 0 && n = 0 then [] else failwith "Lista jest za krótka"
- | h::t -> if k > 0 then podLista (k - 1) n t
- else if n = 0 then [] else h::podLista 0 (n - 1) t
- let rec zrobZera = function
- | [] -> []
- | h::t -> (0, h)::zrobZera t
- let rec dodajJeden el = function
- | [] -> failwithf "Element %A nie znaleziony" el
- | (n, k)::t -> if k = el then (n + 1, k)::t
- else (n, k)::(dodajJeden el t)
- let rec uruchomZliczanie lst z =
- match lst with
- | [] -> z
- | h::t -> let w = dodajJeden h z
- uruchomZliczanie t w
- let zliczWystapienia lst =
- let u = unikatowe lst
- let z = zrobZera u
- uruchomZliczanie lst z
- let czyLiczbaRzeczywista (s:string) =
- let r = Regex "^(\+|-)?\d+\.?(\d*)((E|e)(\+|-)?\d+)?$"
- // r.IsMatch (s)
- let m = r.Match(s)
- (m.Success, m.Value)
- let znajdzEmaile (s:string) =
- let r = Regex "(\w|\.|-|_)+@(\w|\.|-|_)+"
- r.Matches(s) |> Seq.cast |> List.ofSeq |> List.map (fun (m : Match) -> m.Value)
- let rec wyswietlPlik (p : System.IO.TextReader) =
- let line = p.ReadLine()
- if line <> null then printfn "%s" line ; wyswietlPlik p else p.Close()
- let listaPlikow (s:string) =
- System.IO.DirectoryInfo(s).GetFiles() |> List.ofArray
- let znajdzMaksRozmiar (p: System.IO.FileInfo list) =
- match p with
- | [] -> failwith "W katalogu nie ma plików"
- | h::t -> let rec znMaks (r: System.IO.FileInfo list) (t: System.IO.FileInfo) =
- match r with
- | [] -> t
- | a::b -> let f = if a.Length > t.Length then a else t in znMaks b f
- znMaks t h
- type EntryIni =
- | Comment of string
- | Section of string
- | Pair of (string * string)
- let classifyLine (s:string) =
- if System.String.IsNullOrWhiteSpace(s) then Comment s
- else let r = s.Trim()
- if r.StartsWith("[") then Section r
- elif r.StartsWith(";") then Comment s
- else let i = r.IndexOf('=')
- if i = -1 then failwith "Invalid INI format"
- else Pair (r.Substring(0, i), r.Substring(i + 1))
- let rec parseIniFile (p : System.IO.TextReader) =
- let line = p.ReadLine()
- if line <> null then classifyLine line :: parseIniFile p else p.Close(); []
- let rec zwrocdoNastSekcji (l : EntryIni list) =
- match l with
- | [] -> []
- | Section n :: t -> []
- | h::t -> h::zwrocdoNastSekcji t
- let rec znajdzSekcje (l : EntryIni list) (nazwa: string)=
- match l with
- | [] -> []
- | Section n::t when n = nazwa -> (Section n)::zwrocdoNastSekcji t
- | h::t -> znajdzSekcje t nazwa
- let rec podmienWartoscWSekcji (l: EntryIni list) (klucz:string) (wartosc:string) =
- match l with
- | [] -> failwithf "Nie znaleziono klucza %s" klucz
- | Pair (k, v)::t -> if k = klucz then Pair(k, wartosc)::t
- else Pair(k, v)::podmienWartoscWSekcji t klucz wartosc
- | h::t -> h::podmienWartoscWSekcji t klucz wartosc
- let rec usundoNastSekcji (l : EntryIni list) =
- match l with
- | [] -> []
- | Section n :: t -> l
- | h::t -> usundoNastSekcji t
- let rec zastapSekcje (l : EntryIni list) (nazwa: string) (nowa: EntryIni list) =
- match l with
- | [] -> []
- | Section n::t when n = nazwa -> nowa @ usundoNastSekcji t
- | h::t -> h::zastapSekcje t nazwa nowa
- let rec saveIniFIle (l:EntryIni list) (w : System.IO.TextWriter) =
- match l with
- | [] -> w.Close()
- | Comment c::t -> w.WriteLine(c) ; saveIniFIle t w
- | Section n::t -> w.WriteLine(n) ; saveIniFIle t w
- | Pair(k,v)::t -> w.WriteLine(sprintf "%s=%s" k v) ; saveIniFIle t w
- let updateIniFile (inputFileName:string) (section:string) (key:string) (value:string) (outputFileName : string) =
- let listaIni = parseIniFile (System.IO.File.OpenText(inputFileName))
- //printfn "%A" listaIni
- let sekcja = znajdzSekcje listaIni section
- //printfn "Znajdź sekcje: %A" <| sekcja
- let podmiana = podmienWartoscWSekcji sekcja key value
- //printfn "Podmień w sekcji: %A" <| podmiana
- let zamienione = zastapSekcje listaIni section podmiana
- //printfn "\n\n-----\nZamienione: %A" zamienione
- saveIniFIle zamienione (System.IO.File.CreateText(outputFileName))
- [<EntryPoint>]
- let main argv =
- (* let listaIni = parseIniFile (System.IO.File.OpenText("/Users/jan/zad6.ini"))
- printfn "%A" listaIni
- let sekcja = znajdzSekcje listaIni "[SERVER]"
- printfn "Znajdź sekcje: %A" <| sekcja
- let podmiana = podmienWartoscWSekcji sekcja "address" "192.168.1.1"
- printfn "Podmień w sekcji: %A" <| podmiana
- let zamienione = zastapSekcje listaIni "[SERVER]" podmiana
- printfn "\n\n-----\nZamienione: %A" zamienione
- saveIniFIle zamienione (System.IO.File.CreateText("/Users/jan/zad6a.ini")) *)
- updateIniFile "/Users/jan/zad6.ini" "[SERVER]" "address" "192.168.1.1" "/Users/jan/zad6a.ini"
- (* let we = ['a' .. 'd']
- printfn "%A" <| policz we
- printfn "%A" <| policz2 we
- printfn "nty 1: %A" <| ntyEl 1 we
- //printfn "nty 0: %A" <| ntyEl 0 []
- printfn "maks: %A" <| maks [3; 4; 2; 1; -4; 5; 3; 2; 1]
- printfn "mieszaj: %A" <| mieszaj [-3;
- -4;
- -2;] [1; 4; 5; 3; 2; 1]
- printfn "czyWliscie1: %A" <| czyWliscie 3 [1; 4; 5; 3; 2; 1]
- printfn "czyWliscie2: %A" <| czyWliscie2 3 [1; 4; 5; 3; 2; 1]
- printfn "usunElZlist: %A" <| (usunWszyElZlist 'b' <| we @ ['b'; 'a' ; 'e'] )
- printfn "unikatowe: %A" <| unikatowe [3; 4; 2; 1; -4; 5; 3; 2; 1]
- printfn "czescWspolna: %A" <| czescWspolna [3; 4; 2; 1; -4; 5; 3; 2; 1] [-4; -2; 1; 0; 8]
- printfn "podLista: %A" <| podLista 2 3 [3; 4; 2; 1; -4; 5; 3; 2; 1]
- printfn "zliczWystapienia: %A" <| zliczWystapienia ['b'; 'a' ; 'e'; 'b'; 'e'; 'e'; 'a'; 'a'] *)
- 0 // return an integer exit code
- (*
- let dane = ["1"; "2.0"; "-3"; "ala" ; "3e-1"; "0.e-1"; "000"; "abc123abc"]
- let wy = List.map czyLiczbaRzeczywista dane
- List.iter (fun (a,b) -> printfn "%s => %A" a b) <| List.zip dane wy
- printf "ZnajdzEmaile: %A" <| znajdzEmaile "asdhjjdj js@prz-rzeszow.pl asbda das daj . 2; @ adsda 2@
- bla sad asd jsad@prz.edu.pl"
- wyswietlPlik (System.IO.File.OpenText("/Users/jan/myports.txt"))
- printfn "Pliki:"
- listaPlikow "/Users/jan" |> List.map (fun (f:System.IO.FileInfo) -> f.Name) |> printfn "%A"
- printfn "Katalogi:"
- System.IO.DirectoryInfo("/Users/jan").GetDirectories() |> List.ofArray |> List.map (fun (f:System.IO.DirectoryInfo) -> f.Name) |> printfn "%A"
- printfn "Maksimum:"
- printfn "%A" <| (listaPlikow "/Users/jan" |> znajdzMaksRozmiar)
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement