L1 Haskell

Published on January 2017 | Categories: Documents | Downloads: 39 | Comments: 0 | Views: 323
of 1
Download PDF   Embed   Report

Comments

Content

module L1 where
-- 1. Scrieti o functie care rezolva ecuatia de gradul 2 (folositi error pentru
Delta<0)
-- TODO
-- 2. Definiti functia reverse pe liste.
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = (reverse' $ xs) ++ [x]
-- 3. Scrieti o functie care verifica daca un sir e palindrom
isPalindrome :: Eq a => [a] -> Bool
isPalindrome [] = True
isPalindrome [x] = True
isPalindrome (x:xs) = (x == last xs) && isPalindrome(init xs)
-- 4.
e are
zip3'
zip3'
zip3'

Scrieti un exemplu de folosire a functiei zip. Scrieti o functie zip3 car
ca argumente 3 liste.
:: [a] -> [b] -> [c] -> [(a, b, c)]
(a:aas) (b:bbs) (c:ccs) = (a, b, c) : zip3' aas bbs ccs
_
_
_
= []

zip3a' :: [a] -> [b] -> [c] -> [(a, b, c)]
zip3a' aas bbs ccs = [ (a, b, c) | a <- aas, b <- bbs, c <- ccs]
-- 5. Scrieti in doua moduri o functie map2 care are ca argument o functie cu do
ua argumente f si doua liste [x1,x2 ], [y1,y2,..] si intoarce lista [f(x1,y1), f
(x2,y2),..].
map2 :: (a -> b) -> [a] -> [a] -> [(b, b)]
map2 f (a:aas) (b:bbs) = (f a, f b) : map2 f aas bbs
map2 f _
_
= []
-- 6. Scrieti o functie fn care are ca argument o functie f si un numar n iar c
a rezultat fn(x)=f(f(..(f(x)))) (de n ori)
fn :: (Num b, Eq b) => (a -> a) -> b -> a -> a
fn f 1 x = f x
fn f n x = f $ fn f (n - 1) x
-- 7. Folosind ciurul lui Eratostene creati o lista infinita care contine numere
le prime.
primes = sieve [2..]
where sieve (p:xs) =
p : sieve [x | x <- xs, x `mod` p /= 0]

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close