[Haskell-beginners] Resources to learn functional programming

Alexander Bernauer alex-haskell at copton.net
Thu Aug 2 16:19:01 CEST 2012


On Wed, Aug 01, 2012 at 05:53:36PM -0300, Homero Cardoso de Almeida wrote:
> I'm trying to learn it, but got stuck when i reached high-order functions.
> [...]
> I am a decent C++ programmer.

The C++ analogy is as follows: a high-order function is a function that
takes a parameter of a type that has an operator() defined (or returns
a value of such a type).

For example, find_if [1] is such a "high-order function".
---8<---
struct T { int i; };

class Predicate {
public:
   bool operator()(const T& t) { return t.i == 23; }
};

std::vector<T> ts;

bool has23() {
    Predicate pred;
    return find_if(ts.begin(), ts.end(), pred) != ts.end();
}
--->8---

In Haskell the analogous example would be:
---8<---
data T = T Int

pred :: T -> Bool
pred (T i) = i == 23

ts :: [T]

has23 :: Bool
has23 = isJust $ find pred ts
--->8---

HTH

Alex

[1] http://www.sgi.com/tech/stl/find_if.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120802/09244bcf/attachment.pgp>


More information about the Beginners mailing list