Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

2018-11-11-haskel-lab1

--- =============== Liste (de numere)
-- l = (2+3) * 4
-- l = head [5,7,77,63,5,3] -- selecteaza primul element din lista
-- 5
-- l = tail [5,7,77,63,5,3] -- sterge primul element si returneaza lista ramasa
-- [7,77,63,5,3]
-- l = tail ['d', 'a', 's'] -- sterge primul element si returneaza lista ramasa

-- l = tail [5] -- sterge primul element si returneaza lista ramasa
-- []
-- l = tail [] -- sterge primul element si returneaza lista ramasa
-- main.hs:22:8: error:

-- l = [5,7,77,63,5,3] !! 2 -- returneaza elementul de pe pozitia 2 (0-indexed)
-- 77 

-- l = take 3 [5,7,77,63,5,3] -- selecteaza primele n elemente din lista
-- [5,7,77]
-- l = take 3 [5,3] -- selecteaza primele n elemente din lista
-- [5,3]
-- l = drop 3 [5,7,77,63,5,3] -- sterge primele n elemente din lista si returneaza restul
-- [63,5,3]
-- l = length [5,7,77,63,5,3] -- lungimea listei
-- 6
-- l = sum [5,7,77,63,5,3] -- suma elementelor listei
-- 160
-- l = sum [5,7,77,63.45,5,3] -- suma elementelor listei
-- 160.45
-- l = product [5,7,77,63,5,3] -- produsul elementelor listei
-- 2546775
-- l = [5,7,77,63,5,3] ++ [4,5] -- concatenarea a 2 liste
-- [5,7,77,63,5,3,4,5]
-- l = reverse [5,7,77,63,5,3] -- produsul elementelor listei
-- [3,5,63,77,7,5]

-- execute (print)
-- main = print l

--- =============== Strings
-- main = putStrLn "hello world" -- afiseaza un string
-- hello world
-- l = "hello, Dolly!" -- afiseaza un string

-- execute (print)
-- main = putStrLn l


--- =============== Functions
-- Conventie: orice functie care se termina cu s inseamna 'plural', adica returneaza o lista

-- Algoritmul Quicksort:
-- f [] = []
-- f (x:xs) = f ys ++ [x] ++ f zs
--   where ys = [a | a <- xs, a <= x]
--         zs = [b | b <- xs, b > x]
-- -- atentie la copy/paste (din ide-uri online?!), cand: ys = [a | a ← xs, a ≤ x] trebuie corectat in: ys = [a | a <- xs, a <= x]
-- l = f [5,7,77,63,5,3]
-- [3,5,5,7,63,77]

-- aceeasi functie, scrisa mai explicit
quicksort [] = []
quicksort (x:xs) = quicksort small ++ (x : quicksort large)
    where small = [y | y <- xs, y <= x]
          large = [y | y <- xs, y > x]

-- l = quicksort [5,7,77,63,5,3]
-- [3,5,5,7,63,77]

double x = x + x
quadruple x = double (double x)
-- > quadruple 10
-- -- 40
-- > take (double 2) [1,2,3,4,5,6]
-- -- [1,2,3,4]

factorial n = product [1..n]
average ns = sum ns `div` length ns
-- div is enclosed in back quotes, not forward;
-- ❚ x `f` y is just syntactic sugar for f x y.
-- -- execute (print)
-- main = print l


--- =============== Import other files
-- import my_functions

-- l = average [5,7,77,63,5,3]

-- -- execute (print)
-- main = print l



--- =============== Typuri de date
-- l = not False
-- -- True

-- l = type False --- ?? nu functioneaza 

--- =============== Liste
-- l = [False, True, True] :: [Bool]
-- -- [False,True,True]
-- l = ['f', 'f', 'e'] :: [Char]
-- -- "ffe"
-- l = [['f'], ['f', 'e']] :: [[Char]]
-- -- ["f","fe"]

--- =============== Tuple - secvente de valori ce pot avea tipuri diferite, dar nr fix de parametri
-- l = (False, True, True) :: (Bool, Bool, Bool)
-- -- (False,True,True)
-- l = (False, 'a', True) :: (Bool, Char, Bool)
-- -- (False,'a',True)
-- l = ('a', (False, 'b')) :: (Char,(Bool,Char))
-- -- ('a',(False,'b'))
-- l = (True,['a','b']) :: (Bool,[Char])
-- -- (True,"ab")

--- =============== Tuple - secvente de valori ce pot avea tipuri diferite
-- -- add :: (Int, Int) -> Int -- ?? nu merge 
add x y = x + y
-- l = add 2 3
-- -- 5
zeroto :: Int -> [Int]
zeroto n = [0..n]
-- l = zeroto 10
-- -- [0,1,2,3,4,5,6,7,8,9,10]


--- =============== Conditional Expressions
abs_teo :: Int -> Int
abs_teo n = if n >= 0 then n else -n
-- l = abs_teo -7 -- Atentie: "l = abs_teo -7" interpreteaza gresit - ca operator, nu ca semn!
-- -- In an equation for ‘l’: l = abs_teo - 7
-- l = abs_teo (-7) 
-- -- 7

-- signum_teo :: Int -> Int
-- Ambiguous occurrence ‘signum’
--     It could refer to either ‘Prelude.signum’,
signum_teo :: Int -> Int
signum_teo n = if n < 0 then -1 else
              if n == 0 then 0 else 1
-- l = signum_teo (-7)
-- -- -1


--- =============== Guarded Equations - echivalentul instr switch
abs_teo_g n | n >= 0     = n
      | otherwise = -n
signum_teo_g n | n < 0     = -1
         | n == 0    = 0
         | otherwise = 1
-- l = signum_teo_g (-7)
-- -- -1

--- =============== pattern matching
not_teo :: Bool -> Bool
not_teo False = True
not_teo True = False
-- l = not_teo True
-- -- False
-- && nu poate fi redefinit, pentru ca e deja in 
(&&) :: Bool -> Bool -> Bool
True && True = True
True && False = False
False && True = False
False && False = False
-- l = False && True
-- -- Ambiguous occurrence ‘&&’ It could refer to either ‘Prelude.&&’, imported from ‘Prelude’ at main.hs:1:1 (and originally defined in ‘GHC.Classes’) or ‘Main.&&’, defined at main.hs:152:6
and_teo :: Bool -> Bool -> Bool
True `and_teo` True = True
True `and_teo` False = False
False `and_teo` True = False
False `and_teo` False = False
-- l = False `and_teo` True
-- -- False

--- =============== wildcard pattern
and_teo_compact :: Bool -> Bool -> Bool
True `and_teo_compact` True = True
_ `and_teo_compact` _ = False
-- l = False `and_teo_compact` True
-- -- -- False
and_teo_efficient :: Bool -> Bool -> Bool
True `and_teo_efficient` b = b
False `and_teo_efficient` _ = False
l = False `and_teo_efficient` True
-- -- -- False


main = print l




Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.