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: dropWhile returning last dropped element (Michael Orlitzky)
2. Re: dropWhile returning last dropped element (Bob Ippolito)
3. IO and ghci (Mike Houghton)
4. Re: IO and ghci (Friedrich Wiemer)
----------------------------------------------------------------------
Message: 1
Date: Sat, 18 Apr 2015 19:21:24 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] dropWhile returning last dropped
element
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On 04/18/2015 01:10 PM, martin wrote:
> Am 04/17/2015 um 11:18 PM schrieb Michael Orlitzky:
>
>> Why do you want to avoid recursion? You could rewrite this to use list
>> indices if you really wanted to, but anything
>> you come up with is going to be essentially recursive, only less safe
>
> Thanks for the code sample and pointing out that there may not be any last
> dropped element.
>
> I was wondering if there is to achive the desired behavior by plugging
> together higher-order functions. This was the
> only reason why I wanted to avoid explicit recursion.
>
Sure. Whenever you're processing a list and building up a return value,
it's probably a (left) fold. But a fold would pointlessly process the
rest of the list after it had stopped dropping elements, violating one
of your criteria. And foldl is of course implemented recursively =)
A "short-circuiting" version of foldl might exist in some library, but
there are a few ways I can think of to implement it, so it might be hard
to find.
------------------------------
Message: 2
Date: Sat, 18 Apr 2015 16:48:08 -0700
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] dropWhile returning last dropped
element
Message-ID:
<CACwMPm_3gKM8W=h30k9hpbsqgwivh8uqcu4s4ljdrmwfktr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sat, Apr 18, 2015 at 4:21 PM, Michael Orlitzky <[email protected]>
wrote:
> On 04/18/2015 01:10 PM, martin wrote:
> > Am 04/17/2015 um 11:18 PM schrieb Michael Orlitzky:
> >
> >> Why do you want to avoid recursion? You could rewrite this to use list
> indices if you really wanted to, but anything
> >> you come up with is going to be essentially recursive, only less safe
> >
> > Thanks for the code sample and pointing out that there may not be any
> last dropped element.
> >
> > I was wondering if there is to achive the desired behavior by plugging
> together higher-order functions. This was the
> > only reason why I wanted to avoid explicit recursion.
> >
>
> Sure. Whenever you're processing a list and building up a return value,
> it's probably a (left) fold. But a fold would pointlessly process the
> rest of the list after it had stopped dropping elements, violating one
> of your criteria. And foldl is of course implemented recursively =)
>
> A "short-circuiting" version of foldl might exist in some library, but
> there are a few ways I can think of to implement it, so it might be hard
> to find.
You don't need a left fold for this. It's a bit awkward but you can indeed
just combine functions. Here's one way to write it that should not suffer
from any sort of pointless list processing.
import Data.List (tails)
import Data.Maybe (listToMaybe)
dropWhileWithLast :: (a -> Bool) -> [a] -> (Maybe a, [a])
dropWhileWithLast f xs =
-- Not partial. The last element of tails is [] and
-- maybe False will guarantee a non-empty list.
head .
dropWhile (maybe False f . listToMaybe . snd) $
zip (Nothing : map Just xs) (tails xs)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150418/01919fce/attachment-0001.html>
------------------------------
Message: 3
Date: Sun, 19 Apr 2015 10:46:11 +0100
From: Mike Houghton <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] IO and ghci
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi,
I have this in a .hs file and it works fine. That is I see lines printed to the
screen when the .hs file is loaded and run from ghci.
main = do
file <- readFile "poem.txt"
mapM_ putStrLn (lines file)
Experimenting at the ghci command line with
let f = readFile ?poem.txt?
the above is fine.
But doing
lines f
gives
<interactive>:52:7:
Couldn't match type ?IO String? with ?[Char]?
Expected type: String
Actual type: IO String
In the first argument of ?lines?, namely ?f?
In the expression: lines f
In my simplistic beginners way I think this is because when I run main = do ?
everything is or is within an ?impure? area but at ghci command line it is not
and I?m mixing pure/impure functions.
So my questions are
1. Is this simplistic view any where near correct?
2. How can I progress at the command line in ghci?
Many thanks
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150419/309ca221/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 19 Apr 2015 12:03:03 +0200
From: Friedrich Wiemer <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] IO and ghci
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi Mike
as you perhaps have noticed, you use slightly different notations here:
> file <- readFile "poem.txt"
> let f = readFile ?poem.txt?
You can run both versions in ghci, and if you take a look at the type
signatures:
?> file <- readFile "poem.txt"
?> :t file
file :: String
?> let f = readFile "poem.txt"
?> :t f
f :: IO String
You'll notice that f has type "IO String", whereas file only has type
"String".
So, to answer your second question: you can progress by using the "<-"
notation, as in your haskell source code.
To answer the first one: I'm not sure about ghci command prompt, but I
would expect, that it is equivalent to "main = do ...".
That is, the type error you got, is not due to ghci and main = do are
different, but because you used to different types.
To understand the "<-" notation, the example from Learn you a Haskell
was quite good for me:
Try to see types like "IO String" as an action or box, which can result
in a string. You can take the inner type out of the box, by using "<-".
Hope that helps,
cheers,
Friedrich
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150419/ef9ca300/attachment-0001.sig>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 82, Issue 24
*****************************************