#!/usr/bin/env python3
def unfoldTree( f, b) :
match f( b) :
case a, bs:
return [ a, [ unfoldTree( f, b) for b in bs] ]
def foldTree( f, tree) :
def go( node) :
match node:
case x, ts:
return f( x, [ go( child) for child in ts] )
return go( tree)
if __name__ == '__main__' :
def buildNode( x) :
return ( x, [ ] ) if 2 * x + 1 > 7 else ( x, [ 2 *x, 2 *x + 1 ] )
# 二分木の生成
print ( unfoldTree( buildNode, 1 ) )
# 木の値の総和
print ( foldTree( lambda x, xs: sum ( [ x] + xs) , [ 1 , [ [ 2 , [ ] ] , [ 3 , [ ] ] ] ] ) )
# 木の値の最大値
print ( foldTree( lambda x, xs: max ( [ x] + xs) , [ 1 , [ [ 2 , [ ] ] , [ 3 , [ ] ] ] ] ) )
# 木の葉の個数
print ( foldTree( lambda x, xs: 1 if xs == [ ] else sum ( xs) , [ 1 , [ [ 2 , [ ] ] , [ 3 , [ ] ] ] ] ) )
# 木の高さ
print ( foldTree( lambda x, xs: 0 if xs == [ ] else 1 + max ( xs) , [ 1 , [ [ 2 , [ ] ] , [ 3 , [ ] ] ] ] ) )
# フィボナッチ数列
def fib( n) :
def g( n) :
match n:
case 0 :
return ( 0 , [ ] )
case 1 :
return ( 1 , [ ] )
case n:
return ( n, [ n - 1 , n - 2 ] )
return foldTree( lambda x, xs: x if xs == [ ] else sum ( xs) , unfoldTree( g, n) )
print ( fib( 5 ) )
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKZGVmIHVuZm9sZFRyZWUoZiwgYik6CiAgICBtYXRjaCBmKGIpOgogICAgICAgIGNhc2UgYSwgYnM6CiAgICAgICAgICAgIHJldHVybiBbYSwgW3VuZm9sZFRyZWUoZiwgYikgZm9yIGIgaW4gYnNdXQoKZGVmIGZvbGRUcmVlKGYsIHRyZWUpOgogICAgZGVmIGdvKG5vZGUpOgogICAgICAgIG1hdGNoIG5vZGU6CiAgICAgICAgICAgIGNhc2UgeCwgdHM6CiAgICAgICAgICAgICAgICByZXR1cm4gZih4LCBbZ28oY2hpbGQpIGZvciBjaGlsZCBpbiB0c10pCiAgICByZXR1cm4gZ28odHJlZSkKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CgogICAgZGVmIGJ1aWxkTm9kZSh4KToKICAgICAgICByZXR1cm4gKHgsIFtdKSBpZiAyICogeCArIDEgPiA3IGVsc2UgKHgsIFsyKngsIDIqeCArIDFdKQoKICAgICMg5LqM5YiG5pyo44Gu55Sf5oiQCiAgICBwcmludCh1bmZvbGRUcmVlKGJ1aWxkTm9kZSwgMSkpCiAgICAjIOacqOOBruWApOOBrue3j+WSjAogICAgcHJpbnQoZm9sZFRyZWUobGFtYmRhIHgsIHhzOiBzdW0oW3hdICsgeHMpLCBbMSwgW1syLCBbXV0sIFszLCBbXV1dXSkpCiAgICAjIOacqOOBruWApOOBruacgOWkp+WApAogICAgcHJpbnQoZm9sZFRyZWUobGFtYmRhIHgsIHhzOiBtYXgoW3hdICsgeHMpLCBbMSwgW1syLCBbXV0sIFszLCBbXV1dXSkpCiAgICAjIOacqOOBruiRieOBruWAi+aVsAogICAgcHJpbnQoZm9sZFRyZWUobGFtYmRhIHgsIHhzOiAxIGlmIHhzID09IFtdIGVsc2Ugc3VtKHhzKSwgWzEsIFtbMiwgW11dLCBbMywgW11dXV0pKQogICAgIyDmnKjjga7pq5jjgZUKICAgIHByaW50KGZvbGRUcmVlKGxhbWJkYSB4LCB4czogMCBpZiB4cyA9PSBbXSBlbHNlIDEgKyBtYXgoeHMpLCBbMSwgW1syLCBbXV0sIFszLCBbXV1dXSkpCgogICAgIyDjg5XjgqPjg5zjg4rjg4Pjg4HmlbDliJcKICAgIGRlZiBmaWIobik6CiAgICAgICAgZGVmIGcobik6CiAgICAgICAgICAgIG1hdGNoIG46CiAgICAgICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuICgwLCBbXSkKICAgICAgICAgICAgICAgIGNhc2UgMToKICAgICAgICAgICAgICAgICAgICByZXR1cm4gKDEsIFtdKQogICAgICAgICAgICAgICAgY2FzZSBuOgogICAgICAgICAgICAgICAgICAgIHJldHVybiAobiwgW24gLSAxLCBuIC0gMl0pCiAgICAgICAgcmV0dXJuIGZvbGRUcmVlKGxhbWJkYSB4LCB4czogeCBpZiB4cyA9PSBbXSBlbHNlIHN1bSh4cyksIHVuZm9sZFRyZWUoZywgbikpCgogICAgcHJpbnQoZmliKDUpKQo=
compilation info
Traceback (most recent call last):
File "/usr/lib/python3.9/py_compile.py", line 144, in compile
code = loader.source_to_code(source_bytes, dfile or file,
File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "./prog.py", line 4
match f(b):
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.9/py_compile.py", line 150, in compile
raise py_exc
py_compile.PyCompileError: File "./prog.py", line 4
match f(b):
^
SyntaxError: invalid syntax
stdout