Skip to content

Instantly share code, notes, and snippets.

@qoelet
qoelet / goto.hs
Created September 14, 2013 08:38
-- Another day in #haskell
-- (fix $ \goto x -> case x of Nothing -> "hello"; Just _ -> goto Nothing) (Just ())
-- lambdabot: "hello"
import Data.Function (fix)
goto :: Maybe t -> [Char]
goto t = (fix (\goto x -> case x of Nothing -> "hello"; Just _ -> goto Nothing)) t
-- ghci> goto (Just 5)
import Data.List
import Control.Applicative
main :: IO ()
main = do
let tt = [True, True]
ff = [False, False]
tf = head perms_tf
ft = reverse (head perms_tf)
perms_tf = permutations [True, False]
sinusoid :: [Double] -> Double -> Double -> Double -> [Double]
sinusoid [] _ _ _ = []
sinusoid (n:ns) amp ang_freq phase = amp * (cos (ang_freq * n + phase)) : sinusoid ns amp ang_freq phase
-- I finally got sick of reading books on Maybe and IO monads!
-- I spyed beyond that there must be mooarrr!
import Control.Monad.Identity
agent :: (Num a) => a -> Identity a
agent n = do
return n >>= (\n -> Identity (n * 2))
main :: IO ()
@qoelet
qoelet / 2to3.hs
Last active December 23, 2015 19:49
-- from a pair to a triple.
-- a lambda for my newborn son.
((\z (x,y) -> (x, y, z)) "Isaac" ((\x y -> (x,y)) "Kenny" "Audrey"))
-- ("Kenny","Audrey","Isaac")
data ConnectionTest = ConnectionTest { ctVersion :: String, ctResult :: String }
deriving (Show)
instance ToJSON ConnectionTest where
toJSON (ConnectionTest versionV resultV) = object [ "ctVersion" .= versionV,
"ctResult" .= resultV ]
instance FromJSON ConnectionTest where
parseJSON (Object v) = ConnectionTest <$>
v .: "version" <*>
-- the below will fail
data Type1 = Type1 { title :: String, another :: Integer }
data Type2 = Type2 { title :: String, random :: Integer }
-- with an error of "Multiple declarations of 'title'" or something along the lines
-- Thread on Stackoverflow: http://stackoverflow.com/questions/5367167/haskell-record-syntax
-- My simple cheap workaround was to seperate them into different sub-module files (obviously not scalable)
-- Thanks to Chris Forno (http://jekor.com/) who directed me to possible ways (and new brain food!)
-- Given a timestamp from a JSON API call
-- Need to convert to Epoch
import Control.Applicative ((<$>))
import Control.Monad (liftM)
import Data.Time
import Data.Time.Clock.POSIX
import System.Locale
main = do
-- Captain Crunch fun: serialization in Haskell using cereal ("serial, cereal... get it?")
-- Install: cabal install cereal
-- serialization
main = do
let msg = "hello cereal!"
cerealized = encode msg
writeFile "myFile" cerealized
@qoelet
qoelet / fast_fib.hs
Last active December 24, 2015 16:39
-- Calculating a Fibonacci number (see: http://en.wikipedia.org/wiki/Fibonacci_number)
fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
-- Solving a quiz, I needed to generate a Fibonacci sequence up to a certain limit, e.g. fib n < limit
-- Usual recursion here is expensive and inefficient, takes too long.
fibLst :: Integer -> [Integer]
fibLst 0 = []