05.03.2016 Views

Programming in Scala”

fpiscompanion

fpiscompanion

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter notes 10<br />

data structures, both classic and new, from the perspective of functional programm<strong>in</strong>g. We highly<br />

recommend pick<strong>in</strong>g up this book if you’re <strong>in</strong>terested <strong>in</strong> data structures. The dissertation that the<br />

book is based on is also available from Carnegie Mellon University’s website³⁶.<br />

Rose trees<br />

The tree structure that we <strong>in</strong>troduce at the end of the chapter is called a Rose Tree³⁷. It is a nonempty<br />

multi-way tree that conta<strong>in</strong>s data at the nodes rather than at the leaves.<br />

The algebra of data types<br />

The “algebraic” <strong>in</strong> algebraic data types means someth<strong>in</strong>g specific. This is a reference to the fact that<br />

such data types are composed of sums and products of other types. More specifically, data types<br />

form a sem<strong>in</strong>earr<strong>in</strong>g³⁸.<br />

See the follow<strong>in</strong>g l<strong>in</strong>ks:<br />

• The Algebra of Algebraic Data Types³⁹ by Chris Taylor.<br />

• Species and Functors and Types, Oh My!⁴⁰ by Brent Yorgey<br />

• Clowns to the left of me, jokers to the right⁴¹ by Conor McBride<br />

Zippers<br />

S<strong>in</strong>ce an algebraic data type is a type-level function <strong>in</strong>volv<strong>in</strong>g sums and products, we can take the<br />

derivative of such a function, yield<strong>in</strong>g a data structure called a zipper⁴². The zipper for a given<br />

structure is like the orig<strong>in</strong>al structure, but with a movable “focus” or po<strong>in</strong>ter <strong>in</strong>to the structure. This<br />

can be used to <strong>in</strong>sert, remove, and modify elements under the focus.<br />

For example, a list zipper⁴³ consists of one element under focus together with two lists: one<br />

enumerat<strong>in</strong>g all the elements to the left of the focus, and another enumerat<strong>in</strong>g all the elements to<br />

the right of the focus. The focus can me moved left or right (like a zipper on a piece of cloth<strong>in</strong>g) and<br />

elements will move through the focus. The element under focus can then be removed or modified,<br />

and we can <strong>in</strong>sert a new element by cons<strong>in</strong>g onto the lists to its left or right.<br />

³⁶http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf<br />

³⁷http://en.wikipedia.org/wiki/Rose_tree<br />

³⁸http://en.wikipedia.org/wiki/Near-semir<strong>in</strong>g<br />

³⁹http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/<br />

⁴⁰http://www.cis.upenn.edu/~byorgey/papers/species-pearl.pdf<br />

⁴¹http://personal.cis.strath.ac.uk/~conor/Dissect.pdf<br />

⁴²http://en.wikipedia.org/wiki/Zipper_%28data_structure%29<br />

⁴³http://eed3si9n.com/learn<strong>in</strong>g-scalaz/Zipper.html

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!