Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cohama
cohama / onestep_eval.ml
Last active November 27, 2021 12:14
TaPL 7 章 型なしラムダ計算の1ステップ評価の OCaml による実装
(* Utilities *)
let (|>) x f = f x
let (|-) f g = fun x -> g (f x)
let getOrElse defVal = function
| Some(v) -> v
| None -> defVal
(* Term Definition *)
type term =
| TmVar of int * int (* 変数 : deBruijn_index * term_length *)
@cohama
cohama / test.md
Last active August 2, 2021 03:34

りすと

  • リスト
  • になる

これは

  • リストに
  • なる
@cohama
cohama / cps2.rs
Last active August 7, 2017 22:18
fn main() {
repeat_cps(10, "hello", Box::new(|r| {
println!("{:?}", r)
}))
}
fn repeat_cps<'a, A, B>(n: i32, x: A, k: Box<Fn(A) -> B + 'a>) -> B
{
if n == 0 {
k(x)
fn main() {
println!("{:?}", take_while(&[1, 2, 3, 4, 5], |&x| x < 3));
}
fn take_while<T, F>(xs: &[T], f: F) -> &[T]
where F: Fn(&T) -> bool
{
let mut i = 0;
while f(&xs[i]) {
i += 1;
/* eslint no-console: "off", no-unused-vars: "off" */
var xs = [
{id: 'A', pid: '', name: 'aaa'},
{id: 'B', pid: '', name: 'bbb'},
{id: 'C', pid: '', name: 'ccc'},
{id: 'A-a', pid: 'A', name: 'aaa-aaa'},
{id: 'A-b', pid: 'A', name: 'aaa-bbb'},
{id: 'B-a', pid: 'B', name: 'bbb-aaa'},
{id: 'B-b', pid: 'B', name: 'bbb-bbb'},
{id: 'C-a', pid: 'C', name: 'ccc-aaa'},
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
import Data.Ratio
class (Num a) => Dividable a where
divide :: a -> a -> a
instance {-# INCOHERENT #-} (Integral a) => Dividable a where
divide = div
instance Dividable Double where
@cohama
cohama / rpn.hs
Created December 17, 2013 16:00
すごい Haskell 10 章の逆ポーランド電卓 Maybe を使ったエラー処理をしてる版
import Data.String
import Control.Monad
import Data.Maybe
main :: IO ()
main = interactEachLines $ (fromMaybe "Error!") . rpn
interactEachLines :: (String -> String) -> IO ()
interactEachLines f = interact $ unlines . (map f) . lines
let s:V = vital#of('vital')
function! s:truncate(text, max_width, ellipsis)
let ellipsis_width = strdisplaywidth(a:ellipsis)
let text_width = strdisplaywidth(a:text)
if text_width > a:max_width
let truncated = s:V.strwidthpart(a:text, a:max_width - ellipsis_width)
" for double width string
if strdisplaywidth(truncated) != a:max_width - ellipsis_width
let truncated .= ' '
endif
@cohama
cohama / todo.hs
Created December 1, 2013 13:46
すごい Haskell 9.5 もっと ToDo リスト
import System.Environment
import System.Directory
import System.IO
import Data.List
import Control.Exception
dispatch :: String -> [String] -> IO ()
dispatch "add" = add
dispatch "view" = view
dispatch "remove" = remove
@cohama
cohama / formatter_spec.vim
Last active December 29, 2015 06:29
Vim script でランダム文字列生成
let s:V = vital#of('r')
let s:Xor = s:V.import('Random.Xor128')
function! s:randomString(char_kind, length)
let rand_str = ''
for i in range(a:length)
let rand_char = a:char_kind[abs(s:Xor.rand()) % len(a:char_kind)]
let rand_str .= rand_char
endfor
return rand_str