Skip to content

Instantly share code, notes, and snippets.

@truthadjustr
Last active August 4, 2018 07:34
Show Gist options
  • Save truthadjustr/03fb08f786beb1ebc1429f7ba235b73f to your computer and use it in GitHub Desktop.
Save truthadjustr/03fb08f786beb1ebc1429f7ba235b73f to your computer and use it in GitHub Desktop.
fp Sat Aug 4 15:21:38 +08 2018
import Data.List
import Data.List.Split
-- a :: Num a => [a]
a = [1..10]
-------------------------------------------------------------------------------
myreverse xxs =
case xxs of
[] -> []
(x:xs) -> myreverse xs ++ [x]
-------------------------------------------------------------------------------
mean0 xxs = sum xxs / fromIntegral (length xxs)
-------------------------------------------------------------------------------
mean1 xxs = sum xxs / genericLength xxs
-------------------------------------------------------------------------------
mean2 xxs = s/n
where
s = sum xxs
n = fromIntegral(length xxs)
--n = length xxs
-------------------------------------------------------------------------------
avg = go 0 0
where
go a n [] = a/n
go a n (x : xs) = go (a + x) (n + 1) xs
-------------------------------------------------------------------------------
f0 :: [a] -> [a]
f0 = (reverse =<<) . chunksOf 2
-------------------------------------------------------------------------------
chunks :: Int -> [a] -> [[a]]
chunks _ [] = []
chunks n xs =
let (ys, zs) = splitAt n xs
in ys : chunks n zs
-------------------------------------------------------------------------------
chunks0 n = takeWhile (not.null) . unfoldr (Just . splitAt n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment