Another function in the same way is 'const :: a -> b -> a' (on haskell,
but easy to make in clojure if it doesn't exist) which
disregards the second argument and always returns the first.

This example is in haskell, but let's say that you're creating a
histogram, meaning that you go through a list of values, and increase
their value in a map. "Hello" would result in {'h' : 1, 'e' : 1, 'l' :
2, 'o' : 1}. You would reduce over the string, and add the key to the
map with a value of one. If the key already exists, you increase the old
value with one. The type signature for insertWith is '(a -> a -> a) -> k
-> a -> Map k a -> Map k a' meaning that it takes a function with two
parameters, a key, a value and a map and returns a new map. The two
parameters are new and old values of the key if the key existed
beforehand. We could give a function like (\a b -> succ b) which
increases the latter value (in clojure it would be #(inc %2)), or we
could give a 'const succ' which would do it automatically.

-- 
Mats Rauhala
MasseR

Attachment: pgprBfQtlq5Oi.pgp
Description: PGP signature

Reply via email to