1 / 14

Funkcionalno programiranje

Funkcionalno programiranje. Čas7: Rad sa beskonačnim listama Primeri rada sa matricama. Pitagorejske trojke. Zadatak:

quincy
Télécharger la présentation

Funkcionalno programiranje

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Funkcionalno programiranje Čas7: Rad sa beskonačnim listama Primeri rada sa matricama

  2. Pitagorejske trojke • Zadatak: Pitagorejska trojkaje svaka trojka prirodnih brojeva (x,y,z) koja ispunjava uslov x*x+y*y=z*z. Napisati funkciju pythagTripleskoja generiše listu svih pitagorejskih trojki.

  3. pogrešno rešenje pythagTriplesWrong= [ (x,y,z) | x<-[2..], y<-[x+1..], z<-[y+1..], x*x+y*y=z*z]

  4. tačno rešenje pythagTriples = [ (x,y,z) | z<-[2..], y<-[2..z-1], x<-[2..y-1], x*x+y*y=z*z]

  5. Generisanje slučajnih brojeva • Linearni kongruentni metod: Xn+1 = (aXn+c) mod m m>0 - modulus 0<a<m – multiplier 0<=c <m – increment 0<=Xo<=m – seed

  6. Primer • rand.hs

  7. Podsećanje • funkcija all all :: (a → Bool ) → [a ] → Bool all p = and ◦ map p Npr all (>0) [1,2,3]

  8. Karijeve funkcije: • add :: (Int, Int) → Int • add’ :: Int → (Int → Int)

  9. funkcije curry: • Curry pretvara funkciju nad parovima u Karijevu funkciju: curry :: ((a, b) → c) → (a → b → c) curry f = λx y → f (x , y) Primer: curry fst 1 2

  10. funkcija uncurry: • Pretvara Karijevu funkciju u funkciju nad parovima: uncurry :: (a → b → c) → ((a, b) → c) uncurry f = λ(x , y) → f x y Npr. uncurry (+) (1,2)

  11. Funkcija zip: zip :: [a ] → [b ] → [(a, b)] Primer: > zip [’a’, ’b’, ’c’] [1, 2, 3, 4] [(’a’, 1), (’b’, 2), (’c’, 3)]

  12. Funkcija zipWith zipWith f [] _ = [] zipWith f _ [] = [] zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys Primer: zipWith (*) [1,2] [3,4]

  13. Rad sa matricama • type Matrix = [[Int]] • zadaci06.txt

  14. Rad sa matricama • primer: inverse.hs

More Related