Difference between revisions of "Care sunt structurile de control in Haskell ?"
Line 10: | Line 10: | ||
Exemple clasice de programare cu actiuni (monadice) repetate: |
Exemple clasice de programare cu actiuni (monadice) repetate: |
||
− | myforever :: IO () -> IO () |
+ | myforever :: IO () -> IO () <br> |
− | myforever action = action >> myforever action |
+ | myforever action = action >> myforever action <br> |
+ | <br> |
||
− | |||
− | myrepeat Int -> IO a -> IO () |
+ | myrepeat :: Int -> IO a -> IO () <br> |
− | myrepeat 0 act = return () |
+ | myrepeat 0 act = return () <br> |
− | myrepeat n act = action >> myrepeat (n-1) act |
+ | myrepeat n act = action >> myrepeat (n-1) act <br> |
+ | <br> |
||
Dupa "Tackling the Awkward Squad:..." de Simon Peyton Jones. Neaparat de citit cap 2.4 - Control structures din ea. Apoi 2.5. |
Dupa "Tackling the Awkward Squad:..." de Simon Peyton Jones. Neaparat de citit cap 2.4 - Control structures din ea. Apoi 2.5. |
Revision as of 08:31, 8 June 2007
Haskell fiind limbaj functional cu "lazy evaluation" nu are structuri de control in sensul din limbajele imperative. Dar le puteti inventa si folosi pe cele dorite de dumneavoastra ! Flexibilitate maxima.
In Haskell if then else este un operator conditional
if <exp1> then <exp2> else <exp3>
avand mai curand sensul din C al operatorului ' ? : '
Ceea ce va lipseste atunci cand programati in stil imperativ in Haskell poate fi,deci,adaugat. Repet: Haskell permite sa va definiti propriile structuri de control pentru programarea "imperativa" dar (sunt mai usor de facut) fara variabile locale in vre-un context. Deci puteti defini imediat un for care cicleaza parcurgand valorile dintr-o lista si e ceva mai greu de facut un for care modifica o variabila dintr-un context. Exemple clasice de programare cu actiuni (monadice) repetate:
myforever :: IO () -> IO ()
myforever action = action >> myforever action
myrepeat :: Int -> IO a -> IO ()
myrepeat 0 act = return ()
myrepeat n act = action >> myrepeat (n-1) act
Dupa "Tackling the Awkward Squad:..." de Simon Peyton Jones. Neaparat de citit cap 2.4 - Control structures din ea. Apoi 2.5.