'Added haskell small tutorial'
This commit is contained in:
parent
83e14be4ce
commit
1b8341f443
138
08_Haskell/ex1-tutorial.hs
Executable file
138
08_Haskell/ex1-tutorial.hs
Executable file
@ -0,0 +1,138 @@
|
|||||||
|
#!/usr/bin/env runhaskell
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print [1..10]
|
||||||
|
|
||||||
|
|
||||||
|
-- fType :: Int -> Int -> Int
|
||||||
|
-- fType x y = x*x + y*y
|
||||||
|
-- main = print (fType 2 4)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--fType :: Integer -> Integer -> Integer
|
||||||
|
--fType x y = x*x + y*y
|
||||||
|
--main = print (fType 212124454 4454545445455454545445445454544544638247328432894364872648762378462378647823547267865435843685734857394785)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--fact :: Integer -> Integer
|
||||||
|
--fact n | n == 0 = 1
|
||||||
|
-- | n /= 0 = n * fact (n-1)
|
||||||
|
--main = do
|
||||||
|
-- putStrLn "The factorial of 5 is:"
|
||||||
|
-- print (fact 5)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- roots :: (Float, Float, Float) -> (Float, Float)
|
||||||
|
-- roots (a,b,c) = (x1, x2) where
|
||||||
|
-- x1 = e + sqrt d / (2 * a)
|
||||||
|
-- x2 = e - sqrt d / (2 * a)
|
||||||
|
-- d = b * b - 4 * a * c
|
||||||
|
-- e = - b / (2 * a)
|
||||||
|
-- main = do
|
||||||
|
-- putStrLn "The roots of our Polynomial equation are:"
|
||||||
|
-- print (roots(1,-8,6))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- import Data.Char
|
||||||
|
-- import Prelude hiding (map)
|
||||||
|
|
||||||
|
-- map :: (a -> b) -> [a] -> [b]
|
||||||
|
-- map _ [] = []
|
||||||
|
-- map func (x : abc) = func x : map func abc
|
||||||
|
-- main = print $ map toUpper "tutorialspoint.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- main = do
|
||||||
|
-- putStrLn "The successor of 4 is:"
|
||||||
|
-- print ((\x -> x + 1) 4)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- main = do
|
||||||
|
-- let x = [1..10]
|
||||||
|
-- putStrLn "Our list is:"
|
||||||
|
-- print (x)
|
||||||
|
-- putStrLn "The first element of the list is:"
|
||||||
|
-- print (head x)
|
||||||
|
-- putStrLn "The tail of our list is:"
|
||||||
|
-- print (tail x)
|
||||||
|
-- putStrLn "The last element of our list is:"
|
||||||
|
-- print (last x)
|
||||||
|
-- putStrLn "Our list without the last entry:"
|
||||||
|
-- print (init x)
|
||||||
|
-- putStrLn "Is our list empty?"
|
||||||
|
-- print (null x)
|
||||||
|
-- putStrLn "The list in Reverse Order is:"
|
||||||
|
-- print (reverse x)
|
||||||
|
-- putStrLn "The length of this list is:"
|
||||||
|
-- print (length x)
|
||||||
|
-- print(take 5 ([1 .. 10]))
|
||||||
|
-- print(drop 5 ([1 .. 10]))
|
||||||
|
-- putStrLn "The maximum value element of the list is:"
|
||||||
|
-- print (maximum x)
|
||||||
|
-- putStrLn "The minimum value element of the list is:"
|
||||||
|
-- print (minimum x)
|
||||||
|
-- putStrLn "The summation of the list elements is:"
|
||||||
|
-- print (sum x)
|
||||||
|
-- putStrLn "The multiplication of the list elements is:"
|
||||||
|
-- print (product x)
|
||||||
|
-- putStrLn "Does it contain 786?"
|
||||||
|
-- print (elem 9 (x))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- doubleListA [] = []
|
||||||
|
-- doubleListA (x:xs) = 2*x : doubleListA xs
|
||||||
|
-- tripleListB [] = []
|
||||||
|
-- tripleListB (x:xs) = 3*x : tripleListB xs
|
||||||
|
-- multList n [] = []
|
||||||
|
-- multList n (x:xs) = n*x : multList n xs
|
||||||
|
-- tripleList = multList 3
|
||||||
|
-- doubleList = multList 2
|
||||||
|
|
||||||
|
-- main = do
|
||||||
|
-- let x = [1..10]
|
||||||
|
-- putStrLn "Our list is:"
|
||||||
|
-- print (x)
|
||||||
|
-- --print (doubleListA x)
|
||||||
|
-- print (doubleList x)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- eveno :: Int -> Bool
|
||||||
|
-- noto :: Bool -> String
|
||||||
|
|
||||||
|
-- eveno x = if x `rem` 2 == 0
|
||||||
|
-- then True
|
||||||
|
-- else False
|
||||||
|
-- noto x = if x == True
|
||||||
|
-- then "This is an even Number"
|
||||||
|
-- else "This is an ODD number"
|
||||||
|
|
||||||
|
-- main = do
|
||||||
|
-- putStrLn "Example of Haskell Function composition"
|
||||||
|
-- print ((noto.eveno)(16))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- import Data.List
|
||||||
|
|
||||||
|
-- main = do
|
||||||
|
-- putStrLn("Different methods of List Module")
|
||||||
|
-- print(intersperse '.' "Tutorialspoint.com")
|
||||||
|
-- print(intercalate " " ["Lets","Start","with","Haskell"])
|
||||||
|
-- print(splitAt 7 "HaskellTutorial")
|
||||||
|
-- print (sort [8,5,3,2,1,6,4,2])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- factors n = [x | x <- [1..n], mod n x == 0]
|
||||||
|
-- prime n = factors n == [1,n]
|
||||||
|
-- main = do
|
||||||
|
-- print (prime 9)
|
||||||
|
-- print (prime 19)
|
Loading…
Reference in New Issue
Block a user