Tuesday, November 30, 2010

What is 4277075694

Hi, it is NISHIO Hirokazu. I encountered the magic number, 4277075694, when I hacked on Windows. I searched where it comes from. However, it doesn't appear on any code. I also searched 0xFEEEFEEE, 0xFEEE and 65262, and it neither.

Finally I found: 4277075694 (0xFEEEFEEE) comes from Windows NT's heap manager. When a memory released, it is set to 0xFEEEFEEE.

Here are other magic numbers:

0xBAADF00D : 3131961357
0xFDFDFDFD : 4261281277
0xCDCDCDCD : 3452816845
0xDDDDDDDD : 3722304989
0xFEEEFEEE : 4277075694


Here is good instruction: Win32 Debug CRT Heap Internals

Thursday, July 15, 2010

The shortest oneline brainf*ck interpreter in python (558 characters)

Hi, it is Dr. NISHIO Hirokazu. This code was written in 2006-09 but I didn't have publish yet in English.

get from here: http://gist.github.com/476940. It takes a filename as an argument and exec it as Brainf*ck code. All illegal characters are ignored.

exec(reduce(lambda x,y:x.replace(y[0],y[1:]),'Iz"d",|Jz"t",|K1),"|L.read(|Mimport |O,lambda|Psys.std|Q",0);z"|R"Yzp,|S}.get(code[|T)or |U"YZor z"p",p|Vifilter(bool,(|W)%256)or Z,"|X)for x in count())).next()|Y:lambda:|Zz"c",c+1)|q(globals().get(p,0)|zglobals().__setitem__('.split('|'),'Msys;from itertools Mcount,ifilter;z"cQpQj"O kYq==0)^(k==-1)and(Jc+kTI1TVId+{"]":-1,"[":1St],0)*kTd==0 or Jt+kXand z"c",t+1T1TZ);z"code",file(sys.argv[1])L));Vc==len(code)or({">U+K<U-K+Rq+1W-Rq+255W."YPout.write(chrq)TZ,",Rord(PinL1)W[":j(K]":j(-1),Sc]O:Z)()and NoneX'))

See this charm point!

>U+K<U-K+Rq+1W-Rq+255W

It's a definition of >, <, + and - !

Monday, July 12, 2010

Haskell Quiz Answer

Hi, it is Dr. NISHIO Hirokazu. This is an answer for Haskell Quiz. Please read it in advance.

Why some program show strange behavior? To answer the question, I should define what is "strange". For example the following behavior is strange.


// JS
var a = ***, b = ***, c = ***;
console.log(a < b); // true
console.log(b < c); // true
console.log(c < a); // true


In the case, "strange" means "different from mathematical behavior". Now the answer is clear, it is because those are not purely mathematical objects.


var a = "9", b = 10, c = "100";
console.log(a < b); // true
console.log(b < c); // true
console.log(c < a); // true


Haskell also shares some common "strangeness" with other languages.

Prelude> let x = *****
Prelude> x == x + 1
True

You may know the quiz in the other language. This "strangeness" come from the limited resolution of floating point number.

Prelude> let x = 2 ** 64
Prelude> x == x + 1
True


Now, return to the problem I posted.

In Haskell, you can overload different value on one name.
You may disagree. You may see "Multiple declarations of something" error.
But wait. Didn't you have ever defined 'show'? Isn't it overloaded?

Haskell is very different from C++/Java. In C++/Java, you can overload functions whose type of arguments are different. In Haskell, you can also overload functions whose type of return value are different. It is very important.

The quiz's tricks are,

  1. You can overload in Haskell
  2. You can overload functions whose return value are different
  3. and there are no need to take argument
  4. (+) and (==) are t -> t -> t, so it can be used to restrict arguments' type. x + (1::Int) says x is (x::Int).
  5. Number Literal (e.g. 0) are (Num a) => a, so it can be used as both (0::Int) and (0::Integer).


Now let me show my answer.


class Foo t where
a :: t
b :: t

instance Foo Int where
a = 0
b = 1

instance Foo Integer where
a = 1
b = 0

c :: Int
c = 0

d :: Integer
d = 0

main = do
print $ a + c == 0 -- True.
print $ a == c -- True. a::Int == c::Int == 0.
print $ c == 0 -- True.
print $ a + d == 1 -- True.
-- d is Integer. So the variable a is a::Integer,
-- which equals 1. "d == 1" is a misconception.
print $ b + c == 1 -- True. b::Int is 1.
print $ b + d == 0 -- True. However, b::Integer is 0.
print $ b == d -- True. Yes, both are 0::Integer;
print $ d == 0 -- True.
-- Why I use "d == 0" not "b == 0"?
-- Because it causes "ambiguous type" error.
-- You know, ":t 0" is "(Num t) => t". No way to determine
-- it is Int or Integer.

