↓ Skip to main content

Fold

Datatypes and Folds: Part II

·5 mins

Non-mutually re­curs­ive and Mu­tu­ally re­curs­ive data­types Wel­come back. In this post we will look at cre­at­ing type algebra's and folds for more com­plic­ated data types. In es­sence, this ex­er­cise will not be any more dif­fi­cult than the pre­vi­ous ones, provided that you stick with the steps. Up in this part, are the non-mutually re­curs­ive data­types and mu­tu­ally re­curs­ive data­types. First up, data struc­tures that are re­curs­ive into other data struc­tures. The folds for these data struc­tures are no more com­plex than their `simpler’ forms. In fact, you could treat them the same, but for clar­ity we will treat them as dis­tinct. If you want to know more about the pre­vi­ous part, it might help your search if you look for the keyword cata­morph­ism.

Haskell Datatypes and Folds: Part I

·9 mins

Wel­come to this little ex­plan­a­tion on how to de­term­ine the fold of a Haskell data­type. First we’ll look at how we define func­tions over lists, some­thing every­one start­ing with Haskell should be suf­fi­ciently fa­mil­iar with, after which we move on to the data­types. You’ll see dif­fer­ent ways how to cal­cu­late the sum of a list, how to fold over a list, what data­types are and, how to fold over a data­type, spe­cific­ally the BinTree a data­type. Most im­port­antly, I hope you will grow to un­der­stand what a fold is, and why they are so im­port­ant and use­ful when pro­gram­ming Haskell.