
Oke, I have solved it already. The problem was that I did list.length but Haskell uses length list Still too much Ruby in my system :( Roelof Francesco Ariis schreef op 6-2-2015 om 17:50:
(off-list) please consider not using html in your mails, it's quite difficult to read them for us who plaintext-friendly client
On Fri, Feb 06, 2015 at 05:47:24PM +0100, Roelof Wobben wrote:
<html> <head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"> </head> <body bgcolor="#FFFFFF" text="#000000"> Hello, <br> <br> I have to double every second element from the right. <br> <br> So for a even length array that means : 1 , 3 , 5 and so on <br> and for a non even lenght that means the 2,4 and so on. <br> <br> So I thought I could solve that on this way : <br> <br> -- | Doubles every second number from the right.<br> doubleEveryOther :: [Integer] -> [Integer]<br> doubleEveryOther [] = [] <br> doubleEveryOther (x:[]) = [x] <br> doubleEveryOther (x:(y:zs)) <br> | ((x:(y:zs)).length) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs<br> | otherwise = [x *2] ++ y : doubleEveryOther zs<br> <br> <br> <br> but this does not work because I see this error message : <br> <br> <div class="ide-error-span">src/Main.hs@14:8-14:16 </div> <div class="ide-error-msg"><span>Couldn't match expected type ‘Int -> c0’ with actual type </span> <div class="CodeMirror cm-s-default" style="font-size: 14px;">[<span class="cm-variable-2">Integer</span>]</div> <span title="Click to show/hide extra information" class="ide-error-collapse-btn"> …</span><span style="display: inline;"> In the first argument of ‘(.)’, namely ‘(x : (y : zs))’ In the first argument of ‘mod’, namely ‘((x : (y : zs)) . length)’ In the first argument of ‘(/=)’, namely ‘((x : (y : zs)) . length) `mod` 2’<br> <br> <br> Can anyone give me a better way to check if I have a even or odd length array ?<br> <br> Roelof<br> <br> </span></div> <br> </body> </html> _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a]
everyOther _ [] = []
everyOther _ [x] = [x]
everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int]
doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a]
everyThird _ [] = []
everyThird _ [x] = [x]
everyThird _ [x,y] = [x,y]
everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4]
[1,3,3,5]
We can use `cycle` to make an infinite list of functions and `replicate` to
generate the padding of the function list:
everyNth :: Int -> (a -> a) -> [a] -> [a]
everyNth n f = zipWith ($) fs
where
fs = cycle $ replicate (n-1) id ++ [f] -- e.g. cycle [id, f] when n
is 2
everyOther' :: (a -> a) -> [a] -> [a]
everyOther' = everyNth 2
everyThird' :: (a -> a) -> [a] -> [a]
everyThird' = everyNth 3
As for testing whether the length is odd or even: why not just reverse it,
double every other number, and reverse it again?
doubleEveryOther :: Num a => [a] -> [a]
doubleEveryOther = reverse . everyOther (*2) . reverse
Cheers,
Alex
On Fri, Feb 6, 2015 at 8:55 AM, Roelof Wobben
Oke,
I have solved it already. The problem was that I did list.length but Haskell uses length list
Still too much Ruby in my system :(
Roelof
Francesco Ariis schreef op 6-2-2015 om 17:50:
(off-list) please consider not using html in your mails, it's quite difficult to read them for us who plaintext-friendly client
On Fri, Feb 06, 2015 at 05:47:24PM +0100, Roelof Wobben wrote:
<html> <head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"> </head> <body bgcolor="#FFFFFF" text="#000000"> Hello, <br> <br> I have to double every second element from the right. <br> <br> So for a even length array that means : 1 , 3 , 5 and so on <br> and for a non even lenght that means the 2,4 and so on. <br> <br> So I thought I could solve that on this way : <br> <br> -- | Doubles every second number from the right.<br> doubleEveryOther :: [Integer] -> [Integer]<br> doubleEveryOther [] = [] <br> doubleEveryOther (x:[]) = [x] <br> doubleEveryOther (x:(y:zs)) <br> | ((x:(y:zs)).length) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs<br> | otherwise = [x *2] ++ y : doubleEveryOther zs<br> <br> <br> <br> but this does not work because I see this error message : <br> <br> <div class="ide-error-span">src/Main.hs@14:8-14:16 </div> <div class="ide-error-msg"><span>Couldn't match expected type ‘Int -> c0’ with actual type </span> <div class="CodeMirror cm-s-default" style="font-size: 14px;">[<span class="cm-variable-2">Integer</span>]</div> <span title="Click to show/hide extra information" class="ide-error-collapse-btn"> …</span><span style="display: inline;"> In the first argument of ‘(.)’, namely ‘(x : (y : zs))’ In the first argument of ‘mod’, namely ‘((x : (y : zs)) . length)’ In the first argument of ‘(/=)’, namely ‘((x : (y : zs)) . length) `mod` 2’<br> <br> <br> Can anyone give me a better way to check if I have a even or odd length array ?<br> <br> Roelof<br> <br> </span></div> <br> </body> </html> _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks for explanation.
2015. 2. 7. 오전 4:42에 "Alex Hammel"
This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a] everyOther _ [] = [] everyOther _ [x] = [x] everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int] doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a] everyThird _ [] = [] everyThird _ [x] = [x] everyThird _ [x,y] = [x,y] everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4] [1,3,3,5]
We can use `cycle` to make an infinite list of functions and `replicate` to generate the padding of the function list:
everyNth :: Int -> (a -> a) -> [a] -> [a] everyNth n f = zipWith ($) fs where fs = cycle $ replicate (n-1) id ++ [f] -- e.g. cycle [id, f] when n is 2
everyOther' :: (a -> a) -> [a] -> [a] everyOther' = everyNth 2
everyThird' :: (a -> a) -> [a] -> [a] everyThird' = everyNth 3
As for testing whether the length is odd or even: why not just reverse it, double every other number, and reverse it again?
doubleEveryOther :: Num a => [a] -> [a] doubleEveryOther = reverse . everyOther (*2) . reverse
Cheers, Alex
On Fri, Feb 6, 2015 at 8:55 AM, Roelof Wobben
wrote: Oke,
I have solved it already. The problem was that I did list.length but Haskell uses length list
Still too much Ruby in my system :(
Roelof
Francesco Ariis schreef op 6-2-2015 om 17:50:
(off-list) please consider not using html in your mails, it's quite difficult to read them for us who plaintext-friendly client
On Fri, Feb 06, 2015 at 05:47:24PM +0100, Roelof Wobben wrote:
<html> <head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"> </head> <body bgcolor="#FFFFFF" text="#000000"> Hello, <br> <br> I have to double every second element from the right. <br> <br> So for a even length array that means : 1 , 3 , 5 and so on <br> and for a non even lenght that means the 2,4 and so on. <br> <br> So I thought I could solve that on this way : <br> <br> -- | Doubles every second number from the right.<br> doubleEveryOther :: [Integer] -> [Integer]<br> doubleEveryOther [] = [] <br> doubleEveryOther (x:[]) = [x] <br> doubleEveryOther (x:(y:zs)) <br> | ((x:(y:zs)).length) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs<br> | otherwise = [x *2] ++ y : doubleEveryOther zs<br> <br> <br> <br> but this does not work because I see this error message : <br> <br> <div class="ide-error-span">src/Main.hs@14:8-14:16 </div> <div class="ide-error-msg"><span>Couldn't match expected type ‘Int -> c0’ with actual type </span> <div class="CodeMirror cm-s-default" style="font-size: 14px;">[<span class="cm-variable-2">Integer</span>]</div> <span title="Click to show/hide extra information" class="ide-error-collapse-btn"> …</span><span style="display: inline;"> In the first argument of ‘(.)’, namely ‘(x : (y : zs))’ In the first argument of ‘mod’, namely ‘((x : (y : zs)) . length)’ In the first argument of ‘(/=)’, namely ‘((x : (y : zs)) . length) `mod` 2’<br> <br> <br> Can anyone give me a better way to check if I have a even or odd length array ?<br> <br> Roelof<br> <br> </span></div> <br> </body> </html> _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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:
zipWith (+) [1, 2, 3] [4, 5, 6]
which is the same thing as:
[1+4, 2+5, 3+6]
which results in [5, 7, 9]
When you call zipWith ($) you're just applying the functions in one list to
the values in another. E.g.:
zipWith ($) [(+1), (+2), (+3)] [1, 2, 3]
is the same thing as
[($) (+1) 1, ($) (+2) 2, ($) (+3) 3]
which is the same thing as
[1+1, 2+2, 3+3]
once you've rewritten it in sane syntax.
So the example I gave:
zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4]
is the same thing as
[id 1, 2+1, id 3, 4+1]
On Fri, Feb 6, 2015 at 12:24 PM, Roelof Wobben
YCH schreef op 6-2-2015 om 20:54:
Thanks for explanation. 2015. 2. 7. 오전 4:42에 "Alex Hammel"
님이 작성: This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a] everyOther _ [] = [] everyOther _ [x] = [x] everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int] doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a] everyThird _ [] = [] everyThird _ [x] = [x] everyThird _ [x,y] = [x,y] everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4] [1,3,3,5]
Here I miss you,. I have only done the first chapter of the NIC course and it not talked about using zipWith.
I only learned recursion and how that worked on list.
So can you explain how only the second item is added by 1 .
As soon as I understand that part I will study the rest and I think I have more questions.
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Yep, that's correct
On Fri, Feb 6, 2015 at 12:41 PM, Roelof Wobben
Alex Hammel schreef op 6-2-2015 om 21:35:
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:
zipWith (+) [1, 2, 3] [4, 5, 6]
which is the same thing as:
[1+4, 2+5, 3+6]
which results in [5, 7, 9]
When you call zipWith ($) you're just applying the functions in one list to the values in another. E.g.:
zipWith ($) [(+1), (+2), (+3)] [1, 2, 3]
is the same thing as
[($) (+1) 1, ($) (+2) 2, ($) (+3) 3]
which is the same thing as
[1+1, 2+2, 3+3]
once you've rewritten it in sane syntax.
So the example I gave:
zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4]
is the same thing as
[id 1, 2+1, id 3, 4+1]
On Fri, Feb 6, 2015 at 12:24 PM, Roelof Wobben
wrote: YCH schreef op 6-2-2015 om 20:54:
Thanks for explanation. 2015. 2. 7. 오전 4:42에 "Alex Hammel"
님이 작성: This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a] everyOther _ [] = [] everyOther _ [x] = [x] everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int] doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a] everyThird _ [] = [] everyThird _ [x] = [x] everyThird _ [x,y] = [x,y] everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4] [1,3,3,5]
Here I miss you,. I have only done the first chapter of the NIC course and it not talked about using zipWith.
I only learned recursion and how that worked on list.
So can you explain how only the second item is added by 1 .
As soon as I understand that part I will study the rest and I think I have more questions.
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://www.haskell.org/mailman/listinfo/beginners
Oke, I think I get it.
So if I want to add +1 to the thirt item I can do this : λ zipWith ($) [id, id, (+1), id] [1, 2, 3, 4] which gives [1.2.4.4]
Am i correct ?
Roelof
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Alex Hammel schreef op 6-2-2015 om 20:41:
This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a] everyOther _ [] = [] everyOther _ [x] = [x] everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int] doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a] everyThird _ [] = [] everyThird _ [x] = [x] everyThird _ [x,y] = [x,y] everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4] [1,3,3,5]
We can use `cycle` to make an infinite list of functions and `replicate` to generate the padding of the function list:
everyNth :: Int -> (a -> a) -> [a] -> [a] everyNth n f = zipWith ($) fs where fs = cycle $ replicate (n-1) id ++ [f] -- e.g. cycle [id, f] when n is 2
oke, Can you also explain what this function does exactly. I see a variable n and f and I see cycle en replicate and a n-1 What is I want to multiply the second item by 2. That was the challenge I had now. Roelof

