title: SML : Count the number of '\n' in a String
body: <p>I'm quite new in Standard ML and I have a question that will probably be obvious to someone who knows how to code in this language.</p>
  
  <p>I've got an original function as follow : </p>
  
  <pre><code>fun getBlocked w =
  case BlockingMgr.listBlockedSuccessors w
      of nil =&gt; ""
      | ws =&gt;
          concat (
              List.map
                  (fn (r, w') =&gt; (
                      "v r1 w"
                      ^
                        Int.toString (Node.getId w))
                      ^ " w"
                      ^ (Int.toString (Node.getId (w')))
                      ^ "\n"
                  )
              ws
          )       
  </code></pre>
  
  <p>Which is returning me a string perfect to display in my program.</p>
  
  <p>Unfortunately, I need also the number of '\n' in this string. So instead of counting directly from the string (which would be nice) I thought about making another function which is returning an integer :</p>
  
  <pre><code>fun getNbBlocked w =
      let res = 0;
      case BlockingMgr.listBlockedSuccessors w
          of nil =&gt; res
          | ws =&gt;
              List.map
                  (fn (r, w') =&gt; (
                      res = res+1
                  )
              ws  
  </code></pre>
  
  <p>But my brain is too much into the procedural/object thinking and I don't know how to perform what I want in a functional language.</p>
  
  <p>If someone could help me to debug this function because I don't really know what is the problem :/ (or even better, helping me by telling me how to code a function to count the number of '\n' in the existing string)</p>
  
  <p>Thanks in advance for your help !</p>
  
  <p>Best Regards !</p>
  
  <h1>EDIT</h1>
  
  <p>@molbdnilo Your solution seems to be close to the solution, but I don't manage to use it :/ (I'm a true beginner unfortunately).</p>
  
  <p>I've got theses functions (the already existing one and yours) : </p>
  
  <pre><code>val res = 0
  
  fun getBlocked w =
      case BlockingMgr.listBlockedSuccessors w
          of nil =&gt; ""
          | ws =&gt;
              concat (
                  List.map
                      (fn (r, w') =&gt; (
                          "v r1 w"
                          ^
                          Int.toString (Node.getId w))
                          ^ " w"
                          ^ (Int.toString (Node.getId (w')))
                          ^ "\n"
                          )
                      ws
                  )       
  
  fun count c s = List.length (List.filter (fn x =&gt; x = c) (String.explode s));;
  
  fun count_newlines s = count #"\n" s;;
  
  fun af w = print(getBlocked w)
  
  fun aff w = count_newlines(getBlocked w)
  </code></pre>
  
  <p>When I'm calling the function getBlocked() in order to display the created string</p>
  
  <p>I'm doing as follow :</p>
  
  <pre><code>app af (Nodestore.listNodes ())
  </code></pre>
  
  <p>Because it's calling print, so it can be call anywhere.</p>
  
  <p>But for your function, I need to use the returned value of it and I don't manage to do it :/ </p>
  
  <p>I try something like : </p>
  
  <pre><code>res = (app aff Nodestore.listNodes())
  
  (* We then, display the number of edges *)
  print (Int.toString (res));
  </code></pre>
  
  <p>But it's unfortunately, not as simple as I thought :/ </p>
  
  <p>I get the error message : </p>
  
  <pre><code>Error: src/output/modeloutput.sml 101.11.
    Function applied to incorrect argument.
      expects: _ -&gt; [unit]
      but got: _ -&gt; [int]
      in: app aff
  Error: src/output/modeloutput.sml 101.11.
    Function applied to incorrect argument.
      expects: [Node.node list]
      but got: [unit -&gt; Node.node list]
      in: (app aff) Nodestore.listNodes
  Error: src/output/modeloutput.sml 101.11.
    Function not of arrow type.
      function: [unit]
      in: ((app aff) Nodestore.listNodes) ()
        pre codegen raised in 2.79 + 1.50 (35% GC)
        pre codegen raised: Fail
     Compile SML raised in 2.79 + 1.50 (35% GC)
     Compile SML raised: Fail
  </code></pre>