Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Aha !

Now in,

    while m+i is less than the length of S, do:
        if W[i] = S[m + i],
            if i equals the (length of W)-1,
                return m
            let i ← i + 1
            ....
How would I implement the `return m` part ?


There is no explicit return value because the functions aren't functions: they are values, thus the result of the algorithm is bound to the value that is bound to the function.

When you write:

(defn square [x]

     (* x x))
You are really writing:

(def square

    (fn [x]

        (* x x)))
and x * x is bound to the value of square. There would be no logical reason to have an explicit return value here.


It's pretty much the same as this:

    (loop [n 0]
      (if (= n 10)
        n                 ; true case: return n
        (recur (inc n)))) ; false case: loop with n++

    ;=> 10




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: