Funkcionalno programiranje

1 / 14

# Funkcionalno programiranje - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Funkcionalno programiranje' - quincy

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Funkcionalno programiranje

Čas7:

Pitagorejske trojke

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.

pogrešno rešenje

pythagTriplesWrong= [ (x,y,z) |

x<-[2..],

y<-[x+1..],

z<-[y+1..],

x*x+y*y=z*z]

tačno rešenje

pythagTriples = [ (x,y,z) |

z<-[2..],

y<-[2..z-1],

x<-[2..y-1],

x*x+y*y=z*z]

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

Primer
• rand.hs
Podsećanje
• funkcija all

all :: (a → Bool ) → [a ] → Bool

all p = and ◦ map p

Npr all (>0) [1,2,3]

Karijeve funkcije:
• add :: (Int, Int) → Int
• add’ :: Int → (Int → Int)
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

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)

Funkcija zip:

zip :: [a ] → [b ] → [(a, b)]

Primer:

> zip [’a’, ’b’, ’c’] [1, 2, 3, 4]

[(’a’, 1), (’b’, 2), (’c’, 3)]

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]