Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: recursion problem. (Roelof Wobben)
2. Re: recursion problem. (Roelof Wobben)
3. length problem (Roelof Wobben)
4. Re: length problem (Roelof Wobben)
5. Re: length problem (Alex Hammel)
----------------------------------------------------------------------
Message: 1
Date: Fri, 06 Feb 2015 14:44:19 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] recursion problem.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
Marcin Mrotek schreef op 6-2-2015 om 11:27:
> Ah, sorry, I didn't think of that when I responded to your other
> thread. You can always insert a check before recursion:
>
> toDigits :: Integer -> [Integer]
> toDigits n
> | n < 0 = []
> | otherwise = (if n' > 0 then toDigits n' else []) ++ [n `mod` 10]
> where n' = n `div` 10
>
> Regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
Hello,
Can you explain to me why we need a n' and a where here.
I tried the same solution here :
toDigitsRev :: Integer -> [Integer]
toDigitsRev n
| n <= 0 = []
| otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
but I could not make it work.
Roelof
------------------------------
Message: 2
Date: Fri, 06 Feb 2015 15:05:11 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] recursion problem.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
Roelof Wobben schreef op 6-2-2015 om 10:25:
> Hello,
>
> I have figured out how I can make from 123 [1,2,3]
>
> I did it this way :
>
> -- | convert a number to a array in pieces where a negative number
> will be a empty array.
> toDigits :: Integer -> [Integer]
> toDigits n
> | n <= 0 = []
> | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
>
>
> but now when I do toDigits 0 , I see [] as output where I was expected
> [0]
>
> But when I do toDigits 100 I see the right output [ 1,0,0] which
> surprises me because I tought that with the first 0 there will be a []
>
> Or is the n the second time [0,0] and the thirth time [0] So it will
> be like this :
>
> toDigits 100
>
> to Digits [1] ++ [ 0,0]
> toDigits [1] ++ [0] ++ [0]
>
> which leads to [1,0,0]
>
> Roelof
>
Hmm,
this is also not working :
-- | convert a number to a array in pieces where a negative number will
be a empty array.
toDigits :: Integer -> [Integer]
toDigits 0 = [0]
toDigits n
| n < 0 = []
| otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will
be a empty array
toDigitsRev :: Integer -> [Integer]
toDigitsRev 0 = [0]
toDigitsRev n
| n <= 0 = []
| otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | The main entry point.
main :: IO ()
main = do
print $ toDigits 100
print $ toDigitsRev 100
print $ toDigits 0
print $ toDigitsRev 0
print $ toDigits (-17)
print $ toDigitsRev (-17)
you get a zero too much when you use a number above 0 .
------------------------------
Message: 3
Date: Fri, 06 Feb 2015 17:47:24 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] length problem
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150206/539ab9e1/attachment-0001.html>
------------------------------
Message: 4
Date: Fri, 06 Feb 2015 17:55:55 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] length problem
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
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
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Fri, 6 Feb 2015 11:41:48 -0800
From: Alex Hammel <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] length problem
Message-ID:
<CA+_xFepxYBXkq-3wiMwo_w=u554AHeiXJcDt=yue+lsvld0...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150206/5f88b01e/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 80, Issue 9
****************************************