-- hints to make answer unique (perhaps...)
print $ sum([a, b, c]) -- 1: It is [0, 1, 0]::[Int]
print $ sum([a, b, d]) -- 1: It is [1, 0, 0]::[Integer]
print $ [a, b, c] !! a -- 0: (!!) take Int argument,
print $ [a, b, c] !! b -- 1: so it says (a::Int /= b::Int)
print $ [a, b, c] !! c -- 0
-- Why I didn't show [a, b, c] !! d is,
-- it causes error because d is Integer.

Friday, July 2, 2010

Real Point-free Haskell Program

Hi, it is NISHIO Hirokazu. I'm trying to make *REAL* point-free Haskell program. In other words, I want to make point-free (arguments-less) Haskell code with any point(.)

You may know, usually most of Haskell programmer use (.) operator to combine functions. However, it is not necessary. It can replace with (<*>) and const.


Prelude Control.Applicative> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Prelude Control.Applicative> :t \x y z -> (const x <*> y) z
\x y z -> (const x <*> y) z :: (a -> b) -> (b1 -> a) -> b1 -> b


So anytime you want to use (.) operator, you can do it without (.)


Prelude Control.Applicative> (map . (+)) 1 [1, 2, 3]
[2,3,4]
Prelude Control.Applicative> (const map <*> (+)) 1 [1, 2, 3]
[2,3,4]


And it is also easy to remove points from float expression:


Prelude> 1.5
1.5
Prelude> 15e-1
1.5


The most biggest problem is how to remove this point.

import Control.Applicative
^

Tuesday, June 22, 2010

Haskell Quiz

Hi, it is Dr. NISHIO Hirokazu. Today I made a quiz. See following code and answer the definitions of a, b, c and d.


main = do
print $ a + c == 0 -- True.
print $ a == c -- True. a == c == 0.
print $ c == 0 -- True. Of course.
print $ a + d == 1 -- True. d == 1.
print $ b + c == 1 -- True. b == 1.
print $ b + d == 0 -- True. What happened?!
print $ b == d -- True. ?!
print $ d == 0 -- True. ?!?!
-- hints to make answer unique (perhaps...)
print $ sum([a, b, c]) -- 1
print $ sum([a, b, d]) -- 1
print $ [a, b, c] !! a -- 0
print $ [a, b, c] !! b -- 1
print $ [a, b, c] !! c -- 0


I swear:

  • I didn't define (+)
  • I didn't define (!!)
  • I didn't define (==)
  • I didn't define sum
  • I didn't define print
  • I didn't define ($) -- @kmizu sent me a valid solution and he defined ($). I didn't.
  • I didn't use default -- @camlspotter said it may use defaulting, and I didn't know it. I agree with him that it would be easier if I used defaulting. But I didn't. I didn't use default defaulting (i.e. default ordering of Integer and Double) For more detail, see http://www.haskell.org/onlinereport/decls.html#sect4.3.4

All of them are original, came from the prelude module.

The answer will be published on the blog at least 1 week after.

Monday, June 7, 2010

PONG-like game played on iPad with tangible controller

Hi, it is NISHIO Hirokazu. Today I introduce my recent demo movie of Muroto system. Using MUROTO you can controll games on iPad with tangible figures. There are "Muroto device" under the figures. And "Muroto library" tells the type of figures you put on iPhone, the direction and position.

It's not jail-braked. See further information on http://www.nishiohirokazu.org/muroto/

Monday, May 31, 2010

Another Demo movie of "Muroto for iPad"

Hi, it is NISHIO Hirokazu. I posted Hacker's Cafe Blog: MUROTO: tangible figure as a controller for iPad apps, however it said difficult to see that Muroto can get the information of position and direction. So I made another demo program and movie again!

In the movie, it is more clear:

Wednesday, May 26, 2010

MUROTO: tangible figure as a controller for iPad apps

Hi, it is NISHIO Hirokazu. I and KUCHII Jun made a system to control iPhone/iPad apps with tangible figure for "Make: Tokyo Meeting 05" on the last Sunday. We called it "MUROTO". Using MUROTO you can know the type of figure you put on iPhone, the direction and also position(not shown in movie)
So you can use tangible figures as a controller! We are making demo apps for iPad now. Don't miss it! See also: PONG-like game played on iPad with tangible controller

Thursday, May 13, 2010

13 chars in [4,+,-,*,/] can make all number in range(60)

Hi, it is Dr. NISHIO Hirokazu. Today I wonder how much characters required to make fixed-width one-digit-only digital clock. Which digit is the most suitable?

When I allow to use bitwise calculation, 5 is the most suitable digit. It requires only 8 characters.

0 555/5555
1 ~~-5+-~5
2 -~-5+-~5
3 555&5*55
...

