【C?】最初の言語に何を選ぶか【Haskell?】at TECH
【C?】最初の言語に何を選ぶか【Haskell?】 - 暇つぶし2ch286:デフォルトの名無しさん
22/01/19 20:54:02.18 tV3PBnn6.net
でもカッコがある方が
エディタが助けてくれるからなあ
pythonとかタブの位置を合わせるのが大変

287:デフォルトの名無しさん
22/01/20 18:59:46.11 Ty/TufmK.net
# Make10面白いな Elixirの勉強がてらに思わず仕事さぼって全解探索作ってしまったわ
defmodule Make10 do
@nums 1..9 |> Enum.map(&{:num, &1, Integer.to_string(&1)})
@ops [{:plus, "+"}, {:minus, "-"}, {:mul, "*"}, {:div, "/"}]
|> Enum.map(&Tuple.insert_at(&1, 0, :calc))
@initial_stack []
@initial_usednumbers []
@initial_state [{@initial_stack, @initial_usednumbers}]
@accept_duplicated_number false
def do_action({stack, used}, action) do
case {stack, action} do
{_, {:num, val, symbol}} ->
if !@accept_duplicated_number and Enum.member?(used, val) do
{[{{val, 1}, "#{symbol}"} | stack], []}
else
{[{{val, 1}, "#{symbol}"} | stack], [val | used]}
end
{[{{c1_n, c1_d}, exp1} | [{{c2_n, c2_d}, exp2} | tail]], {:calc, op, symbol}} ->
expression = "(#{exp2}#{symbol}#{exp1})"
case op do
:plus -> {[{{c2_n * c1_d + c1_n * c2_d, c1_d * c2_d}, expression} | tail], used}
:minus -> {[{{c2_n * c1_d - c1_n * c2_d, c1_d * c2_d}, expression} | tail], used}
:mul -> {[{{c1_n * c2_n, c1_d * c2_d}, expression} | tail], used}
:div -> {[{{c2_n * c1_d, c2_d * c1_n}, expression} | tail], used}
end
end
end

288:デフォルトの名無しさん
22/01/20 19:00:29.87 Ty/TufmK.net
# 続きだよ
def action_candidates(stepcount_remain, {stack, _}) do
stack_depth = Enum.count(stack)
case stack do
[] -> @nums
[_ | []] -> @nums
_ when stack_depth > stepcount_remain -> @ops
_ -> @nums ++ @ops
end
end
def expand_each_state_node(stepcount_remain, state) do
action_candidates(stepcount_remain, state)
|> Enum.map(&do_action(state, &1))
|> Enum.filter(fn {stack, used} -> !Enum.empty?(used) end)
end
def step_one_action(stepcount_remain, statelist) do
statelist
|> Enum.map(&expand_each_state_node(stepcount_remain, &1))
|> List.flatten()
end
def run(num_of_cards \\ 4, {target_val_n, target_val_d} \\ {10, 1}) do
(num_of_cards * 2 - 1)..1
|> Enum.reduce(@initial_state, &step_one_action(&1, &2))
|> Enum.filter(fn {[{{lastval_n, lastval_d}, expression}], used} ->
lastval_n * target_val_d == target_val_n * lastval_d
end)
# |> Enum.each(fn {[{ {lastval_n,lastval_d} ,expression}] , used} -> IO.puts expression end)
end
end

289:デフォルトの名無しさん
22/01/20 19:02:16.35 Ty/TufmK.net
iex(96)> Make10.run
[
{[{{10, 1}, "(1+(2+(3+4)))"}], [4, 3, 2, 1]},
{[{{10, 1}, "(1*(2+(3+5)))"}], [5, 3, 2, 1]},
{[{{10, 1}, "(1-(2-(3+8)))"}], [8, 3, 2, 1]},
{[{{10, 1}, "(1+((2+3)+4))"}], [4, 3, 2, 1]},
{[{{10, 1}, "(1*((2+3)+5))"}], [5, 3, 2, 1]},
{[{{10, 1}, "((1+(2+3))+4)"}], [4, 3, 2, 1]},
{[{{10, 1}, "((1*(2+3))+5)"}], [5, 3, 2, 1]},
{[{{10, 1}, "(1-((2-3)-8))"}], [8, 3, 2, 1]},
{[{{10, 1}, "(1-((2-3)*9))"}], [9, 3, 2, 1]},
{[{{10, 1}, "((1-(2-3))*5)"}], [5, 3, 2, 1]},
{[{{10, 1}, "((1-(2-3))+8)"}], [8, 3, 2, 1]},
{[{{10, 1}, "(1*((2*3)+4))"}], [4, 3, 2, 1]},
{[{{10, 1}, "((1*(2*3))+4)"}], [4, 3, 2, 1]},
{[{{30, 3}, "((1+(2/3))*6)"}], [6, 3, 2, 1]},
{[{{10, 1}, "(1+(2+(4+3)))"}], [3, 4, 2, 1]},
{[{{20, 2}, "(1/(2/(4*5)))"}], [5, 4, 2, 1]},
{[{{10, 1}, "(1-(2-(4+7)))"}], [7, 4, 2, 1]},
・・・

290:デフォルトの名無しさん
22/01/20 19:05:30.69 Ty/TufmK.net
分数も対応(数字4つで129/8)
iex(102)> Make10.run(4, {129,8})
[
{[{{129, 8}, "((1/8)+(7+9))"}], [9, 7, 8, 1]},
{[{{129, 8}, "(((1/8)+7)+9)"}], [9, 7, 8, 1]},
{[{{129, 8}, "((1/8)+(9+7))"}], [7, 9, 8, 1]},
{[{{129, 8}, "(((1/8)+9)+7)"}], [7, 9, 8, 1]},
{[{{129, 8}, "((3*5)+(9/8))"}], [8, 9, 5, 3]},
{[{{129, 8}, "(3*(6-(5/8)))"}], [8, 5, 6, 3]},
{[{{129, 8}, "((5*3)+(9/8))"}], [8, 9, 3, 5]},
{[{{129, 8}, "((6-(5/8))*3)"}], [3, 8, 5, 6]},
{[{{129, 8}, "(7+((1/8)+9))"}], [9, 8, 1, 7]},
{[{{129, 8}, "((7+(1/8))+9)"}], [9, 8, 1, 7]},
{[{{129, 8}, "(7+(9+(1/8)))"}], [8, 1, 9, 7]},
{[{{129, 8}, "((7+9)+(1/8))"}], [8, 1, 9, 7]},
{[{{129, 8}, "(9+((1/8)+7))"}], [7, 8, 1, 9]},
{[{{129, 8}, "((9+(1/8))+7)"}], [7, 8, 1, 9]},
{[{{129, 8}, "(9+(7+(1/8)))"}], [8, 1, 7, 9]},
{[{{129, 8}, "((9+7)+(1/8))"}], [8, 1, 7, 9]},
{[{{129, 8}, "((9/8)+(3*5))"}], [5, 3, 8, 9]},
{[{{129, 8}, "((9/8)+(5*3))"}], [3, 5, 8, 9]}
]

291:デフォルトの名無しさん
22/01/20 19:13:42.14 Ty/TufmK.net
Prologで書いたら楽そうなところはなかったw

292:デフォルトの名無しさん
22/01/20 19:54:16.86 Ty/TufmK.net
ちなみにeval使わず分数計算自前だよ
Elixirも楽しい!

293:デフォルトの名無しさん
22/02/03 03:27:48.02 5caWIEV6.net
Prologって簡単な深さ優先・探索列挙はともかく動的に細かな探索の制御をしようと思うと途端に面倒になるからな
他の言語で書いた方が小回りきくわ、ってなる
実用上、組み合わせの少ない練習問題的な全解探索にしか使えないゆえん

294:デフォルトの名無しさん
22/02/03 10:21:28.73 lrjvvrdU.net
スッキリ書ける俺々プリプロセッサわんさかありそう

295:デフォルトの名無しさん
22/05/16 08:48:09.42 fgDPgjMn.net
Mbは死んだか


最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch