Skip to content

Instantly share code, notes, and snippets.

:set -i./src -i./bin
-- Read GHCI commands from the file whose name is
-- in the GHCIRC environment variable
:def _load const(System.Environment.getEnvironment>>=maybe(return"")readFile.lookup"GHCIRC")
:_load
:undef _load
:def hoogle \str -> return $ ":! hoogle --count=15 \"" ++ str ++ "\""
-- <http://www.cs.kent.ac.uk/people/staff/cr3/toolbox/haskell/dot-squashed.ghci641>
let { redir varcmd = case break Data.Char.isSpace varcmd of { (var,_:cmd) -> return $ unlines [":set -fno-print-bind-result","tmp <- System.Directory.getTemporaryDirectory","(f,h) <- System.IO.openTempFile tmp \"ghci\"","sto <- GHC.Handle.hDuplicate System.IO.stdout","GHC.Handle.hDuplicateTo h System.IO.stdout","System.IO.hClose h",cmd,"GHC.Handle.hDuplicateTo sto System.IO.stdout","let readFileNow f = readFile f >>= \\t->length t `seq` return t",var++" <- readFileNow f","System.Directory.removeFile f"]; _ -> return "putStrLn \"usage: :redir <var> <cmd>\"" } }
:def redir redir
@maoe
maoe / ViewPatterns.hs
Created June 21, 2010 23:12
How to use view patterns
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ViewPatterns #-}
module ViewPatterns where
import Data.List
data EMail = EMail User Domain deriving Show
type User = String
type Domain = String
view :: String -> Maybe EMail
// data Tree a = Leaf | Branch a (Tree a) (Tree a) deriving Show
//
// isEmpty :: Tree a -> Bool
// isEmpty Leaf = True
// isEmpty _ = False
//
// insert :: Ord a => a -> Tree a -> Tree a
// insert e Leaf = Branch e Leaf Leaf
// insert e b@(Branch e' l r)
// | e < e' = Branch e' (insert e l) r
import scala.actors._
import scala.actors.Actor._
import java.net.InetAddress
sealed abstract class DnsMessage
case class DnsRequest(name: String, sender: Actor) extends DnsMessage
case class DnsResponse(name: String, address: Option[InetAddress]) extends DnsMessage
class DnsResolver extends Actor {
def act() {
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct rlimit rlimit_t;
int main(void)
{
rlimit_t *r = (rlimit_t *) malloc(sizeof(struct rlimit));
module BiFold where
import Prelude hiding (foldr, foldl)
-- http://d.hatena.ne.jp/nobsun/20100824/1282617326
bifold :: ((a, c) -> b -> (a, c)) -> (a, c) -> [b] -> (a, c)
bifold f (a, c) (b:bs) = (a'', c'')
where (a', c'') = f (a, c') b
(a'', c') = bifold f (a', c) bs
bifold _ z _ = z
@maoe
maoe / HyloM.hs
Created September 11, 2010 02:35
unfoldMの使い方
module Main where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.Loops
import System.Environment
import System.IO
main :: IO ()
main = do
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UnicodeSyntax #-}
module Arrow where
import Control.Arrow
import Control.Applicative (pure)
import Control.Category
import Control.Category.Unicode ((∘), (⋙))
import Prelude hiding (id, (.))
import qualified Prelude
@maoe
maoe / gist:784011
Created January 18, 2011 05:05
Fibs.hs
module Fibs where
import StreamProc
import Control.Arrow
test = take 10 $ runSP fibs [0..]
fibs :: SP a Integer
fibs = put 0 fibs'
where fibs' = put 1 $ liftA2 (+) fibs fibs'
@maoe
maoe / Existentials.hs
Created February 2, 2011 16:47
UHCのドキュメントにあるexistentialsの例をGHCでやってみた
{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Data.Char (ord)
data Exists r = forall a. Exists a (a -> r)
x2 :: Exists Int
x2 = Exists (3 :: Int) id
xapp :: Exists a -> a