Tutorialspoint

Compile and Execute Haskell Online

module Geometry
( sphereVolume
, sphereArea
, cubeVolume
, cubeArea
, cuboidArea
, cuboidVolume
) where

sphereVolume :: Float -> Float
sphereVolume radius = (4.0 / 3.0) * pi * (radius ^ 3)

sphereArea :: Float -> Float
sphereArea radius = 4 * pi * (radius ^ 2)

cubeVolume :: Float -> Float
cubeVolume side = cuboidVolume side side side

cubeArea :: Float -> Float
cubeArea side = cuboidArea side side side

cuboidVolume :: Float -> Float -> Float -> Float
cuboidVolume a b c = rectangleArea a b * c

cuboidArea :: Float -> Float -> Float -> Float
cuboidArea a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2

rectangleArea :: Float -> Float -> Float
rectangleArea a b = a * b

Compile and Execute Haskell Online

--isVowel :: Char -> Bool
--isVowel vowel
--          |vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u' = True 
--          |otherwise = False

-- isNegative :: Float -> Bool
-- isNegative inputNum = if inputNum < 0 then True else False 


-- middleNumber :: Integer -> Integer -> Integer -> Integer
-- middleNumber  x y z
--           |x > y && x < z || x > z && x < y = x
--           |y > x && y < z || y > z && y < x = y
--           |otherwise = z

--triangleArea :: Integer -> Integer -> Float 
--triangleArea a b = (fromIntegral a * fromIntegral b) * 0.5

nor :: Bool -> Bool -> Bool
nor a b
     | a == True  && b == True  = False
     | a == False && b == True  = False
     | a == True  && b == False = False
     | a == False && b == False = True

hasRemainder :: Integer -> Integer -> Bool 
hasRemainder divisor divised = if div divisor divised  == 0 then False else True

letterGrade :: Integer -> String 
letterGrade grade
             | grade >= 0  && grade < 60 = "F"
             | grade >= 60 && grade < 70 = "D"
             | grade >= 70 && grade < 80 = "C"
             | grade >= 80 && grade < 90 = "B"
             | otherwise = "A"

averageThree :: Integer -> Integer -> Integer-> Float
averageThree num num1 num2 = (fromIntegral num + fromIntegral num1 +fromIntegral num2) / 3

tripleNumber :: Integer -> Integer
tripleNumber num
             |num <= 100 = num * 3
             |otherwise = num


-- howManyBelowAverage :: Integer -> Integer -> integer -> Float 
-- howManyBelowAverage num num1 num2
--                              |num < averageThree num num1 num2
--                              |otherwise = 2.0


main = print(tripleNumber 101)

Haskell tutorial - 1

-- Addition Operator
-- https://www.tutorialspoint.com/haskell/haskell_basic_operators.htm
{-main = do 
   let var1 = 2.9 
   let var2 = 3 
   putStrLn "The addition of the two numbers is:" 
   print(var1 + var2) 
-}


-- Sequence / Range Operator
{-main :: IO() 
main = do 
   print [1..10]
-}  


-- Int
{-futureValue :: Float -> Float -> Float -> Float  
futureValue capitalInicial juroAnual numeroDeAnos = capitalInicial*(1+juroAnual/100)**numeroDeAnos
main = print (futureValue 100.0 10.0 1.0) 
-}

-- Factorial
{-fact :: Int -> Int 
fact 0 = 1
fact a = a * fact (a-1)
main = print (fact 3)
-}

-- Factorial on lists
{-
fact :: Int -> Int 
fact 0 = 1
fact a = a * fact (a-1)
map :: (a -> b) -> [a] -> [b]
--map f xs = [f x | x <- xs]
map fact [x1, x2, ..., xn] == [fact x1, fact x2, ..., fact xn]

map fact [x1, x2, ...] == [fact x1, fact x2, ...]
--map fact [1,1,2,3,5,8] 
main = print ( map fact [1..10] ) 
-}

-- Reverse list
{-revr :: [Int] -> [Int]
revr [] = []
revr (h:tl) =  revr tl ++ [h]
main = print (revr [1..20])
-}

-- Max of list
{-maximus :: [Int] -> Int 
maximus [a] = a
maximus (x1:tl) = if x1 > (maximus tl)
                  then x1 
                  else maximus tl
main = print (  maximus [231,9000,22,3] ) 
-}

