Skip to content

Instantly share code, notes, and snippets.

@naohaq
naohaq / Q2357.hs
Created November 2, 2017 06:14
An extension field of ℚ
{- -*- coding: utf-8-unix -*- -}
module Q2357
( ExtR2(..)
, ExtR3(..)
, ExtR5(..)
, ExtR7(..)
, Q2357
, factor
, root2
@naohaq
naohaq / chomp.hs
Last active October 3, 2017 03:54
Stripping '\r' at the end of the string.
module Main where
import qualified Data.ByteString.Char8 as C
chomp :: C.ByteString -> C.ByteString
chomp str = chompCRLF $ chompCRLF str
where chompCRLF s =
case C.unsnoc s of
Nothing -> s
Just (s_c, '\r') -> s_c
%!PS-Adobe-3.0
%%BoundingBox: 0 0 280 36
%%HiResBoundingBox: 0.0 0.0 280.0 36.0
%%CreationDate: Fri Jul 31 14:07:27 2015
%%BeginProlog
/! { load def } def
/M/moveto !/RM/rmoveto !/G/glyphshow !
%%EndProlog
%%BeginPageSetup
<</PageSize [280 36] /Margins [0 0] /PageOffset [0 0] /Orientation 0 >> setpagedevice
@naohaq
naohaq / skapplicative.hs
Created July 15, 2015 04:08
Combinators defined under Applicative.
module Main where
import Control.Applicative
newtype A = A Integer deriving Show
newtype B = B Integer deriving Show
newtype C = C Integer deriving Show
k :: Applicative f => a -> f a
k = pure
@naohaq
naohaq / sum_sed.sh
Created May 14, 2015 04:36
Sum of 1 to 10
#!/bin/sh
cmd=$(cat <<EOF)
s/j/.i/g
s/i/.h/g
s/h/.g/g
s/g/.f/g
s/f/.e/g
s/e/.d/g
s/d/.c/g
@naohaq
naohaq / summation_third.sh
Created May 14, 2015 04:00
Sum of 1 to 10
#!/bin/sh
cnt=`mktemp`
acc=`mktemp`
for c in a b c d e f g h i j ; do
echo -n $c >> $cnt ; cat $cnt >> $acc
done
wc -c < $acc
@naohaq
naohaq / summation_next.sh
Created May 14, 2015 03:44
Sum of 1 to 10
#!/bin/sh
fn=`mktemp`
echo -n 'a' >> $fn
echo -n 'aa' >> $fn
echo -n 'aaa' >> $fn
echo -n 'aaaa' >> $fn
echo -n 'aaaaa' >> $fn
echo -n 'aaaaaa' >> $fn
#!/bin/sh
echo -n 'aababcabcdabcdeabcdefabcdefgabcdefghabcdefghiabcdefghij' | wc -c
#!/usr/bin/tail -1
55
@naohaq
naohaq / ps-mode.el
Created April 3, 2015 05:26
Emacs lisp for semi-automatic update of "CreationDate" comment on a postscript file
(defun format-time-string-with-locale-c (fmt)
(let ((system-time-locale "C")) (format-time-string fmt)))
(require 'ps-mode)
(defun ps-mode-update-creationdate ()
(interactive nil)
(save-excursion
(goto-char (point-min))
(and
(re-search-forward "^%%CreationDate: " nil t)