Skip to content

Commit a8e5299

Browse files
committed
additional exercise in ch9
1 parent 07889d8 commit a8e5299

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Exercises/Chapter09/Exercises.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
using NUnit.Framework;
2+
using System;
3+
14
using LaYumba.Functional;
25
using LaYumba.Functional.Data.LinkedList;
36
using static LaYumba.Functional.Data.LinkedList.LinkedList;
47

5-
using NUnit.Framework;
6-
using System;
8+
using LaYumba.Functional.Data.BinaryTree;
9+
using static LaYumba.Functional.Data.BinaryTree.Tree;
710

811
namespace Exercises.Chapter9
912
{
1013
static class Exercises
1114
{
12-
// LISTS: implement functions to work with the singly linked List defined in this chapter:
15+
// LISTS
16+
17+
// Implement functions to work with the singly linked List defined in this chapter:
1318
// Tip: start by writing the function signature in arrow-notation
1419

1520
// InsertAt inserts an item at the given index
@@ -28,13 +33,24 @@ static class Exercises
2833
// DropWhile:
2934

3035
// number of new objects required:
36+
// InsertAt:
37+
// RemoveAt:
38+
// TakeWhile:
39+
// DropWhile:
3140

3241
// TakeWhile and DropWhile are useful when working with a list that is sorted
3342
// and you’d like to get all items greater/smaller than some value; write implementations
3443
// that take an IEnumerable rather than a List
3544

3645

37-
// TREES: Implement a LabelTree type, where each node has a label of type string and a list of subtrees;
46+
// TREES
47+
48+
// Is it possible to define `Bind` for the binary tree implementation shown in this
49+
// chapter? If so, implement `Bind`, else explain why it’s not possible (hint: start by writing
50+
// the signature; then sketch binary tree and how you could apply a tree-returning funciton to
51+
// each value in the tree).
52+
53+
// Implement a LabelTree type, where each node has a label of type string and a list of subtrees;
3854
// this could be used to model a typical navigation tree or a cateory tree in a website
3955

4056
// Imagine you need to add localization to your navigation tree: you're given a `LabelTree` where

Exercises/Chapter09/Solutions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using LaYumba.Functional.Data.LinkedList;
66
using static LaYumba.Functional.Data.LinkedList.LinkedList;
77

8+
using LaYumba.Functional.Data.BinaryTree;
9+
using static LaYumba.Functional.Data.BinaryTree.Tree;
10+
811
namespace Exercises.Chapter9.Solutions
912
{
1013
static class Solutions
@@ -55,6 +58,16 @@ static List<T> DropWhile<T>(this List<T> @this, Func<T, bool> pred)
5558

5659
// Answer: see LaYumba.Functional.EnumerableExt
5760

61+
62+
// Is it possible to define `Bind` for the binary tree implementation shown in this
63+
// chapter? If so, implement `Bind`:
64+
65+
static Tree<R> Bind<T, R>(this Tree<T> tree, Func<T, Tree<R>> f)
66+
=> tree.Match(
67+
Leaf: f,
68+
Branch: (l, r) => Branch(l.Bind(f), r.Bind(f))
69+
);
70+
5871
}
5972

6073
// Implement a LabelTree type, where each node has a label of type string and a list of subtrees;

0 commit comments

Comments
 (0)