Skip to content

Instantly share code, notes, and snippets.

-- rewrite after checking the actual formula :X
leibs_pi :: Double -> Double
leibs_pi n = 4 * sum [(-1.0)**n' / (2.0*n' + 1.0) | n' <- [0.0..(n-1)]]
@qoelet
qoelet / lebs_pi.hs
Last active December 21, 2015 14:39
-- leibniz's pi formula
-- a very elementary take i guess :P
odd_ds :: Int -> [Double]
odd_ds n = [x | x <- take n [1.0,3.0..]]
y_eq :: Double -> Double -> Double
y_eq d s = s * (1.0 / d)
leibs_series :: [Double] -> Double -> Double
-- just giving functors and the applicative operators a test drive
import Control.Applicative
import Data.Char
emails :: [String]
emails = ["[email protected]", "[email protected]", "somethingthatsmellslikeSPAM", "[email protected]", "[email protected]"]
hasAt :: [Char] -> Bool
-- read a text file
-- print a line, wait for return by user, repeat. exits when no more lines to print
import System.Environment
import System.IO
import System.Exit
parse ["-h"] = putStrLn "./text_reader [-h] file" >> exitWith ExitSuccess
parse [f] = readFile f
import System.Random
import Control.Monad
subV :: Integer -> Integer -> Integer
subV x y | x == y = 0
| otherwise = 1
encode :: [Integer] -> [Integer] -> [Integer]
encode [] _ = []
encode (p:ps) (v1:v1s) = [subV p v1] ++ encode ps v1s
-- implementing a simple 2 key encrypt/decrypt
-- assuming a n-vector p (message)
-- generate a random n-vector v1 (key1)
-- and n-vector v2 (via subtract v1 from p)
subV :: Integer -> Integer -> Integer
subV x y | x == y = 0
| otherwise = 1
encode :: [Integer] -> [Integer] -> [Integer]
{-# LANGUAGE OverloadedStrings #-}
import Prelude
import qualified Prelude as P
import Data.Monoid (mempty)
import Web.Scotty
import qualified Web.Scotty as S
import Text.Blaze.Html5
import qualified Text.Blaze.Html5 as H
let q s = putStrLn (s ++ show s) in q "let q s = putStrLn (s ++ show s) in q "
-- s is mapped to string "let q s = putStrLn (s ++ show s) in q "
-- q is mapped to expr putStrLn (s ++ show s)
-- show s produces a string with quotes escaped
-- e.g. show "hello" produces "\"hello\""
-- 'in q' above seems to cause the expression to be evaluated
-- e.g.
import Data.Char
type Bit = Int
-- in reverse order
bits2int :: [Bit] -> Int
bits2int bits = sum [w*b | (w,b) <- zip weights bits]
where weights = iterate (*2) 1
int2bin :: Int -> [Bit]
-- this works
expo :: Int -> Int -> Int
expo 0 n = 0
expo m 0 = 1
expo m n = m * (expo m (n - 1))
-- not this one
-- attempt to load in ghci throws: exponentiation.hs:11:10: Parse error in pattern: n + 1
expo :: Int -> Int -> Int
expo 0 n = 0