Skip to content

Instantly share code, notes, and snippets.

View nishio's full-sized avatar

NISHIO Hirokazu nishio

View GitHub Profile
-- Quiz: http://blog.hackers-cafe.net/2010/06/haskell-quiz.html
-- Answer: http://blog.hackers-cafe.net/2010/07/haskell-quiz-answer.html
class Foo t where
a :: t
b :: t
instance Foo Int where
a = 0
b = 1
let str_to_list (src : string) : char list =
let rec _stol (src : string) (pos : int) (rest : char list) =
let c = get src pos in
if pos > 0 then
_stol src (pos - 1) (c :: rest)
else
c :: rest
in _stol src (length src - 1) []
let rec tokenize (data : char list) : token list =
match data with
'['::xs -> Begin::(tokenize xs)
| ']'::xs -> End::(tokenize xs)
| '+'::xs -> (Inc 1)::(tokenize xs)
| '-'::xs -> (Dec 1)::(tokenize xs)
| '>'::xs -> (Right 1)::(tokenize xs)
| '<'::xs -> (Left 1)::(tokenize xs)
| '.'::xs -> Print::(tokenize xs)
| ','::xs -> Read::(tokenize xs)
let rec get_chained_token (data : char list) : (int * char list) =
let (first::rest) = data in
let rec _get data count =
match data with
| x::xs -> if x == first then
_get xs (count + 1)
else
(count, data)
| [] -> (count, [])
in
let rec f x = g (x - 1)
let rec g x =
if x < 0 then
0
else
f x
let rec f x = g (x - 1)
and g x =
if x < 0 then
0
else
f x
let f x y =
match x with
| 1 ->
match y with
| "foo" -> 2
| 2 -> 2
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'))
# The shortest oneline brainf*ck interpreter in python (558 characters)
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'))