Skip to content

Instantly share code, notes, and snippets.

> List.map ((+)1) [1..10000000];;
Real: 00:00:06.655, CPU: 00:00:07.328, GC gen0: 83, gen1: 64, gen2: 3
val it : int list =
[2; 3; 4; 5; 6; 7; 8; ...]
> let map_cps f l' =
- let rec go l c =
- match l with
- | [] -> c []
- | (x::xs) -> go xs (fun xs' -> c (f x :: xs'))
module Main(main) where
foo :: [()] -> ()
foo x = bar x where
bar x = {-# SCC "my" #-} head x
main = print $ foo []
{-
*** Exception (reporting due to +RTS -xc): (THUNK_2_0), stack trace:
mvmap f v = go $ M.length v where
go 0 = return ()
go (n + 1) = update v n f >> go n
@klapaucius
klapaucius / foldm.hs
Created March 13, 2012 10:39
worker/wrapper
{-# OPTIONS_GHC -fstatic-argument-transformation #-}
module Main (main) where
import Criterion.Main
import Control.Monad
import Data.List
import Data.Functor.Identity
foldM' :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
structure S1 = struct
fun foo (x:{cells:int list, columns:int, rows:int}) = (#columns x, #rows x, #cells x)
end;
structure S2 = struct
fun foo (x:{cells:int list, columns:int, rows:int}) = (#columns x, #rows x, #cells x)
end;
S1.foo {columns = 1, rows = 2, cells = [1,2]};
val it = (1,2,[1,2]) : int * int * int list
@klapaucius
klapaucius / 1.hs
Created July 4, 2012 10:47
XDefaultSignatures
Prelude Control.Monad> :set -XDefaultSignatures
Prelude Control.Monad>
newtype Box a = Box a deriving Show
Prelude Control.Monad>
instance Monad Box where
return = Box
(Box x) >>= f = f x
@klapaucius
klapaucius / gist:3357481
Created August 15, 2012 07:59
Typeable
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Control.Exception
Prelude Control.Exception> import Data.Typeable
Prelude Control.Exception Data.Typeable>
newtype Foo = Foo (() -> IO ())
Prelude Control.Exception Data.Typeable> :set -XSafe
@klapaucius
klapaucius / gist:3367586
Created August 16, 2012 07:03
runtime/compile-time
Prelude> class Pet a where kill :: a -> String
Prelude> data Dog = Dog deriving Show
Prelude> data Cat = Cat deriving Show
Prelude> instance Pet Dog where kill = const "dead"
Prelude> instance Pet Cat where kill = const "c_1|dead> + c_2|alive>"
Prelude> map kill [Cat, Cat] -- диспетчеризация времени компиляции
["c_1|dead> + c_2|alive>","c_1|dead> + c_2|alive>"]
Prelude> map kill [Dog, Dog] -- диспетчеризация времени компиляции
["dead","dead"]
Prelude> data PetBox where Box :: Pet a => a -> PetBox
> :{
let merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) | x < y = x: merge xs (y:ys)
| otherwise = y: merge (x:xs) ys
:}
> :set -XNoMonomorphismRestriction
> let mergeAll = foldr1 merge
> take 20 $ mergeAll [[5..],[1..],[3..],[7..],[2..],[1..]]
[1,1,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,5,6,6]
@klapaucius
klapaucius / gist:3795780
Created September 27, 2012 19:01
type-prelude
*Prelude.Type> T::T (Any ((==) 1) '[3,2,1])
True
it ::
T (Bool -> Constraint)
(Any
Nat (Nat == 1) ((':) Nat 3 ((':) Nat 2 ((':) Nat 1 ('[] Nat)))))
*Prelude.Type> T::T (Any ((==) 1) '[3,2])
False
it ::
T (Bool -> Constraint)