`everyNth` applies a function f to every nth item in a list. If you want to
double every other item in a list you can do
everyNth 2 (*2) aList
In order to accomplish this, it makes an infinite list of functions where
every nth function is `f` (the user-supplied function) and the rest are
`id`. (This function terminates when applied to finite lists becase zipWith
stops when the shorter of it's arguments runs out). So:
everyNth 2 (*2) [1, 2, 3, 4]
is the same as
zipWith ($) [id, (*2), id, (*2)] [1, 2, 3, 4]
On Fri, Feb 6, 2015 at 12:52 PM, Roelof Wobben
Alex Hammel schreef op 6-2-2015 om 20:41:
This is mostly for my own recreation, feel free to ignore it.
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:
everyOther :: (a -> a) -> [a] -> [a] everyOther _ [] = [] everyOther _ [x] = [x] everyOther f (x:y:xs) = x : f y : everyOther f xs
doubleEveryOther :: [Int] -> [Int] doubleEveryOther = everyOther (*2)
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:
everyThird :: (a -> a) -> [a] -> [a] everyThird _ [] = [] everyThird _ [x] = [x] everyThird _ [x,y] = [x,y] everyThird f (x:y:z:xs) = x : y : f z : everyThird f xs
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.
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:
λ zipWith ($) [id, (+1), id, (+1)] [1, 2, 3, 4] [1,3,3,5]
We can use `cycle` to make an infinite list of functions and `replicate` to generate the padding of the function list:
everyNth :: Int -> (a -> a) -> [a] -> [a] everyNth n f = zipWith ($) fs where fs = cycle $ replicate (n-1) id ++ [f] -- e.g. cycle [id, f] when n is 2
oke, Can you also explain what this function does exactly.
I see a variable n and f and I see cycle en replicate and a n-1
What is I want to multiply the second item by 2. That was the challenge I had now.
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Alex Hammel
-
Roelof Wobben
-
YCH