開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の3章(型を定義し、関数を単純化する)、3.13(ガードの条件節の評価)、練習問題8.をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
練習問題8.
コード
sample.dart
import 'dart:html'; void main(){ Tree tree1 = new Tree(null, null, null); var tree2 = new Tree("x", null, null); var tree3 = new Tree("x", null, new Tree("y", null, null)); List<Tree> trees = [tree1, tree2, tree3]; run.onClick.listen((MouseEvent event){ pre.text = window.navigator.userAgent + '\n'; for(var tree in trees){ pre.text += '$tree 高さ: ${tree.height()}\n'; } }); clear.onClick.listen((MouseEvent event) => pre.text = ''); } ButtonElement run = querySelector('#run_dart'); ButtonElement clear = querySelector('#clear'); PreElement pre = querySelector('#pre0'); class Tree{ var a; Tree left; Tree right; Tree(this.a, this.left, this.right); int height(){ if(a == null){ return 0; } if(left == null && right == null){ return 1; } if(left == null){ return 1 + right.height(); } if(right == null){ return 1 + left.height(); } return 1 + (left.height() > right.height() ? left.height() : right.height()); } String toString(){ if(a == null){ return "Empty"; } return 'Node $a ($left) ($right)'; } }
0 コメント:
コメントを投稿