Skip to content

Instantly share code, notes, and snippets.

@Dubhead
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dubhead/a51904c9e55908c747d0 to your computer and use it in GitHub Desktop.
Save Dubhead/a51904c9e55908c747d0 to your computer and use it in GitHub Desktop.
「ソフトウェアエンジニアならば1時間以内に解けなければいけない5つの問題」の5問目を Pure で解いてみた
数式の文字列を eval できる言語ならどれでも大差なく解けそうなもの。
というわけで Pure (http://purelang.bitbucket.org/) で解いてみた。
#!/usr/bin/pure
using system; // for 'puts'
add100 acc 9
= puts acc if eval acc == 100;
= _ otherwise;
add100 acc n = map f ["+", "-", ""]
with f s = add100 (acc + s + str(n+1)) (n+1);
end;
add100 "1" 1;
// eof
実行結果:
% ./add100.pure
1+2+3-4+5+6+78+9
1+2+34-5+67-8+9
1+23-4+5+6+78-9
1+23-4+56+7+8+9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
123+4-5+67-89
123+45-67+8-9
123-4-5-6-7+8-9
123-45-67+89
%
@Dubhead
Copy link
Author

Dubhead commented May 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment