Skip to content
Browse files

Better C++ example

  • Loading branch information...
1 parent 4174378 commit 14b568150c380c1098a1c4754ffae2ff02801e2a @yogsototh committed Apr 10, 2012
View
62 content/html/en/blog/Haskell-the-Hard-Way.md
@@ -1382,12 +1382,11 @@ y = complex_square(x);
For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates[^022001]:
-
-[^022001]: I know there is a cleaner way, to do this in C++. This is only an example.
+In C++ there is a better way, the C++ templates:
<code class="c++">
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -1396,51 +1395,28 @@ T square(T x)
return x*x;
}
-// An example of usage:
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
</code>
-Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex
-
-<code class="c++">
-// Just an example
-class Complex
-{
-private:
- double real_;
- double img_;
-
-public:
- Complex(double real, double img) : real_(real),
- img_(img) {}
-
- // overloaded the multiplication operator
- Complex operator*(Complex z) {
- return Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</code>
-
-To be fair, there is also a definition of the multiplication of `Complex` in Haskell.
-But it takes only one line.
-Somewhere in the source of the module `Data.Complex`:
+C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+[this article](http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/)
+for example.
-<code class="haskell">
-instance Num (Complex a) where
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</code>
+In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.
The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
View
62 content/html/fr/blog/Haskell-the-Hard-Way.md
@@ -1388,12 +1388,11 @@ y = complex_square(x);
For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates[^022001]:
-
-[^022001]: I know there is a cleaner way, to do this in C++. This is only an example.
+In C++ there is a better way, the C++ templates:
<code class="c++">
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -1402,51 +1401,28 @@ T square(T x)
return x*x;
}
-// An example of usage:
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
</code>
-Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex
-
-<code class="c++">
-// Just an example
-class Complex
-{
-private:
- double real_;
- double img_;
-
-public:
- Complex(double real, double img) : real_(real),
- img_(img) {}
-
- // overloaded the multiplication operator
- Complex operator*(Complex z) {
- return Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</code>
-
-To be fair, there is also a definition of the multiplication of `Complex` in Haskell.
-But it takes only one line.
-Somewhere in the source of the module `Data.Complex`:
+C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+[this article](http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/)
+for example.
-<code class="haskell">
-instance Num (Complex a) where
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</code>
+In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.
The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
View
62 multi/blog/Haskell-the-Hard-Way.md
@@ -1441,12 +1441,11 @@ y = complex_square(x);
For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates[^022001]:
-
-[^022001]: I know there is a cleaner way, to do this in C++. This is only an example.
+In C++ there is a better way, the C++ templates:
<code class="c++">
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -1455,51 +1454,28 @@ T square(T x)
return x*x;
}
-// An example of usage:
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
</code>
-Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex
-
-<code class="c++">
-// Just an example
-class Complex
-{
-private:
- double real_;
- double img_;
-
-public:
- Complex(double real, double img) : real_(real),
- img_(img) {}
-
- // overloaded the multiplication operator
- Complex operator*(Complex z) {
- return Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</code>
-
-To be fair, there is also a definition of the multiplication of `Complex` in Haskell.
-But it takes only one line.
-Somewhere in the source of the module `Data.Complex`:
+C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+[this article](http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/)
+for example.
-<code class="haskell">
-instance Num (Complex a) where
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</code>
+In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.
The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
View
62 output/Scratch/en/blog/Haskell-the-Hard-Way/code/02_Hard_Part/20_Types.lhs
@@ -70,12 +70,11 @@ y = complex_square(x);
For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates[^022001]:
-
-[^022001]: I know there is a cleaner way, to do this in C++. This is only an example.
+In C++ there is a better way, the C++ templates:
<code class="c++">
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -84,51 +83,28 @@ T square(T x)
return x*x;
}
-// An example of usage:
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
</code>
-Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex
-
-<code class="c++">
-// Just an example
-class Complex
-{
-private:
- double real_;
- double img_;
-
-public:
- Complex(double real, double img) : real_(real),
- img_(img) {}
-
- // overloaded the multiplication operator
- Complex operator*(Complex z) {
- return Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</code>
-
-To be fair, there is also a definition of the multiplication of `Complex` in Haskell.
-But it takes only one line.
-Somewhere in the source of the module `Data.Complex`:
+C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+[this article](http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/)
+for example.
-<code class="haskell">
-instance Num (Complex a) where
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</code>
+In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.
The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
View
15 output/Scratch/en/blog/Haskell-the-Hard-Way/code/02_Hard_Part/type_inference.cpp
@@ -1,4 +1,5 @@
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -8,9 +9,13 @@ T square(T x)
}
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
View
71 output/Scratch/en/blog/Haskell-the-Hard-Way/index.html
@@ -1517,10 +1517,11 @@ <h4 id="type-inference">Type inference</h4>
<p>For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates<sup id="fnref:022001"><a href="#fn:022001" rel="footnote">5</a></sup>:</p>
+In C++ there is a better way, the C++ templates:</p>
<pre class="twilight">
-<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&quot;</span>Complex.h<span class="String">&quot;</span></span></span>
+<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&lt;</span>iostream<span class="String">&gt;</span></span></span>
+<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&lt;</span>complex<span class="String">&gt;</span></span></span>
<span class="Keyword">using</span> <span class="Keyword">namespace</span> std;
<span class="Storage">template</span>&lt;<span class="Storage">typename</span> T&gt;
@@ -1529,51 +1530,28 @@ <h4 id="type-inference">Type inference</h4>
<span class="Keyword">return</span> x*x;
}
-<span class="Comment"><span class="Comment">//</span> An example of usage:</span>
<span class="Storage">int</span> <span class="Entity">ma<span class="Entity">in</span></span>() {
- cout &lt;&lt; square&lt;<span class="Storage">int</span>&gt;(<span class="Constant">5</span>) &lt;&lt; endl;
- cout &lt;&lt; square&lt;<span class="Storage">double</span>&gt;(<span class="Constant">5.3</span>) &lt;&lt; endl;
- Complex z=Complex(<span class="Constant">5</span>,<span class="Constant">3</span>);
- cout &lt;&lt; square&lt;Complex&gt;(z) &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> int</span>
+ <span class="Storage">int</span> sqr_of_five = square(<span class="Constant">5</span>);
+ cout &lt;&lt; sqr_of_five &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> double</span>
+ cout &lt;&lt; (<span class="Storage">double</span>)square(<span class="Constant">5.3</span>) &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> complex</span>
+ cout &lt;&lt; square( complex&lt;<span class="Storage">double</span>&gt;(<span class="Constant">5</span>,<span class="Constant">3</span>) )
+ &lt;&lt; endl;
<span class="Keyword">return</span> <span class="Constant">0</span>;
}
</pre>
-<p>Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex</p>
+<p>C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+<a href="http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/">this article</a>
+for example.</p>
-<pre class="twilight">
-<span class="Comment"><span class="Comment">//</span> Just an example</span>
-<span class="Storage">class</span> Complex
-{
-<span class="Storage">private</span>:
- <span class="Storage">double</span> real_;
- <span class="Storage">double</span> img_;
-
-<span class="Storage">public</span>:
- <span class="Entity">Complex</span>(<span class="Storage">double</span> real, <span class="Storage">double</span> img) : real_(real),
- <span class="Entity">img_</span>(img) {}
-
- <span class="Comment"><span class="Comment">//</span> overloaded the multiplication operator</span>
- Complex <span class="Entity">operat<span class="Entity">or</span>*</span>(Complex z) {
- <span class="Keyword">return</span> Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</pre>
-
-<p>To be fair, there is also a definition of the multiplication of <code>Complex</code> in Haskell.
-But it takes only one line.
-Somewhere in the source of the module <code>Data.Complex</code>:</p>
-
-<pre class="twilight">
-<span class="Keyword">instance</span> <span class="Constant">Num</span> (<span class="Constant">Complex</span> a) <span class="Keyword">where</span>
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</pre>
+<p>In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.</p>
<p>The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
@@ -2416,7 +2394,7 @@ <h3 id="deal-with-io">Deal With IO</h3>
<p>This is a nice way to tell there was an error while trying to create/compute
a value.
The <code>maybeRead</code> function is a great example of this.
-This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">6</a></sup>,
+This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">5</a></sup>,
but if something goes wrong the returned value is <code>Nothing</code>.
If the value is right, it returns <code>Just &lt;the value&gt;</code>.
Don&rsquo;t try to understand too much of this function.
@@ -2649,10 +2627,10 @@ <h3 id="io-trick-explained">IO trick explained</h3>
<p>Not all function could have access to this variable.
Those who have access to this variable can potentially be impure.
-Functions whose the world variable isn&rsquo;t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">7</a></sup>.</p>
+Functions whose the world variable isn&rsquo;t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">6</a></sup>.</p>
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
-But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">8</a></sup>:</p>
+But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">7</a></sup>:</p>
<pre class="twilight">
<span class="Entity">main</span> :: <span class="Constant">World</span> -&gt; ((),<span class="Constant">World</span>)
@@ -3281,7 +3259,7 @@ <h4 id="the-list-monad">The list monad</h4>
</ul>
<p>If you have followed me until here, then you&rsquo;ve done it!
-You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">9</a></sup>!</p>
+You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">8</a></sup>!</p>
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
@@ -3644,9 +3622,6 @@ <h3 id="more-on-infinite-tree">More on Infinite Tree</h3>
<li id="fn:0216">
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">&#8617;</a></p>
</li>
- <li id="fn:022001">
- <p>I know there is a cleaner way, to do this in C++. This is only an example.<a href="#fnref:022001" rel="reference">&#8617;</a></p>
- </li>
<li id="fn:1">
<p>Which itself is very similar to the javascript <code>eval</code> on a string containing JSON).<a href="#fnref:1" rel="reference">&#8617;</a></p>
</li>
View
2 output/Scratch/en/index.html
@@ -103,7 +103,7 @@
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Copyright ©, Yann Esposito</a>
</div>
<div id="lastmod">
- Modified: 04/09/2012
+ Modified: 04/10/2012
</div>
<div>
Entirely done with
View
62 output/Scratch/fr/blog/Haskell-the-Hard-Way/code/02_Hard_Part/20_Types.lhs
@@ -70,12 +70,11 @@ y = complex_square(x);
For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates[^022001]:
-
-[^022001]: I know there is a cleaner way, to do this in C++. This is only an example.
+In C++ there is a better way, the C++ templates:
<code class="c++">
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -84,51 +83,28 @@ T square(T x)
return x*x;
}
-// An example of usage:
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
</code>
-Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex
-
-<code class="c++">
-// Just an example
-class Complex
-{
-private:
- double real_;
- double img_;
-
-public:
- Complex(double real, double img) : real_(real),
- img_(img) {}
-
- // overloaded the multiplication operator
- Complex operator*(Complex z) {
- return Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</code>
-
-To be fair, there is also a definition of the multiplication of `Complex` in Haskell.
-But it takes only one line.
-Somewhere in the source of the module `Data.Complex`:
+C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+[this article](http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/)
+for example.
-<code class="haskell">
-instance Num (Complex a) where
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</code>
+In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.
The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
View
15 output/Scratch/fr/blog/Haskell-the-Hard-Way/code/02_Hard_Part/type_inference.cpp
@@ -1,4 +1,5 @@
-#include "Complex.h"
+#include <iostream>
+#include <complex>
using namespace std;
template<typename T>
@@ -8,9 +9,13 @@ T square(T x)
}
int main() {
- cout << square<int>(5) << endl;
- cout << square<double>(5.3) << endl;
- Complex z=Complex(5,3);
- cout << square<Complex>(z) << endl;
+ // int
+ int sqr_of_five = square(5);
+ cout << sqr_of_five << endl;
+ // double
+ cout << (double)square(5.3) << endl;
+ // complex
+ cout << square( complex<double>(5,3) )
+ << endl;
return 0;
}
View
71 output/Scratch/fr/blog/Haskell-the-Hard-Way/index.html
@@ -1524,10 +1524,11 @@ <h4 id="type-inference">Type inference</h4>
<p>For each type, you need to write a new function.
The only way to work around this problem is to use some meta-programming trick.
For example using the pre-processor.
-In C++ there is a better way, the C++ templates<sup id="fnref:022001"><a href="#fn:022001" rel="footnote">5</a></sup>:</p>
+In C++ there is a better way, the C++ templates:</p>
<pre class="twilight">
-<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&quot;</span>Complex.h<span class="String">&quot;</span></span></span>
+<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&lt;</span>iostream<span class="String">&gt;</span></span></span>
+<span class="CCCPreprocessorLine">#<span class="CCCPreprocessorDirective">include</span> <span class="String"><span class="String">&lt;</span>complex<span class="String">&gt;</span></span></span>
<span class="Keyword">using</span> <span class="Keyword">namespace</span> std;
<span class="Storage">template</span>&lt;<span class="Storage">typename</span> T&gt;
@@ -1536,51 +1537,28 @@ <h4 id="type-inference">Type inference</h4>
<span class="Keyword">return</span> x*x;
}
-<span class="Comment"><span class="Comment">//</span> An example of usage:</span>
<span class="Storage">int</span> <span class="Entity">ma<span class="Entity">in</span></span>() {
- cout &lt;&lt; square&lt;<span class="Storage">int</span>&gt;(<span class="Constant">5</span>) &lt;&lt; endl;
- cout &lt;&lt; square&lt;<span class="Storage">double</span>&gt;(<span class="Constant">5.3</span>) &lt;&lt; endl;
- Complex z=Complex(<span class="Constant">5</span>,<span class="Constant">3</span>);
- cout &lt;&lt; square&lt;Complex&gt;(z) &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> int</span>
+ <span class="Storage">int</span> sqr_of_five = square(<span class="Constant">5</span>);
+ cout &lt;&lt; sqr_of_five &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> double</span>
+ cout &lt;&lt; (<span class="Storage">double</span>)square(<span class="Constant">5.3</span>) &lt;&lt; endl;
+ <span class="Comment"><span class="Comment">//</span> complex</span>
+ cout &lt;&lt; square( complex&lt;<span class="Storage">double</span>&gt;(<span class="Constant">5</span>,<span class="Constant">3</span>) )
+ &lt;&lt; endl;
<span class="Keyword">return</span> <span class="Constant">0</span>;
}
</pre>
-<p>Even with this example of C++ templates you are forced to write the type on the call of the function.
-Not to mention the definiton of the class Complex</p>
+<p>C++ does a far better job than C.
+For more complex function the syntax can be hard to follow:
+look at
+<a href="http://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/">this article</a>
+for example.</p>
-<pre class="twilight">
-<span class="Comment"><span class="Comment">//</span> Just an example</span>
-<span class="Storage">class</span> Complex
-{
-<span class="Storage">private</span>:
- <span class="Storage">double</span> real_;
- <span class="Storage">double</span> img_;
-
-<span class="Storage">public</span>:
- <span class="Entity">Complex</span>(<span class="Storage">double</span> real, <span class="Storage">double</span> img) : real_(real),
- <span class="Entity">img_</span>(img) {}
-
- <span class="Comment"><span class="Comment">//</span> overloaded the multiplication operator</span>
- Complex <span class="Entity">operat<span class="Entity">or</span>*</span>(Complex z) {
- <span class="Keyword">return</span> Complex(
- real_*z.real_ - img_*z.img_,
- img_*z.real_ + real_*z.img_);
- }
-
- ...
-</pre>
-
-<p>To be fair, there is also a definition of the multiplication of <code>Complex</code> in Haskell.
-But it takes only one line.
-Somewhere in the source of the module <code>Data.Complex</code>:</p>
-
-<pre class="twilight">
-<span class="Keyword">instance</span> <span class="Constant">Num</span> (<span class="Constant">Complex</span> a) <span class="Keyword">where</span>
- ...
- (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
- ...
-</pre>
+<p>In C++ you must declare that a function can work with different types.
+In Haskell this is the opposite.
+The function will be as general as possible by default.</p>
<p>The inference of type gives Haskell a feeling of the freedom that dynamic
typed languages provide.
@@ -2423,7 +2401,7 @@ <h3 id="deal-with-io">Deal With IO</h3>
<p>This is a nice way to tell there was an error while trying to create/compute
a value.
The <code>maybeRead</code> function is a great example of this.
-This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">6</a></sup>,
+This is a function similar to the function <code>read</code><sup id="fnref:1"><a href="#fn:1" rel="footnote">5</a></sup>,
but if something goes wrong the returned value is <code>Nothing</code>.
If the value is right, it returns <code>Just &lt;the value&gt;</code>.
Don&rsquo;t try to understand too much of this function.
@@ -2656,10 +2634,10 @@ <h3 id="io-trick-explained">IO trick explained</h3>
<p>Not all function could have access to this variable.
Those who have access to this variable can potentially be impure.
-Functions whose the world variable isn&rsquo;t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">7</a></sup>.</p>
+Functions whose the world variable isn&rsquo;t provided to should be pure<sup id="fnref:032001"><a href="#fn:032001" rel="footnote">6</a></sup>.</p>
<p>Haskell consider the state of the world is an input variable for <code>main</code>.
-But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">8</a></sup>:</p>
+But the real type of main is closer to this one<sup id="fnref:032002"><a href="#fn:032002" rel="footnote">7</a></sup>:</p>
<pre class="twilight">
<span class="Entity">main</span> :: <span class="Constant">World</span> -&gt; ((),<span class="Constant">World</span>)
@@ -3288,7 +3266,7 @@ <h4 id="the-list-monad">The list monad</h4>
</ul>
<p>If you have followed me until here, then you&rsquo;ve done it!
-You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">9</a></sup>!</p>
+You know monads<sup id="fnref:03021301"><a href="#fn:03021301" rel="footnote">8</a></sup>!</p>
<p><a href="code/03_Hell/02_Monads/13_Monads.lhs" class="cut">03_Hell/02_Monads/<strong>13_Monads.lhs</strong> </a></p>
@@ -3651,9 +3629,6 @@ <h3 id="more-on-infinite-tree">More on Infinite Tree</h3>
<li id="fn:0216">
<p>You should remark <code>squareEvenSum''</code> is more efficient that the two other versions. The order of <code>(.)</code> is important.<a href="#fnref:0216" rel="reference">&#8617;</a></p>
</li>
- <li id="fn:022001">
- <p>I know there is a cleaner way, to do this in C++. This is only an example.<a href="#fnref:022001" rel="reference">&#8617;</a></p>
- </li>
<li id="fn:1">
<p>Which itself is very similar to the javascript <code>eval</code> on a string containing JSON).<a href="#fnref:1" rel="reference">&#8617;</a></p>
</li>
View
2 output/Scratch/fr/index.html
@@ -148,7 +148,7 @@
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.fr">Droits de reproduction ©, Yann Esposito</a>
</div>
<div id="lastmod">
- modifié le : 09/04/2012
+ modifié le : 10/04/2012
</div>
<div>
Site entièrement réalisé avec
View
4 output/Scratch/sitemap.xml
@@ -394,7 +394,7 @@
</url>
<url>
<loc>http://yannesposito.com/Scratch/fr/blog/Haskell-the-Hard-Way/</loc>
- <lastmod>2012-04-09</lastmod>
+ <lastmod>2012-04-10</lastmod>
</url>
<url>
<loc>http://yannesposito.com/Scratch/fr/blog/2010-02-15-All-but-something-regexp/</loc>
@@ -542,7 +542,7 @@
</url>
<url>
<loc>http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/</loc>
- <lastmod>2012-04-09</lastmod>
+ <lastmod>2012-04-10</lastmod>
</url>
<url>
<loc>http://yannesposito.com/Scratch/fr/blog/Haskell-Mandelbrot/</loc>

0 comments on commit 14b5681

Please sign in to comment.
Something went wrong with that request. Please try again.