-- Circle Area
-- URL:  https://www.tutorialspoint.com/haskell/haskell_types_and_type_class.htm
data Area = Circle Float Float Float  
surface :: Area -> Float   
surface (Circle _ _ r) = pi * r ^ 2   
main = print (surface $ Circle 10 20 10 ) 























surface (Circle _ _ r) = pi * r ^ 2   
main = print (surface $ Circle 10 20 10 )
-}

Compile and Execute Haskell Online

mean :: [Double] -> Double
mean [a] = a
mean xs = sum xs / (fromIntegral (length xs))

varianceSummation :: [Double] -> Double -> Double
varianceSummation [a] mean = (a - mean)^2
varianceSummation (x:xs) mean = (x - mean)^2 + varianceSummation xs mean

myVariance :: [Double] -> Double
myVariance xs = (varianceSummation xs (mean xs)) / (fromIntegral (length xs))

coVarianceSummation :: [Double] -> [Double] -> Double -> Double -> Double
coVarianceSummation [a] [b] meanX meanY = (a - meanX) * (b - meanY)
coVarianceSummation (x:xs) (y:ys) meanX meanY = ((x - meanX) * (y - meanY) +
                           coVarianceSummation xs ys meanX meanY)
                           
myCovariance :: [Double] -> [Double] -> Double
myCovariance xs ys = (coVarianceSummation xs ys (mean xs) (mean ys))
                     / (fromIntegral ((length xs) - 1))
                     
dotprd :: [Double] -> [Double] -> Double
dotprd [a] [b] = a * b 
dotprd (x:xs) (y:ys) = (x * y) + (dotprd xs ys)

vectorlen :: [Double] -> Double
vectorlen xs = sqrt (dotprd xs xs)

getangle :: [Double] -> [Double] -> Double
getangle xs ys = acos ((dotprd xs ys) / ((vectorlen xs) * (vectorlen ys)))

main = do 
    print (myCovariance [1.0,2.0] [1.0,2.0])

Haskell Add function

import Data.List
import System.IO

type Stack = [Int]

step :: Stack -> String -> Stack
step stack "+" = head stack + stack !! l :drop 2 stack
step stack "-" = head stack + stack !! l :drop 2 stack
step stack "*" = head stack + stack !! l :drop 2 stack
step stack string = read string : stack

Compile and Execute Haskell Online

main = putStrLn "hello world"

□/□ * □/□ = □□/□

-- □/□ * □/□ = □□/□
r0 = [[a,b,c,d,e,f,g]|a<-[1..9],b<-[2..9],c<-[(a+1)..9],d<-[2..9],e<-[1..9],f<-[1..9],g<-[2..9],(a/b)*(c/d)==(10*e+f)/g,a/=b,a/=c,a/=d,a/=e,a/=f,a/=g,b/=c,b/=d,b/=e,b/=f,b/=g,c/=d,c/=e,c/=f,c/=g,d/=e,d/=f,d/=g,e/=f,e/=g,f/=g]
r1 = [[a,b,c,d,e,f,g]|a<-[1..9],b<-[1..9],c<-[(a+1)..9],d<-[1..9],e<-[1..9],f<-[1..9],g<-[2..9],(a/b)*(c/d)==(10*e+f)/g,a/=b,a/=c,a/=d,a/=e,a/=f,a/=g,b/=c,b/=d,b/=e,b/=f,b/=g,c/=d,c/=e,c/=f,c/=g,d/=e,d/=f,d/=g,e/=f,e/=g,f/=g]
f x = map floor x
ans0 = map f r0
ans1 = map f r1
f0 = do 
    putStrLn "One is NOT permitted as denominator"
    putStrLn $ show(length(ans0)) ++ " combinations"
    print ans0

f1 = do
    putStrLn "permissive of 1 as denominator"
    putStrLn $ show(length(ans1)) ++ " combinations"
    print ans1


main = do
    f0
    f1

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




find single element

import Data.Bits

findSingle :: [Int] -> Int
findSingle = foldl xor 0


main = putStrLn $ show  $ findSingle [1, 2, 2, 1, 5]

Compile and Execute Haskell Online

main = putStrLn "hello world"

1 2 3 4 5 6 7 ... 11 Next
Advertisements
Loading...

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