tag:blogger.com,1999:blog-5964816804623588850.post1748449069422285907..comments2024-03-15T00:31:24.817-07:00Comments on Digithead's Lab Notebook: Scala tutorialChristopher Barehttp://www.blogger.com/profile/01570188379488941406noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5964816804623588850.post-90654412838043994532010-02-24T16:57:30.530-08:002010-02-24T16:57:30.530-08:00Stupid me, forgot about Const(0). I like the eleg...Stupid me, forgot about Const(0). I like the elegance of your code, Martin :)Unknownhttps://www.blogger.com/profile/00481093782039815284noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-20055114291952112352010-02-24T16:26:16.397-08:002010-02-24T16:26:16.397-08:00Very nice to btsai and Martin... both are much cle...Very nice to btsai and Martin... both are much cleaner than my awkward attempt.Christopher Barehttps://www.blogger.com/profile/01570188379488941406noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-57432006862222669792010-02-24T15:50:38.096-08:002010-02-24T15:50:38.096-08:00This avoid simplifying the subtrees twice and also...This avoid simplifying the subtrees twice and also catches the cases where one side of the Sum is zero:<br /><br /> def simplify(t: Tree): Tree = t match {<br /> case Sum(l, r) => {<br /> (simplify(l), simplify(r)) match {<br /> case (Const(0.), y) => y<br /> case (x, Const(0.)) => x<br /> case (Const(x), Const(y)) => Const(x + y)<br /> case (x, y) => Sum(x, y)<br /> }<br /> }<br /> case t => t<br /> }Unknownhttps://www.blogger.com/profile/05118464016313645817noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-14189448013338278292010-02-11T11:25:36.789-08:002010-02-11T11:25:36.789-08:00Just realized it's kind of silly to simplify e...Just realized it's kind of silly to simplify each subtree twice.<br /><br />def simplify(t:Tree):Tree =<br /> t match {<br /> case Sum(l, r) =><br /> val sl:Tree = simplify(l)<br /> val sr:Tree = simplify(r)<br /> (sl, sr) match {<br /> case (Const(a), Const(b)) => Const(a + b)<br /> case _ => Sum(sl, sr)<br /> }<br /> case _ => t<br /> }Unknownhttps://www.blogger.com/profile/00481093782039815284noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-1342595816709305182010-02-11T11:18:02.831-08:002010-02-11T11:18:02.831-08:00I think this also works:
def simplify(t:Tree):Tre...I think this also works:<br /><br />def simplify(t:Tree):Tree =<br /> t match {<br /> case Sum(l, r) => <br /> (simplify(l), simplify(r)) match {<br /> case (Const(a), Const(b)) => Const(a + b)<br /> case _ => Sum(simplify(l), simplify(r))<br /> }<br /> case _ => t<br /> }Unknownhttps://www.blogger.com/profile/00481093782039815284noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-31529546119933233072009-01-16T18:52:00.000-08:002009-01-16T18:52:00.000-08:00Thanks yardus, I like it. It didn't occur to me to...Thanks yardus, I like it. It didn't occur to me to let the cases fall through like that. Maybe there's a pattern matching syntax that would match the <B><I>Const(0)</I></B> cases, I don't know.Christopher Barehttps://www.blogger.com/profile/01570188379488941406noreply@blogger.comtag:blogger.com,1999:blog-5964816804623588850.post-54484422904652155842009-01-11T05:39:00.000-08:002009-01-11T05:39:00.000-08:00A probably more functional version of simplify (my...A probably more functional version of <B>simplify</B> (my first attempt at Scala):<BR/><BR/><B><BR/>def simplify(t: Tree): Tree = t match {<BR/> case Sum(l, r) if (l == Const(0)) => simplify(r);<BR/> case Sum(l, r) if (r == Const(0)) => simplify(l);<BR/> case Sum(l, r) if (l == Const(0) && r == Const(0)) => Const(0);<BR/> case Sum(l: Const, r: Const) => Const(l.v + r.v);<BR/> case Sum(l, r) if (simplify(l) == Const(0) || simplify(r) == Const(0)) => simplify(Sum(simplify(l), simplify(r)));<BR/> case Sum(l, r) => Sum(simplify(l), simplify(r));<BR/> case _ => t;<BR/> }<BR/></B>Enthusiasthttps://www.blogger.com/profile/07800148689847158802noreply@blogger.com