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

Compile and Execute Haskell Online

{- Somatório em C
int somatorio(int i, int n) {
    int soma = 0;
    int k;
    
    for(k=i;k < n; k++) {
        soma = soma + i;
    }

    return soma;
-}

somatorio::Int->Int->Int
somatorio i n = sum [i..n]


var = 100::Int   -- Em C: int var = 100;
var = 44         --       var = 44;



main = print (somatorio 1 99)

Compile and Execute Haskell Online

data Item = I String deriving (Eq,Show)
data User = U String deriving (Eq,Show)
data Rating double = NoRating | R double deriving (Eq,Show)
--first function
occ a [] = False
occ a (x:xs) = if ( a == x ) then True
else occ a xs

dis :: Eq a  => [a] -> [a]
dis [] = []
dis (x:xs) = if occ x xs then dis xs
else x:(dis xs)
--second function
fromRatingsToItems :: Eq a => [(b,a,c)] -> [a]
fromRatingsToItemshelper [] =[]
fromRatingsToItemshelper ((a,b,c):xs) = b: fromRatingsToItemshelper xs
fromRatingsToItems x = dis (fromRatingsToItemshelper x)
--third function
hasRating :: (Eq a, Eq b) => a -> b -> [(a,b,c)] -> Bool
hasRating _ _ [] = False
hasRating a b ((x,y,z):xs) = if a == x && b == y then True 
else hasRating a b xs 
--Fourth function
getRating :: (Eq a, Eq b) => a -> b -> [(a,b,c)] -> c
getRating _ _ [] = error"No given rating"
getRating a b ((x,y,z):xs) = if a == x && b == y then z 
else getRating a b xs
--Fifth function **

-------------------------------------------------------------------------------------
formMatrixUser :: (Eq a, Eq b, Fractional c) => b -> [a] -> [(b,a,c)] -> [Rating c]
formMatrixUseh _ _ []=NoRating
formMatrixUseh u i ((ua,ia,r):xs)= if(u==ua && i==ia) then (R r) 
													  else  formMatrixUseh u i xs
formMatrixUser _ [] _=[]
formMatrixUser u (i:is) x=[formMatrixUseh u i x]++ formMatrixUser u is x



--sixth function
formMatrix :: (Eq a, Eq b, Fractional c) => [b] -> [a] -> [(b,a,c)] -> [[Rating c]]

formMatrix [] _ _ = []
formMatrix (a:b) x y = formMatrixUser a x y : formMatrix b x y 
--7th function
numberRatingsGivenItem :: (Fractional a, Num b) => Int -> [[Rating a]] -> b
helper 0 (x:xs) = x
helper a (x:xs) = helper (a-1) xs
numberRatingsGivenItem _ [] = 0
numberRatingsGivenItem a (x:xs) = if helper a x == NoRating then numberRatingsGivenItem a xs
else 1+numberRatingsGivenItem a xs
--8th function
differeneRatings :: Fractional a => Rating a -> Rating a -> a
differeneRatings _ NoRating = 0.0
differeneRatings NoRating _ = 0.0
differeneRatings (R a) (R b) = a-b
--9th function
matrixPairs :: Num a => a -> [(a,a)]
helper2 a 0 = [(a,0)]
helper2 a b = [(a,b)] ++ helper2 a (b-1)
matrixPairs2 0 _= []
matrixPairs2 a b = helper2 (a-1) (b) ++ matrixPairs2 (a-1) b
matrixPairs 0 = []
matrixPairs b = reverse (matrixPairs2 b (b-1))  
--10th function

project

main = putStrLn "hello world"

Compile and Execute Haskell Online

main = putStrLn "hello world"

Compile and Execute Haskell Online

--Pi(3.14159) = pi
--Radius = r
--Diameter = d
--Circumfrance = C
--Area = A
--Area From Radius (pi * r ^ 2)
data Area = Circle Float
surface :: Area -> Float
surface(Circle r) = pi * r ^ 2
--Diameter From Radius (r * 2)
data DiameterFromRadius = Diameter Float
surfac :: DiameterFromRadius -> Float
surfac(Diameter r) = r * 2
--Circumfrance from radius (r * 2 * pi)
data CirFromRad = Cir Float
cir :: CirFromRad -> Float
cir(Cir r) = 2 * r * pi
--Area from Diameter (pi * (d / 2) ^ 2)
data AreaFromDia = Area Float
area :: AreaFromDia -> Float
area(Area d) = pi * (d / 2) ^ 2
--Circumfrance from Diameter (pi * d)
data CirFromDia = Circ Float
circ :: CirFromDia -> Float
circ(Circ d) = pi * d
--Radius from Diameter (d / 2)
data RadFromDia = Rad Float
rad :: RadFromDia -> Float
rad(Rad d) = d / 2
--Area From Circumfrance (pi * ((C / pi) / 2) ^ 2)
data AreaFromCir = Are Float
are :: AreaFromCir -> Float
are(Are c) = pi * ((c / pi) / 2) ^ 2
--Diameter From Circumfrance (C / pi)
data DiaFromCir = Dia Float
dia :: DiaFromCir -> Float
dia(Dia c) = c / pi
--Radius From Circumfrance ((C  / pi) / 2)
data RadFromCir = Ra Float
ra :: RadFromCir -> Float
ra(Ra c) = (c / pi) / 2
--Circumfrance From Area ()
--Diameter From Area ()
--Radius From Area ()
main = do
   --print (nameOfType $ nameOfMessurement messurement)
   print (surface $ Circle 10)
   print (area $ Area 20)
   

Compile and Execute Haskell Online

data Planet = Mercury
            | Venus
            | Earth
            | Mars
            | Jupiter
            | Saturn
            | Uranus
            | Neptune
            deriving (Eq, Show)

type System = [Planet]

main = putStrLn $ "hello world " ++ show Mercury

Compile and Execute Haskell Online

{-# LANGUAGE FlexibleInstances #-}
module TAMO 

where

infix 1 ==>

(==>) :: Bool -> Bool -> Bool
x ==> y = (not x) || y

infix 1 <=>

(<=>) :: Bool -> Bool -> Bool
x <=> y = x == y 

infixr 2 <+>

(<+>) :: Bool -> Bool -> Bool
x <+> y = x /= y 

p = True
q = False

formula1 = (not p) && (p ==> q) <=> not (q && (not p))

formula2 p q = ((not p) && (p ==> q) <=> not (q && (not p)))

valid1 :: (Bool -> Bool) -> Bool
valid1 bf =  (bf True) && (bf False)

excluded_middle :: Bool -> Bool
excluded_middle p = p || not p

valid2 :: (Bool -> Bool -> Bool)  -> Bool
valid2 bf =   (bf True  True)  
           && (bf True  False) 
           && (bf False True) 
           && (bf False False)

form1 p q = p ==> (q ==> p)
form2 p q = (p ==> q) ==> p

valid3 :: (Bool -> Bool -> Bool -> Bool) -> Bool
valid3 bf = and [ bf p q r | p <- [True,False], 
                             q <- [True,False], 
                             r <- [True,False]] 

valid4 :: (Bool -> Bool -> Bool -> Bool -> Bool) -> Bool
valid4 bf = and [ bf p q r s | p <- [True,False], 
                               q <- [True,False], 
                               r <- [True,False], 
                               s <- [True,False]] 

logEquiv1 ::  (Bool -> Bool) -> (Bool -> Bool) -> Bool
logEquiv1 bf1 bf2 =  
    (bf1 True  <=> bf2 True) && (bf1 False <=> bf2 False) 

logEquiv2 :: (Bool -> Bool -> Bool) -> 
                    (Bool -> Bool -> Bool) -> Bool
logEquiv2 bf1 bf2 = 
  and [(bf1 p q) <=> (bf2 p q)  |  p <- [True,False], 
                                   q <- [True,False]]

logEquiv3 :: (Bool -> Bool -> Bool -> Bool) ->
                 (Bool -> Bool -> Bool -> Bool) -> Bool
logEquiv3 bf1 bf2 = 
  and [(bf1 p q r) <=> (bf2 p q r) |  p <- [True,False], 
                                      q <- [True,False], 
                                      r <- [True,False]] 

formula3 p q = p 
formula4 p q = (p <+> q) <+> q

formula5 p q = p <=> ((p <+> q) <+> q)

class TF p where 
  valid :: p -> Bool
  lequiv :: p -> p -> Bool

instance TF Bool
 where
  valid  = id
  lequiv f g = f == g

instance TF p => TF (Bool -> p)
 where
  valid f = valid (f True) && valid (f False)
  lequiv f g = (f True) `lequiv` (g True)
               && (f False) `lequiv` (g False)

test1  = lequiv id (\ p -> not (not p))
test2a = lequiv id (\ p -> p && p) 
test2b = lequiv id (\ p -> p || p) 
test3a = lequiv (\ p q -> p ==> q) (\ p q -> not p || q)
test3b = lequiv (\ p q -> not (p ==> q)) (\ p q -> p && not q)
test4a = lequiv (\ p q -> not p ==> not q) (\ p q -> q ==> p)
test4b = lequiv (\ p q -> p ==> not q) (\ p q -> q ==> not p)
test4c = lequiv (\ p q -> not p ==> q) (\ p q -> not q ==> p)
test5a = lequiv (\ p q -> p <=> q) 
                (\ p q -> (p ==> q) && (q ==> p))
test5b = lequiv (\ p q -> p <=> q) 
                (\ p q -> (p && q) || (not p && not q))
test6a = lequiv (\ p q -> p && q) (\ p q -> q && p)
test6b = lequiv (\ p q -> p || q) (\ p q -> q || p)
test7a = lequiv (\ p q -> not (p && q)) 
                (\ p q -> not p || not q)
test7b = lequiv (\ p q -> not (p || q)) 
                (\ p q -> not p && not q)
test8a = lequiv (\ p q r -> p && (q && r)) 
                (\ p q r -> (p && q) && r)
test8b = lequiv (\ p q r -> p || (q || r)) 
                (\ p q r -> (p || q) || r)
test9a = lequiv (\ p q r -> p && (q || r)) 
                (\ p q r -> (p && q) || (p && r))
test9b = lequiv (\ p q r ->  p || (q && r)) 
                (\ p q r -> (p || q) && (p || r))

square1 :: Integer -> Integer
square1 x = x^2 

square2 :: Integer -> Integer 
square2 = \ x -> x^2

m1 :: Integer -> Integer -> Integer 
m1 = \ x -> \ y -> x*y

m2 :: Integer -> Integer -> Integer 
m2 = \ x y -> x*y

solveQdr :: (Float,Float,Float) -> (Float,Float) 
solveQdr =  \ (a,b,c) -> if a == 0 then error "not quadratic"
                         else let d = b^2 - 4*a*c in 
                         if d < 0 then error "no real solutions"
                         else 
                           ((- b + sqrt d) / 2*a,
                            (- b - sqrt d) / 2*a)

every, some :: [a] -> (a -> Bool) -> Bool
every xs p = all p xs 
some  xs p = any p xs

hask

main ::Integer->Integer
main x =x*x

prac

main = putStrLn "hello world"

Calc

data Token
data Expression

tokenize :: String -> [Token]
tokenize = undefined

parse :: [Token] -> Expression
parse = undefined

evaluate :: Expression -> Double
evaluate = undefined

main :: IO()
main = putStrLn "It works, so are we done yet?"

Advertisements
Loading...

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