Skip to content

Instantly share code, notes, and snippets.

@kgadek
kgadek / chineseRemainder.hs
Created May 2, 2013 16:07
Chinese Remainder Theorem in Haskell
{-# LANGUAGE UnicodeSyntax,FlexibleInstances #-}
import Control.Monad (foldM)
-- |Extended Euclidean algorithm
-- Given A,B, finds integers X,Y that satisfy Bézout's identity:
-- A*X + B*Y = gcd(A,B)
egcd a b = aux a b 1 0 0 1
where aux r 0 x y _ _ = (r,x,y)
aux r r' x y x' y' = aux r' r'' x' y' x'' y''
where r'' = r `rem` r'
@kgadek
kgadek / generators.erl
Created May 9, 2013 23:57
Generator-thingy in Erlang
-module (generators).
-define (YIELD (F), fun() -> F end).
-export ([fib/0]).
fib() ->
{0, ?YIELD(fib(1,1))}.
fib(A,B) ->
{A, ?YIELD(fib(B, A+B))}.
@kgadek
kgadek / avs3.hs
Created May 27, 2013 23:03
AVSystems challenge 3
palindromes base = concat $ zipWith (\bcofs -> (map sum . sequence . zipWith (\b -> map (*b)) bcofs)) gen coefs
where
aa x = x : aa (x*base) -- [1, 10, 100, 1000, ..]
a = repeat $ aa 1 -- [ [1, ..], [1, 10, ..], [1, 10, 100, ..], ..]
b = scanl (\(a:acc) x -> base*a:a:acc) [1] [1..] -- [ [1], [10,1], [100, 10, 1], ..]
c = [-1] : [0] : map (\xs -> 0:(map (*base) xs)) c -- [ [-1], [0], [0, -10], [0, 0], [0, 0, -100], ..]
gen = zipWith3 (zipWith3 (\x y z -> x+y+z)) a b c -- [ [1], [11], [101, 10], [1001, 110], [10001,1010,100], ..
@kgadek
kgadek / a3.hs
Last active December 17, 2015 21:29
avs 3 another try
{-# LANGUAGE BangPatterns #-}
import System.Environment (getArgs)
import Data.List (foldl1', foldl')
import Data.Word (Word64)
palindromeCoeffBase :: Word64 -> [[Word64]]
palindromeCoeffBase b = oddCoeff 1 $ auxList
where oddCoeff n (x:xs) = (zipWith (\x y -> if x==y then x else x+y) x $ take n inc) : evenCoeff n xs
evenCoeff n (x:xs) = (zipWith (+) x $ take n inc) : oddCoeff (n+1) xs
@kgadek
kgadek / fm_draw.lisp
Created June 25, 2013 00:35
FM Group -- network draw
#|
Konwertuje plik z opisem drzewa współpracowników FM Group[1] (plik *.csv; patrz: +datafile+)
do języka dot[2].
Linki:
[1] http://www.perfumy.fm/inc/tree2.php
[2] http://www.graphviz.org/
|#
(declaim (optimize (speed 3) (compilation-speed 0) (safety 0) (debug 0)))
@kgadek
kgadek / test.hs
Created September 18, 2013 01:05
I cannot into attoparsec
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import qualified Data.Text.IO
import qualified Data.Attoparsec.Text as AT
import qualified Data.Attoparsec.Combinator as ATC
import Control.Monad
readValue :: AT.Parser T.Text
readValue = do
-- http://www.reddit.com/r/compsci/comments/1mv7n2/can_anyone_please_help_me_crack_this_circuit/
-- Hey /r/compsci
-- I am in the midst of doing an assigment that requires me to
-- make a circuit that has 7 inputs and outputs a 1 if the input
-- have at least or more than 4 ones. The requirements are that
-- I can only 4 AND gates and limitless XOR gates. Also I can
-- only have 2 input connected to a gate.
--
-- I've been looking at this for 4 hours now and really can't
@kgadek
kgadek / gist:7119881
Created October 23, 2013 14:28
Haskell MPI example
-- Below is a small but complete MPI program. Process 1 sends the message "Hello World"
-- to process 0, which in turn receives the message and prints it to standard output.
-- All other processes, if there are any, do nothing.
-- -- http://hackage.haskell.org/package/haskell-mpi
module Main where
import Control.Parallel.MPI.Simple (mpiWorld, commWorld, unitTag, send, recv)
main :: IO ()
@kgadek
kgadek / c.cpp
Created November 11, 2013 23:34
Fixer dla Malwiny
#include <cstdio>
#include <cstdlib>
int main() {
int ch;
int cha;
ch = getchar();
if(ch == 0xEF) {
getchar(); // 0xBB
getchar(); // 0xBF
[ 20%] Building CXX object CMakeFiles/daemon.dir/src/sqlite.cpp.o
In file included from /home/konrad/lachesis/cpp/src/sqlite.cpp:1:
In file included from /home/konrad/lachesis/cpp/src/sqlite.hpp:7:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/memory:81:
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/bits/unique_ptr.h:137:9: error: static_assert failed "constructed with null function pointer deleter"
{ static_assert(!is_pointer<deleter_type>::value,
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/konrad/lachesis/cpp/src/sqlite.cpp:32:20: note: in instantiation of member function 'std::unique_ptr<sqlite3, int (*)(sqlite3 *)>::unique_ptr' requested here
LachesisSQLite3db::LachesisSQLite3db(const char *db_path) throw(sqlExecException) {
^