Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/make -f
CFLAGS = -O3
.PHONY: all clean
all: main
main: main.c hoge.o
cc $(CFLAGS) -o $@ $^
module Sumlen where
sumList :: (Num a) => [a] -> a
sumList = foldr (+) 0
lenList :: (Num b) => [a] -> b
lenList = foldr (\_ y->y+1) 0
sumlenList :: (Num a, Num b) => [a] -> (a,b)
sumlenList = foldr (\x (y,z)->(x+y,z+1)) (0,0)
@naohaq
naohaq / HeapTree.hs
Created July 1, 2013 14:22
IFPH Section 6.3 / Heap Tree
module HeapTree where
data HTree a = HNull | HFork a (HTree a) (HTree a) deriving Show
hflatten :: (Ord a) => HTree a -> [a]
hflatten HNull = []
hflatten (HFork x xt yt) = x : merge (hflatten xt) (hflatten yt)
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] ys = ys
@naohaq
naohaq / furui.hs
Created April 10, 2013 09:58
Sieve of Eratosthenes.
import System.Environment (getArgs)
data PFilter a = PFilter a (a -> Bool)
instance Show a => Show (PFilter a) where
show (PFilter s _) = "PF " ++ show s
mfilter :: Integral a => a -> a -> Bool
mfilter p x = (x `mod` p) /= 0
@naohaq
naohaq / sudoku.ps
Created March 14, 2013 14:14
Solving sudoku by postscript.
/# {load def} bind def
/! {exch def} bind def
/M/moveto #/L/lineto #/RM/rmoveto #/RL/rlineto #
/s/stroke #/f/fill #/S/show #/CP/closepath #
/sudokuDict 256 dict def
sudokuDict begin
@naohaq
naohaq / quicksort.txt
Last active December 12, 2015 03:09
procedure quicksort(A,M,N); value M,N;
array A; integer M,N;
begin integer I,J;
if M < N then begin partition(A,M,N,I,J);
quicksort(A,M,J);
quicksort(A,I,N);
end
end quicksort
@naohaq
naohaq / uminus.rb
Created November 28, 2012 03:59
Unary '-' operator in Ruby
colinux> ruby uminus.rb
a(0) called.
a(-3) called.
c = -3
d = -6
@naohaq
naohaq / sequence.hs
Created November 16, 2012 15:18
Re-implementation of 'sequence'
mysequence :: (Monad m) => [m a] -> m [a]
mysequence xs = loop xs []
where loop [] ys = return $ reverse ys
loop (x:xs) ys = x >>= \y -> loop xs (y:ys)
@naohaq
naohaq / struct.c
Created November 15, 2012 05:38
Using a pointer to a struct without any definition.
#include <stdio.h>
#include <stdlib.h>
struct hoge * tmp_ptr;
void
foo( void )
{
printf("%p\n", tmp_ptr);
}
@naohaq
naohaq / fizzbuzz.sed
Created November 8, 2012 10:49
FizzBuzz by sed script
#!/bin/sed -f
s/[05]$/& @Buzz/
s/[12346789]$/& @/
s/^[0-9]/@&/
T
{
:loop
s/@\([0-9]\)\(.* \)/\1@\2\1/