[python] Scoping

Python now follow LEGB rule

LEGB Rule:

L. Local. (Names assigned in any way within a fuctnion (def or lambda), and not declared global in that function.

E. Enclosing function locals. (Name in the local scope of any and all enclosing functions (def or lambda), form inner to outer.

G. Global (module). Names assigned at the top-level of a module file, or declared global in a def within the file.

B. Built-in (Python). Names preassigned in the built-in names module : open,range,SyntaxError,…

And it’s the same when meeting Modules.

Ex: the example will print ‘A’ instead of  ‘main’

main.py
------------------------------------

import A
owner = 'main'
a = A.show()
A.py
-----------------------------------------

owner='A'
def show()
    print owner
Advertisements

[Python] Sudoku Solver

之前在玩Sudoku, 剛好這兩天有空閒,就用python來寫一寫,順便玩了一下wxPython來做GUI,只要輸入盤面就可以顯示解答,也用了一下官方的IDE–IDLE,還是沒有vim順手XD,不過堪用就是~ 回到台灣後好久沒寫程式了,難得玩一下,很久沒用python了,都快忘光光了= =””, 不過寫這種暴搜似乎還是用C比較合我的口味~
Code
以下是screen shot:

[PL] untype lambda calcus

最近在看Types and programming language, 利用haskell寫了一個來實做, 會自動轉成de Bruijn terms.

ex:

lambda x. lambda y. x y x; -> lambda . lambda . 1 0 1
lambda x. x; -> lambda . 0
(lambda x. x) (lambda x. x x); -> (lambda . 0 0 )
lambda x.(x x); -> (lambda . 0 0 )

BNF-converter 的grammar:


entrypoints Program;
Prog. Program::=[Def];separator Def ";" ;
TermDef. Def::= Term;

AbsTerm. Term::= “lambda” Ident “.” Term;
AppTerm. Term1::=Term1 Term2;
IdTerm. Term2::= Ident;
coercions Term 2;

for more detail code
haskell code