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

Lab 1

main :: IO ()
main
= print (Node Tip 100 (Node Tip 200 Tip))


len :: [Int] -> Int
len [] = 0
len (x:xs) = 1 + len xs

contains :: Eq a => [a] -> a -> Bool
contains [] element = False
contains (x:xs) element = if x == element then True else contains xs element

-- set :: [String] -> Bool
-- set [] = True
-- set (x:xs) = 
--     if contains xs x then False 
--     else set xs
    
--another way of doing it 

set :: [String] -> Bool
set []
    = True
set (x:xs)
    = not (contains xs x) && set xs
    
--don't forget it will return true if it gets down to an empty list, else it will return false

nodups :: [Int] -> Bool
nodups []
    = True
nodups (x:xs) 
    = if contains xs x then False
    else nodups xs

zipped :: ([a],[b]) -> [(a,b)]
zipped (xs,[]) = []
zipped ([],ys) = []
zipped ((x:xs), (y:ys))
    = (x,y) : zipped (xs,ys)

insert :: Int -> [Int] -> [Int]
insert e []
    = [e]
insert e (x:xs)
    | e > x = x : insert e xs
    | otherwise = e : x : xs



sort :: [Int] -> [Int]
sort [] = []
sort (x:xs) = insert x (sort xs)

data Tree
    = Tip
    | Node Tree Int Tree
    deriving show 

mirror :: Tree -> Tree
mirror Tip
    = Tip
mirror (Node l x r)
    = Node (mirror r) x (mirror l)

Advertisements
Loading...

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