Surprisingly in the hardest restriction it requires only 13 characters.

  • no bitwise operation. allow only the 4 arithmetical operations.
  • no minus-minus connection. 4--4 = 4 - -4 is not allowed
  • negate operation should not be aside of the other operation. 4+-4, 4*-4, 4/-4 are also omitted
  • true division. 4 / 44 is not 0, it is 1 / 11.


Here is the result. I think 37 == 4444/44-4*4*4 is very cute!! I love it!


0 4444*4-4*4444
1 44-44+444/444
2 4/4+4444/4444
3 44/44+4/4+4/4
4 4-4/4+444/444
5 4-4-4+4+4+4/4
6 4-4/4+4-44/44
7 -4-44+44+44/4
8 4-4*44+44*4+4
9 4-4+4+4+44/44
10 444/4-4444/44
11 -4+4-4+4+44/4
12 4-4-4-4+4*4+4
13 4-4-4+4*4+4/4
14 4-4/44+444/44
15 4-4+4*4-44/44
16 -4-4-4-4*4+44
17 4*4+4444/4444
18 4+4*4-4/4-4/4
19 -44+4*4*4-4/4
20 4-4/4+4*4+4/4
21 4-4+4+4*4+4/4
22 4-4+44/4+44/4
23 4+4+4*4-44/44
24 4-44+44+4*4+4
25 -4-4*4+44+4/4
26 -4/4+4*4+44/4
27 -4+4*4+4+44/4
28 4*4-4-4+4*4+4
29 4-4-4-44/4+44
30 4+4+44/4+44/4
31 4*4+4*4-44/44
32 44-44/4-44/44
33 44/44+4*4+4*4
34 44+44/44-44/4
35 -44/44-4-4+44
36 -44-4-4+44+44
37 4444/44-4*4*4
38 4-44/4+44+4/4
39 -4-4+4-4/4+44
40 -4*4-4+4*4+44
41 -4+44+444/444
42 -4+44+4/4+4/4
43 -4+44+4-44/44
44 -4*44+44*4+44
45 -44+44+44+4/4
46 -4/4-4/4+4+44
47 -4-4/4+4+44+4
48 -4-4-4+4*4+44
49 -44/4-4+4*4*4
50 44-4-4/4+44/4
51 4+44+44/4-4-4
52 -44+4+44+44+4
53 44+4+4*4-44/4
54 44+44/4-44/44
55 44-44+44+44/4
56 4-4-4-4+4*4*4
57 4/4-4-4+4*4*4
58 4-4/4+44+44/4
59 44+4-4+4+44/4


see also The list of all numbers which can generate from nine 4s and arithmetics operations.

Wednesday, April 7, 2010

What happen if you put both str and unicode key on a dict in Python?


In [1]: {u"a": 1, "a": 2}
Out[1]: {u'a': 2}


Yas, it is because:


In [2]: u"a" == "a"
Out[2]: True


Python2.* think u"a" and "a" are EQUAL. of course:


In [3]: u"a" is "a"
Out[3]: False


they are not IDENTITY. If Guido designed to use IDENTITY to check whether they are SAME key, it causes:


In [4]: (1, 2) is (1, 2)
Out[4]: False


that uncomfortable behavior. We don't want to distinguish EQUAL tuples. It is a difficult question on designing new languages. Those behavior was changed from Python3.0. On it, bytes(byte sequence) and text(unicode char sequence) are not compatible. No automatic conversion between them.


>>> b"a" == "a"
False
>>> {b"a": 1, "a": 2}
{b'a': 1, 'a': 2}

Monday, February 8, 2010

A Quiz like an IQ test

Hi, it is Dr. NISHIO Hirokazu. I enjoyed an IQ test by Mensa. And found such a non-verbal quiz is good for publishing to the world. I just made another quiz. Please try it!


It is published under Creative Commons BY 3.0 License.

For your information: my IQ was 138.






Making quiz is fun! Here is another one!






And another one!

Sunday, January 10, 2010

Can you say what this graphics is?

Hi, it is Dr. NISHIO Hirokazu, a mathematical artist!

Today's question is:


Hint: There are no randomness. There are no magic numbers in my code (except for the paper's width, height and an 2)

Here is a close-up of the graphics:






I struggled to show the interesting mathematical interesting structures and got this:



It contains some magic numbers (for folding, spacing etc.) but more easy to understand than the first 'pure' graphics.




P.S. (2010-01-07)

Ivanhoe said
Without a high-resolution version of the complete (or at least the upper left corner) of the artwork it seems hard to guess.


Oh, it is very good question. The upper left corner is the most important part of the artwork. It helps you to solve the quiz.


Ivanhoe said...
I would say that this figure the last bit or the sum parity of some 2D function, am I close ?
It is reciprocal numbers in binary notation. 1/2 = 0.1, 1/3 = 0.01010101..., 1/4 = 0.01, 1/5 = 0.00110011, etc. I omit preceding 0s, and make 0 as black, 1 as white. Left-most column corresponts to 1/2.