# DGM Interview with Vito ### Fix the bugs in this function to count reachable nodes in a directed graph. ```typescript type Node<T> = { value: T, children: Node<T>[], } a === b // the same node in javascript const countNodes<T> = (seen: Node<T>[]) => (root: Node<T>): number => root.children .filter(x => x not in seen) .map(countNodes([...seen, root])) .reduce((sum, x) => sum + x, 1); A -> B; A -> C B -> D; C -> D a -> b; a -> c b -> d; c -> d ``` ```haskell countNodes :: [Node t] -> Node t -> Int ``` ```php class Node { public $value; public $children; // count how many nodes are reachable from this node including this node public countNodes($seen=[]) { $n = 0; $seen[] = $this; foreach($this->children as $child) { $n += $child->countNodes($seen); } return $n; } } ``` #### Test cases ```typescript const singleton: Node<null> = {value: null, children: []}; countNodes(singleton) === 1; ``` ```typescript const pair: Node<null> = {value: null, children: [singleton]}; countNodes(pair) === 2; ``` ```typescript const singleton_loop: Node<null> = {value: null, children: [singleton_loop]}; countNodes(singleton_loop) === 1; ``` ### Why doesn't 0.4 + 0.8 == 1.2? (in javascript, Java, PHP, C, etc.) ```haskell ``` ## What and What Languages liftA2 (+) x y x :: Applicative f => f Float ((+) <$> x <*> y) >>= (\a -> if a > 0 then pure a else empty) do clamp <- getdefalutclamp glassclamp <- getdefaultglassclamp Backend - Java, PHP, Haskell Frontends - Javascript and Typescript