Skip to content

Instantly share code, notes, and snippets.

@naohaq
naohaq / bar.rb
Created May 29, 2012 02:41
Top level local variable in Ruby
load "foo.rb"
puts a # Error!
@naohaq
naohaq / del0.c
Created June 20, 2012 01:15
Delete characters on heads of lines.
#include <stdio.h>
int
main( )
{
int c;
int newl = 0;
while((c = getchar( )) != EOF) {
if (c == '\r' || c == '\n') {
newl = 1;
@naohaq
naohaq / local.rb
Created June 22, 2012 08:15
Scoping of local variables in Ruby
colinux> ruby local.rb
foo(true):
a is not defined.
a is defined.
a = 1
foo(false):
a is not defined.
a is defined.
a = nil
@naohaq
naohaq / func_if.rb
Created July 19, 2012 04:44
Use "if" as a name of a method.
#!/usr/bin/ruby1.9.1 -Ku
# -*- mode: ruby; coding: utf-8-unix -*-
class Foo
def if(p,t,f)
if p.call then
t.call
else
f.call
end
@naohaq
naohaq / block_if.rb
Created July 19, 2012 05:03
Represent then/else clauses as blocks.
#!/usr/bin/ruby1.9.1 -Ku
# -*- mode: ruby; coding: utf-8-unix -*-
class Conditional
class Else
def initialize(p,v)
@p = p
@v = v
end
@naohaq
naohaq / ptr.c
Created November 7, 2012 03:09
A compilation result of cast "const int *" into "int *"
#include <stdio.h>
void
f(int * p)
{
*p = 2;
}
int
main(int argc, char * argv[])
@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/
@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 / 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 / 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