<div dir="ltr">Yep, that's correct<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 6, 2015 at 12:41 PM, Roelof Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Alex Hammel schreef op 6-2-2015 om
21:35:<br>
</div><div><div class="h5">
<blockquote type="cite">
<div dir="ltr">You can think of zipWith as a function that
combines the values of two lists. If you want to add the numbers
in two lists you can do this:<br>
<br>
zipWith (+) [1, 2, 3] [4, 5, 6]<br>
<br>
which is the same thing as:<br>
<br>
[1+4, 2+5, 3+6]<br>
<br>
which results in [5, 7, 9]<br>
<br>
When you call zipWith ($) you're just applying the functions in
one list to the values in another. E.g.:<br>
<br>
zipWith ($) [(+1), (+2), (+3)] [1, 2, 3]<br>
<br>
is the same thing as<br>
<br>
[($) (+1) 1, ($) (+2) 2, ($) (+3) 3]<br>
<br>
which is the same thing as<br>
<br>
[1+1, 2+2, 3+3]<br>
<br>
once you've rewritten it in sane syntax.<br>
<br>
So the example I gave:<br>
<br>
zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4]<br>
<br>
is the same thing as<br>
<br>
[id 1, 2+1, id 3, 4+1]<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Feb 6, 2015 at 12:24 PM, Roelof
Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>YCH schreef op 6-2-2015 om 20:54:<br>
</div>
<span>
<blockquote type="cite">
<p dir="ltr">Thanks for explanation.</p>
<div class="gmail_quote">2015. 2. 7. 오전 4:42에 "Alex
Hammel" <<a href="mailto:ahammel87@gmail.com" target="_blank">ahammel87@gmail.com</a>>
님이 작성:<br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">This is mostly for my own
recreation, feel free to ignore it.<br>
<br>
Your solution is fine, but it lacks modularity.
What if you discover that you don't actually
want to double every other number but triple it?
Or if the list of numbers is suddenly a list of
words and you need to capitalize every other
one? You don't want to have to write a new
function from scratch. Let's make a function
that applies any function to every other value:<br>
<br>
everyOther :: (a -> a) -> [a] -> [a]<br>
everyOther _ [] = []<br>
everyOther _ [x] = [x]<br>
everyOther f (x:y:xs) = x : f y : everyOther f
xs<br>
<br>
doubleEveryOther :: [Int] -> [Int]<br>
doubleEveryOther = everyOther (*2)<br>
<br>
But hang on, what if the requirements change
again and now we have to double every third
value? Writing something like this is no fun:<br>
<br>
everyThird :: (a -> a) -> [a] -> [a]<br>
everyThird _ [] = []<br>
everyThird _ [x] = [x]<br>
everyThird _ [x,y] = [x,y]<br>
everyThird f (x:y:z:xs) = x : y : f z :
everyThird f xs<br>
<br>
And the implementation of everyHundredAndFifth
will obviously be ridiculous. Clearly what we
need is an `everyNth` function which allows the
programmer to specify which list elements the
function is applied to.<br>
<br>
One trick is to create a list of functions and
use zipWith ($). ($) is just function
application; so a list with `id` at every
position except the nth will work:<br>
<br>
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4]<br>
[1,3,3,5]<br>
<br>
</div>
</blockquote>
</div>
</blockquote>
<br>
</span> Here I miss you,. I have only done the first
chapter of the NIC course and it not talked about using
zipWith. <br>
<br>
I only learned recursion and how that worked on list. <br>
<br>
So can you explain how only the second item is added by 1
.<br>
<br>
As soon as I understand that part I will study the rest
and I think I have more questions. <br>
<span><font color="#888888"> <br>
Roelof<br>
<br>
</font></span></div>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
Beginners mailing list
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br></div></div>
Oke, I think I get it.<br>
<br>
So if I want to add +1 to the thirt item I can do this : <span>λ zipWith ($) [id, id, (+1), id] [1, 2, 3, 4]</span>
which gives [1.2.4.4] <br>
<br>
Am i correct ?<br>
<br>
Roelof<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
Roelof<br>
<br>
</font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>