Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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:  Couldn't match expected type `(a0 -> [a0]) -> [a0]',
      with actual type `[a0]' (Mike Meyer)
   2.  ClassyPrelude vs. Haskell.Language.Interpreter (Mike Meyer)


----------------------------------------------------------------------

Message: 1
Date: Sat, 23 May 2015 07:45:56 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Couldn't match expected type `(a0 ->
        [a0]) -> [a0]', with actual type `[a0]'
Message-ID:
        <CAD=7U2B1uW1Yy1C-urBZK2RoZjoQ15Yvdq83bpqL=n2p4q6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Try this:

toDigits' :: Integral a => [a] -> a -> [a]
toDigits' acc 0 = acc
toDigits' acc n = toDigits' (n `mod` 10 : acc) (n `div` 10)

If the accumulator in a recursive function doesn't accumulate anything
during some recursive call, but is just passed on unchanged, then  you
might as well make it a free variable. Or eliminate it entirely if it's
unused.

You can now use a functional programming idiom, and assign the function
toDigits to the function toDigits' [], like so:

toDigits :: Integral a => a -> [a]
toDigits = toDigits' []

On Sat, May 23, 2015 at 6:59 AM, Sumit Sahrawat, Maths & Computing, IIT
(BHU) <[email protected]> wrote:

> The error lies here:
>
> toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod`
> 10))
>
> It should instead be
>
> toDigits' acc number = (number `mod` 10) : (toDigits' acc (number `mod`
> 10))
>
> My suggestion would be to look at it like a fold.
>
> toDigits' :: Integral a => a -> a -> [a]
> toDigits' acc 0 = [acc]
> toDigits' acc n = n `mod` 10 : toDigits' acc (n `div` 10)
>
> Now this gives the digits in the reverse order, so in toDigits, you can
> reverse it.
>
> A good exercise would now be to re-write this as a fold. Graham Hutton has
> a good paper about it. [1]
>
> The best way would be to directly convert the number to a string using
> show, but that's not the point of the exercise.
>
> [1]: https://www.cs.nott.ac.uk/~gmh/fold.pdf
>
> On 23 May 2015 at 12:28, Roelof Wobben <[email protected]> wrote:
>
>> Hello,
>>
>> For some reasons my file's are corrupted.
>> I had repair them with success except this one.
>>
>> Here is the code :
>>
>> toDigits  number
>>   | number <= 0   = []
>>   | otherwise     = toDigits' [] number
>>   where
>>     toDigits' acc 0  = acc
>>     toDigits' acc number   = ((number `mod` 10 ): acc) (toDigits' (number
>> `mod` 10))
>>
>> main = print $ toDigits 123
>>
>>
>> and here is the error:
>>
>> Couldn't match expected type `(a0 -> [a0]) -> [a0]'
>>                 with actual type `[a0]'
>>     The function `(number `mod` 10) : acc' is applied to one argument,
>>     but its type `[a0]' has none
>>     In the expression:
>>       ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
>>     In an equation for toDigits':
>>         toDigits' acc number
>>           = ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
>>
>>
>> Roelof
>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>> antivirussoftware.
>> http://www.avast.com
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150523/877c84d8/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 23 May 2015 14:38:07 -0500
From: Mike Meyer <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] ClassyPrelude vs.
        Haskell.Language.Interpreter
Message-ID:
        <CAD=7u2bv9ogwjhlnxume87fcn9xjk1p7fevw_5ewsukdcmk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'm trying to run interpreted code via ClassyPrelude, and getting some
results that make me suspect a bug in the Prelude's type system. Or maybe
the interpreter.

Anyway, here's a bit of code that works as expected:

{-# LANGUAGE NoImplicitPrelude #-}

import ClassyPrelude
import Language.Haskell.Interpreter

main :: IO ()
main = do
  fun <- runInterpreter $ makeFun "reverse"
  case fun of
   Left e -> print e
   Right f -> readFile "/etc/motd" >>= hPut stdout . f


makeFun expr = do
  set [languageExtensions := [NoImplicitPrelude]]
  setImportsQ [("ClassyPrelude", Nothing)]
  interpret expr (as :: Text -> Text)


I don't think I can simplify this any further. It works as expected, and
also works as expected, and prints out the contents of /etc/motd reversed.

However, if you change the type signature in the last line from Text ->
Text to LText -> Ltext (to get lazy text), you get no output. But if you
change the function in the first line after main from "reverse" to "id", it
works.

So far, it might be an issue with lazy IO. However, change the type
signature in the last line to LText -> Text. In this case, there is no
output for either value of the expression.  I expect an error in this case,
as neither id nor reverse should be able to have the type LText -> Text!

So, is there something I missed in either ClassyPrelude or the Interpreter?
Or is this a subtle interaction, in which case can someone suggest a
workaround? Or have I found a bug in one of the two?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150523/e6651843/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 83, Issue 53
*****************************************

Reply via email to