I was trying to implement some Graph related functions in Functional form and came accross this result which I found surprising.

I want a function that does various operations on a graph.

The simplest operation is Identity which could simply count the number of nodes in the graph.

Somehow this function does exactly that.

Fold[1 + #&][1,{a,b,c,d}]



And the corollary result which shows how it works:

FoldList[1 + #&][1,{a,b,c,d}]


{1, 2, 3, 4, 5}

The two questions that arise are:

  1. Can this result be explained in an intuitive fashion ?
  2. Can this idea be used to perform more complex operations on the graph ?
  • $\begingroup$ Did you forget a }? Or did you have a { too much? $\endgroup$ – C. E. Dec 22 '18 at 21:25
  • 1
    $\begingroup$ FoldList >> Properties and Relations : Functions that ignore their second argument give the same result as in NestList. Similarly for Fold. $\endgroup$ – kglr Dec 22 '18 at 21:40
  • $\begingroup$ @C.E. Thanks for pointing this out. I removed the superfluous {. $\endgroup$ – v1j4y Dec 23 '18 at 0:33
  • $\begingroup$ @kglr Ah yes, reading the docs. Thanks a lot. I learned my first lesson the hard way ! Althouth this is such an obfuscated way to count the length of a list :-P $\endgroup$ – v1j4y Dec 23 '18 at 0:36