Skip to content

Instantly share code, notes, and snippets.

@plaster
plaster / gcj2013-r1a-b-small.scm
Last active December 16, 2015 23:49
Google Code Jam 2013 Round 1A Problem B solution core
(use srfi-42)
(define (dfs E0 E R vs)
(match vs
[ () 0]
[ (v . vs)
(max-ec (:range e 0 (+ E 1))
(+ (* v e)
(dfs E0
(min E0 (+ E R (- e)))
#! /usr/bin/env python
import sys
def zigzag(sig, steps):
while True:
yield (sig, steps)
sig *= -1
steps += 1
def locus(x, motion):
i = 0
@plaster
plaster / LibZip'.hs
Last active December 14, 2015 12:09
LibZip を ByteString と String に両対応させようとしてコンパイルが通らない何か
{- | Polymorphic version of @Codec.Archive.LibZip@.
Codec.Archive.Libzip is with useful and simple API, and a high-leveled library to deal with zip archives.
...Although, in the library, specification way of the path to a file inside a zip archive is `String`.
This limitation is inconvenient especially when dealing with zip archived file
of which path contains foreign system's multi-byte characters.
This module provides APIs of polymorphic version,
which can specify and receive archived file's path as String or Data.ByteString (as you like).
@plaster
plaster / convtest.hs
Created February 17, 2013 15:18
ISO-2022-JP な行と UTF-8 な行の混ざったファイルを読んでみるテスト
import Codec.Text.IConv as IConv
import Codec.Binary.UTF8.String as UTF8
import Data.ByteString as B
import Data.ByteString.Lazy as BL
import System.IO as IO
convertDetect :: [ IConv.EncodingName ] -> IConv.EncodingName -> BL.ByteString ->
Either BL.ByteString (IConv.EncodingName, BL.ByteString)
convertDetect [] _ src = Left src
convertDetect (srcEnc:srcEncs) dstEnc src = case IConv.convertStrictly srcEnc dstEnc src of

孤独のHaskell 第二話 参加記録

知り合いの主催する勉強会イベント「孤独のHaskell 第二話」http://www.zusaar.com/event/502008 があったので、参加してきました。 みなさん自由な感じで特に発表などもなく、好きに過ごす感じです。

そんな中、Haskell初心者な私は

  • 「すごいHaskellたのしく学ぼう」を教科書に
  • 「今日はIOをできるようになろう!」を目標に
@plaster
plaster / memotest.clj
Last active December 10, 2015 22:38
メモ化再帰実験
(defn fib [n]
(let [a-fib (atom false)]
(reset! a-fib (memoize (fn [n] (if (< 1 n) (+ (@a-fib (- n 1)) (@a-fib (- n 2))) 1))))
(@a-fib n)
))
;;; inspired by @tnoda
(def ^:dynamic *m-fib*)
(defn fibv [n]
(binding [*m-fib* (memoize (fn [x] (if (< 1 x) (+ (*m-fib* (- x 1)) (*m-fib* (- x 2))) 1)))]
(def solve
(memoize
(fn [w h]
(if (or (zero? w)
(zero? h))
1
(+ (solve (dec w) h)
(solve w (dec h)))
))))
;;; Project Euler Problem 23 solution
;;; http://projecteuler.net/problem=23
(use 'clojure.test)
(def limit 28123)
(defn gen-sigma1-list
"original implementation by @ypsilon-takai: https://gist.github.com/4284814"
[^long size]
@plaster
plaster / gist:4363770
Last active February 18, 2017 06:54
Clojureのダイナミックスコープと`Var/setDynamic`

https://gist.github.com/4357479 で「ダイナミックスコープな変数であるかどうか」をプログラムから(というかreplから)知る方法について調べた経緯です。

*print-readably* を対象にして調べる途上で出会ったいろいろなものについて、とりとめなくgdgdに書いています。

var

replで *print-readably* だけみると、値については教えてくれますが、変数については教えてくれません。 さっきのしらべものの途中で見つけた http://d.hatena.ne.jp/athos/20111204/elephant_things_in_clojure#'fact の表記があったため、試してみたところ

pe-16.core=> #'*print-readably*