Permalink
Browse files

Add a few more examples to advantages.hs. Add SVG for advantages.hs.

1 parent 610ad9a commit a396ec7ce3e631fe65c56a1bdab4fbb9a313a36d @rgleichman committed Jan 7, 2017
Showing with 66 additions and 4 deletions.
  1. +63 −4 examples/advantages.hs
  2. +3 −0 examples/advantages.svg
@@ -1,7 +1,7 @@
-{-Advantages of Glance:
+{-Advantages of a Visual Representation of Code:
-The following are several admittedly contrived examples demonstrating some
-of the advantages of Glance over textual code.
+The following are several admittedly contrived examples demonstrating six
+of the advantages of displaying code visually instead of textually.
1. Display out of order arguments:
@@ -20,7 +20,7 @@ been swapped.
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
-}
badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a)
-{- In the Glance image of badSyntaxGraphToTuple, it is immediately apparent that
+{-In the Glance image of badSyntaxGraphToTuple, it is immediately apparent that
some of the tuple arguments are out of order.
@@ -37,6 +37,7 @@ y x = g where
g = v
to just
+
y x = 2 * x
-}
@@ -128,4 +129,62 @@ foo x y
in temp2
{-In future version of Glance, nested guards/cases could be rendered using a 2D grid.
+
+
+5. Information at a Glance:
+
+Let's look again at the factorial function from the README.
+
+factorial x =
+ if x == 0
+ then 1
+ else x * factorial (x - 1)
+-}
+factorial x =
+ if x == 0
+ then 1
+ else x * factorial (x - 1)
+{-In the visual version, it is quick and easy to see that the parameter (x)
+is used exactly three times in the body of the function. In the text,
+figuring out how many times a parameter is used requires searching through
+the entire function.
+
+Similarly, in the visual image, it is easy to see that "factorial" is recursively
+called exactly once.
+
+
+6. See the Topology of Code:
+
+Assume we have a function "mapFirst" that maps the first item in a tuple.
+
+mapFirst :: (a -> a) -> (a, a) -> (a, a)
+mapFirst f (x, y) = (x', y) where
+ x' = f x
+-}
+mapFirst f (x, y) = (x', y) where
+ x' = f x
+
+{-Now, for whatever reason, we want to write a version of mapFirst where the items
+in the result tuple are swapped like so:
+mapFirstAndSwap f (x, y) = (y, x') where
+ x' = f x
+
+But we make a mistake and accidentally write:
+badMapFirstAndSwap f (x, y) = (x', x') where
+ x' = f x
+
+When we visualize badMapFirstAndSwap we can immediately see that (f x)
+is no longer nested, and has become an additional node in the graph.
+
+Since swapping the values in a tuple should not change the topology of our code,
+this extra node indicates there is an error in the code.
+-}
+badMapFirstAndSwap f (x, y) = (x', x') where
+ x' = f x
+
+{-
+If you were curious, here is what the good version of mapFirstAndSwap
+looks like.
-}
+mapFirstAndSwap f (x, y) = (y, x') where
+ x' = f x
Oops, something went wrong.

0 comments on commit a396ec7

Please sign in to comment.