0% found this document useful (0 votes)
65 views

Laborator 6: Tipuri de Baz A Operatori Pe Liste Definire Functii

This document provides a cheat sheet for Haskell with summaries of: 1) Basic types like Int, Char, String, Bool and how to determine a value's type. 2) List operations like concatenation (++), accessing head/tail, length, elem membership testing, and reversing lists. 3) Defining functions using if/then/else, guards, case expressions and pattern matching. 4) Tuples which have a fixed number of elements that can be of different types. 5) Common functional operations like map, filter, foldl, foldr, zip, and zipWith. 6) Anonymous functions using lambda syntax.

Uploaded by

Ionuț Roșoga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Laborator 6: Tipuri de Baz A Operatori Pe Liste Definire Functii

This document provides a cheat sheet for Haskell with summaries of: 1) Basic types like Int, Char, String, Bool and how to determine a value's type. 2) List operations like concatenation (++), accessing head/tail, length, elem membership testing, and reversing lists. 3) Defining functions using if/then/else, guards, case expressions and pattern matching. 4) Tuples which have a fixed number of elements that can be of different types. 5) Common functional operations like map, filter, foldl, foldr, zip, and zipWith. 6) Anonymous functions using lambda syntax.

Uploaded by

Ionuț Roșoga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Haskell CheatSheet

Laborator 6

Tipuri de bază Operatori pe liste Definire functii


(++) head tail last init take drop
5 :: Int -- if .. then .. else
’H’ :: Char [1, 2] ++ [3, 4] [1, 2, 3, 4] factorial x =
"Hello" :: String if x < 1 then 1 else x * factorial (x - 1)
True :: Bool head [1, 2, 3, 4] 1
False :: Bool tail [1, 2, 3, 4] [2, 3, 4] -- guards
factorial x
last [1, 2, 3, 4] 4 | x < 1 = 1
init [1, 2, 3, 4] [1, 2, 3] | otherwise = x * factorial (x - 1)
Determinarea tipului unei expresii
:t take 2 [1, 2, 3, 4] [1, 2] -- case .. of
take 2 "HelloWorld" "He" factorial x = case x < 1 of
> :t 42
True -> 1
42 :: Num a => a
drop 2 [1, 2, 3, 4] [3, 4] _ -> x * factorial (x - 1)
a reprezintă o variabilă de tip, restrictionată la toate
null [] True -- pattern matching
tipurile numerice. null [1, 2, 3] False factorial 0 = 1
> :t 42.0 factorial x = x * factorial (x - 1)
42 :: Fractional a => a
Alte operat, ii
In acest exemplu, a este restrictionată la toate tipurile Curry
length elem reverse
numerice fract, ionare (e.g. Float, Double). In Haskell funct, iile sunt, by default, in forma curry.
length [1, 2, 3, 4] 4
Constructori liste elem 3 [1, 2, 3, 4] True :t (+)
[] (:) elem 5 [1, 2, 3, 4] False (+) :: Num a => a -> a -> a

[] -- lista vida reverse [1, 2, 3, 4] [4, 3, 2, 1] :t (+ 1)


(:) -- operatorul de adaugare (+ 1) :: Num a => a -> a
-- la inceputul listei

1 : 3 : 5 : [] -- lista care contine 1, 3, 5 Tupluri


[1, 3, 5] -- sintaxa echivalenta Funct, ionale uzuale
Spre deosebire de liste, tuplurile au un număr fix de ele-
mente, iar acestea pot avea tipuri diferite. map filter foldl foldr zip zipWith

Operatori logici import Data.Tuple


map :: (a -> b) -> [a] -> [b]
not && || ("Hello", True) :: (String, Bool) filter :: (a -> Bool) -> [a] -> [a]
(1, 2, 3) :: (Integer, Integer, Integer) foldl :: (a -> b -> a) -> a -> [b] -> a
not True False zip :: [a] -> [b] -> [(a, b)]
not False True zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
True || False True fst ("Hello", True) "Hello"
True && False False snd ("Hello", True) True
swap ("Hello", True) (True, "Hello")
map (+ 2) [1, 2, 3] [3, 4, 5]

filter odd [1, 2, 3, 4] [1, 3]


Funct, ii anonime (lambda)
\arg1 arg2 → corp foldl (+) 0 [1, 2, 3, 4] 10
foldl (-) 0 [1, 2] -3 (0 - 1) - 2
\x -> x functia identitate foldr (-) 0 [1, 2] -1 1 - (2 - 0)
(\x y -> x + y) 1 2 3
let f = \x y -> x + y legare la un nume zip [1, 2] [3, 4] [(1, 3), (2, 4)]
(f 1 2) 3 zipWith (+) [1, 2] [3, 4] [4, 6]

You might also like