repl.it
@sebastienmorgan/

Haskell_01

Haskell

No description

fork
loading
Files
  • main.hs
  • Geometry
  • Geom.hs
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Data.List 
import Data.Array
import Data.Char
import Data.Maybe
import qualified Data.Set as Set 
import Geometry.Sphere
import Geom
--import qualified Geometry.Sphere as Sphere 

-- data Person = Person String String Int Float String String deriving (Show) -->

data Person = Person { firstName :: String
                     , lastName :: String
                     , age :: Int
                     , height :: Float
                     , phoneNumber :: String
                     , flavor :: String
                     } deriving (Show)

map' :: (a -> b) -> [a] -> [b]  
map' f xs = foldr (\x acc -> f x : acc) [] xs  

sqrtSum' :: Int
sqrtSum' = length (takeWhile (<1000) (scanl1 (+) (map sqrt [1..]))) + 1

showMbe :: Show a => Maybe a -> String
showMbe Nothing = "-1"
showMbe x = show (fromJust x)

-- show (fromMaybe 0x0 (125 `elemIndex` [1,2,3,4,5,6]))
-- "Im a big baby" \\ "big" 

calcT' :: String -> Int
calcT' cs = foldl f 0 $ words cs
   where f acc x = acc + (f1 $ span (`elem` ['0'..'9']) x) 
         f1 :: (String, String) -> Int
         f1 (f, s) = read f * cf s
         cf ch 
            | ch == "w" = 40
            | ch == "d" = 8
            | otherwise = 1

calcTime :: String -> Int
calcTime [] = 0
calcTime x = 
  let parsed = words x
      getItem ch p = head . filter (ch `isSuffixOf`) $ p
      getNum p s = case item of [] -> 0
                                xs -> read item
        where item = init (getItem p s)      
      w = (getNum "w" parsed) * 40
      d = (getNum "d" parsed) * 8
      h = (getNum "h" parsed)
  in w + d + h


search :: (Eq a) => [a] -> [a] -> Bool
search needle haystack = 
    let nlen = length needle
    in foldl (\acc x -> if take nlen x == needle then True else acc) False (tails haystack)

{-
let xs = [[5,4,5,4,4],[1,2,3],[3,5,4,3],[],[2],[2,2]] 
sortBy (compare `on` length) xs  
-}

main = do
  putStrLn "𝛌"
  putStrLn (show (calcT' "3d 5h"))
  --putStrLn (show $ Geometry.Sphere.volume 2.9)
  --putStrLn $ Geom.Circle 10 20 5 
  --putStrLn (show (calcTime "1w 1d 1h"))
GHCi, version 8.6.5