// eloadason tesztelt fuggvenyek // A fuggvenyek kisebb hibakat tartalmazhatnak, azokat nektek kell kijvitani module eloadasok import StdEnv //Start = sqrt (6.0*pi_26) // where (pi_26,_) = pi26 pi26 :: (Real, Int) pi26 = until goodEnough improve (0.0, 1) goodEnough:: (Real, Int) -> Bool goodEnough (_, k) = k>10000 improve:: (Real, Int) -> (Real, Int) improve (s, k) = (s + 1.0/(k2),k+1) where k2 = toReal (k*k) // a modulbol hianyzik a Start= kifejezes // a peldaprogramok elott ott van, ki kell kommentelni Start = [{sz= ~1 , nev=2},{sz= ~1 , nev=3} .. {sz= 5 , nev=2}] // Start = {sz= 1 , nev=2} + {sz= ~1 , nev=2} :: Q = {sz:: Int, nev::Int} simplify {sz=p,nev=q} | q==0 = abort "zeroval valo osztas" | q <0 = { sz= ~p/r, nev= ~q/r} = { sz=p/r, nev=q/r} where r = gcd p q instance one Q where one = {sz=1, nev=1} instance zero Q where zero = {sz=0, nev=1} instance < Q where (<) {sz=sz1, nev=nev1} {sz=sz2, nev=nev2} = sz1*nev2 < sz2*nev1 instance + Q where (+) {sz=sz1, nev=nev1} {sz=sz2, nev=nev2} = simplify {sz=sz_o, nev=n_o} where sz_o = sz1*nev2 + sz2*nev1 n_o = nev1 *nev2 instance - Q where (-) {sz=sz1, nev=nev1} {sz=sz2, nev=nev2} = simplify {sz=sz_k, nev=n_k} where sz_k = sz1*nev2 - sz2*nev1 n_k = nev1 *nev2 // Start = mySqrt 3.0 mySqrt x = until goodEnough improve 1.0 where goodEnough y = (y*y) ~=~ x improve y = 0.5*(y + x/y) (~=~) z1 z2 = abs(z1-z2)<0.00000001 // Start = my_max [2.0; 1.0, 2.0, 4.0, 5.0, 12.0] my_max :: [a] -> a | < a my_max [x] = x my_max [fl:ml] | fl > mml = fl = mml where mml = my_max ml // Start = take 30 evens // pelda a generikus definiciokra es a filter fuggvenyre evens = filter even [1..] even :: Int -> Bool even x = ((x mod 2) == 0) // a MOD operator feluldefinialasa az INT osztalyra. instance mod Int where (mod) a b | (b>0) && (a < b) && (a>=0) = a | (b>0) && (a > 0) = (a-b) mod b | (b>0) && (a < 0) = (a+b) mod b // a termeszetes logaritmus, az E szam kiszamitasa nat_base:: Real nat_base = nat_calc 1.0 2 1.0 nat_calc:: Real Int Real -> Real nat_calc 0.0 _ x = x nat_calc a n x = nat_calc (a/(toReal n)) (n+1) (x+a) // LISTAKKAL torteno muveletek // lista fej-maradek elvalasztasa // Start = [1,2:[3]] ( =[1:[2,3]] ) // generikus halmazjellemzes, az osszes negyzetszam halmazanak az elemei a // 23.tol a 26.ig // Start = [x*x \\ x<-[1..] ]@(23,26) // uj infix index-muvelet definialasa // Start = [0,1,2,3,4]@(3,5) (@):: [a] (Int,Int) -> [a] (@) list (frm,to) = take (to-frm+1) (drop frm list) // pi^2/6 kiszamitasa // pelda az UNTIL mukodesenek a bemutatasara pi2per6:: Real pi2per6 = x where (x,y) = until goodEnough improve (0.0,1) where goodEnough::(Real, Int) -> Bool goodEnough (_,k) = 1.0/(toReal (k*k)) < 0.00000000001 improve:: (Real, Int) -> (Real, Int) improve (x,k) = (x1, k+1) where x1 = x + 1.0/(toReal(k*k))