daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 63 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. Haskell counting elements of a list
  2. report :: [[String]] -> String -> [String]
  3. report (x:xs) typ = do
  4.                     case typ of
  5.                         "registrations" -> reportReg (map head xs)
  6.                         "completions" -> reportReg (map head xs)
  7.  
  8. reportReg :: [String]
  9. reportReg [x] = do
  10.                     print x
  11.                     print 1
  12. reportReg (x:xs) = do
  13.                     let count = instances x (x:xs)
  14.                     print x
  15.                     print count
  16.                     let newlist = filter (==x) (x:xs)
  17.                     reportReg newlist
  18.  
  19. instances::String->[String]->Int
  20. instances x [] = 0
  21. instances x (y:ys)
  22.     | x==y = 1+(instances x ys)
  23.     | otherwise = instances x ys
  24.    
  25. reportReg :: [String]
  26.    
  27. reportReg :: [String] -> IO ()
  28.    
  29. Prelude> :load typef.hs
  30. [1 of 1] Compiling Main             ( typef.hs, interpreted )
  31. Ok, modules loaded: Main.
  32. *Main> :t report
  33. report :: (Eq a, Show a) => [[a]] -> [Char] -> IO ()
  34. *Main> :t reportReg
  35. reportReg :: (Eq a, Show a) => [a] -> IO ()
  36. *Main> :t instances
  37. instances :: (Num t, Eq a) => a -> [a] -> t
  38.    
  39. reportReg :: [String]
  40.    
  41. reportReg :: [String] -> ???
  42.    
  43. reportReg :: [String] -> IO ()
  44.    
  45. reportReg :: [a] -> IO ()
  46. reportReg [] = do return ()
  47. reportReg xs = do print (head x)
  48.                   print (count x xs)
  49.                   reportReg (tail xs)
  50.  
  51. count :: a -> [a] -> Integer
  52. count x xs = length (filter (==x) xs)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top