Advertisement
Guest User

IA_lab01

a guest
Oct 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.14 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2. // See the 'F# Tutorial' project for more help.
  3.  
  4. module Sandbox =
  5.  
  6.     let factorial n =
  7.         let rec fact n acc =
  8.             if n <= 1 then
  9.                 acc
  10.             else
  11.                 fact (n - 1) (acc * n)
  12.         fact n 1
  13.  
  14.     let mainSb() =
  15.         factorial 7
  16.  
  17. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. module Program1=
  19.     let read nr =
  20.         printf "Latura %d: > " nr
  21.         int(System.Console.ReadLine())
  22.  
  23.     let pozitiv x =
  24.         if x > 0 then
  25.             true
  26.         else
  27.             false
  28.  
  29.     let stri a b c =
  30.         (a + b > c)
  31.  
  32.     let validate a b c =
  33.         pozitiv a && pozitiv b && pozitiv c && stri a b c && stri b c a && stri c a b
  34.  
  35.     let mainP1() =
  36.         let a, b, c = read 1, read 2, read 3
  37.         if validate a b c then
  38.             printfn "Triunghi"
  39.         else
  40.             printfn "Nu e triunghi"
  41.  
  42. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  43.  
  44. module Program2 =
  45.  
  46.     let and' a b = a && b
  47.    let or' a b = a || b
  48.     let not' a = not a
  49.  
  50.    let xor' a b = and' (or' a b) (or' (not' a) (not' b))
  51.  
  52.    let f1 a b = and' a (or' a (not' b))
  53.     let f2 a b = xor' a b
  54.  
  55.    let printLine f a b =
  56.        printfn "%A\t%A\t%A" a b (f a b)
  57.  
  58.    let printTable f =
  59.        printLine f false false
  60.        printLine f false true
  61.        printLine f true false
  62.        printLine f true true
  63.  
  64.    let mainP2() =
  65.        printfn "a\tb\tf1 = a * (a + !b)"
  66.        printTable f1
  67.        printfn "\n"
  68.        printfn "a\tb\tf2 = a ^ b"
  69.        printTable f2
  70.        
  71. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  72. module Program3 =
  73.    open System
  74.    let sqrtRecursive x =
  75.        let rec sqrt' x b  =
  76.             if (b * b - x) > 1e-12 then
  77.                 sqrt' x (((x/b) + b)/2.0)
  78.            else
  79.                b
  80.        sqrt' x x
  81.  
  82.     let mainP3() =
  83.         printfn "Radical recursiv.\n\tIntroduceti numarul:"
  84.         let nr = float(System.Console.ReadLine())
  85.         printfn "sqrt (%A) = %A" nr (sqrtRecursive nr)
  86. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87.  
  88. module Program4 =
  89.     let rec guess nr x =
  90.         printfn "Ghiceste numarul [0, 100]; Incercari ramase: %A" x
  91.         if (x > 0) then
  92.             let myguess = int(System.Console.ReadLine())
  93.             if (myguess = nr) then
  94.                 printfn("Felicitari! Ai castigat!");
  95.             else
  96.                 guess nr (x-1)
  97.         else
  98.             printfn("Ai pierdut!")
  99.     let mainP4() =
  100.         let nr = (new System.Random()).Next(100)
  101.         printfn "%A" nr
  102.         guess nr 7
  103.  
  104. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  105.  
  106.  
  107. [<EntryPoint>]
  108. let main argv =
  109.     //printfn "%A" (Sandbox.mainSb())
  110.     printfn "- - - - Verificare triunghi - - - - "
  111.     Program1.mainP1()
  112.     printfn "- - - - Tabele adevar - - - - "
  113.     Program2.mainP2()
  114.     printfn "- - - - Radical - - - - "
  115.     Program3.mainP3()
  116.     printfn "- - - - Ghiceste numarul - - - - "
  117.     Program4.mainP4()
  118.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement