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: how to read file with locking (Ertugrul Soeylemez)
2. Re: how to read file with locking (Ertugrul Soeylemez)
3. Re: While Condition in Haskell (Thomas Miedema)
4. Re: While Condition in Haskell (Lorenzo Isella)
5. Memory tuning (Thorsten Hater)
6. Reading Multiple Files and Iterate Function Application
(Lorenzo Isella)
----------------------------------------------------------------------
Message: 1
Date: Mon, 11 Oct 2010 07:35:25 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: how to read file with locking
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Jimmy Wylie <[email protected]> wrote:
> > It is, BTW, always preferable to use withFile over openFile, if you can.
> > This makes your code cleaner and also exception-safe.
>
> I don't think I quite understand. How is withFile exception-safe?
> Under the covers it's using openFile. I was under the impression
> withFile was just a nice way to remove boilerplate file operation code.
> Here's what I found on hoogle: "withFile
> <http://haskell.org/hoogle/?hoogle=withFile> name mode act opens a file
> using openFile <http://haskell.org/hoogle/?hoogle=openFile> and passes
> the resulting handle to the computation act. The handle will be closed
> on exit from withFile <http://haskell.org/hoogle/?hoogle=withFile>,
> whether by normal termination or by raising an exception."
The last statement is the point:
"[...] whether by normal termination or by raising an exception."
If you openFile and hClose manually, then you need to take care of
exceptions yourself. You need to make sure that hClose is called in all
cases. For example, what if reading the file throws an exception, which
is not catched? hClose may be skipped that way. withFile ensures that
the handle is always closed, even if an exception escapes your function.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 2
Date: Mon, 11 Oct 2010 07:39:01 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: how to read file with locking
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Joey Hess <[email protected]> wrote:
> Ertugrul Soeylemez wrote:
> > readFileLocked :: FilePath -> IO B.ByteString
> > readFileLocked fn =
> > withFile fn ReadMode $ \h -> do
> > lockFile h -- for a suitable function lockFile
> > B.hGetContents h
> >
> > It is, BTW, always preferable to use withFile over openFile, if you
> > can. This makes your code cleaner and also exception-safe.
>
> Unless there is a better locking primative than waitToSetLock
> available, I don't know how to build your lockFile function. It seems
> that it would have a side effect of closing the handle. It could
> return a new handle like this, but then withFile's automatic close of
> the file would be defeated.
I wasn't addressing the locking issue, but rather the laziness issue.
The lockFile function was just a placeholder.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 3
Date: Mon, 11 Oct 2010 13:46:54 +0200
From: Thomas Miedema <[email protected]>
Subject: Re: [Haskell-beginners] While Condition in Haskell
To: Patrick LeBoutillier <[email protected]>
Cc: Lorenzo Isella <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi Lorenzo,
what Patrick said was correct. Here's your program a bit more the Haskell
way. Also see:
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-List.html
import Data.List
main = do
let (past, future) = splitAt 8 [4,55,66,77,88,99,12,9,77,88,99,12,-99]
subfutures = tail . inits $ future
print . takeWhile (`isInfixOf` past) $ subfutures
Regards,
Thomas
On Mon, Oct 11, 2010 at 4:09 AM, Patrick LeBoutillier <
[email protected]> wrote:
> Lorenzo,
>
> On Sun, Oct 10, 2010 at 4:15 PM, Lorenzo Isella
> <[email protected]> wrote:
> > ...
> > In order to find the length of the longest list in the future which has
> > already been seen in the past, I could select the "true" values in the
> > output of function
> >
> > iter_find list i
> >
> > but this is a waste of CPU: I simply would like a while condition to tell
> my
> > function to stop checking new sublists as soon as it finds one which has
> not
> > occurred in the past and I would like a counter (a kind of i++) telling
> me
> > how many times the process has been iterated.
>
> I'm still a beginner myself, but here's my take on it. Since Haskell
> is lazy, the results
> will only be generated as they are needed. For example, if you change:
>
> let b = iter_find list i
>
> for
>
> let b = takeWhile id $ iter_find list i
>
> Haskell will stop generating the list as soon as it sees a False result.
> Then you can take the length of b as your answer.
>
> Patrick
>
>
> > Any suggestion is helpful.
> > Cheers
> >
> > Lorenzo
> >
> > -------------------------------------------------------------------
> >
> > import Data.Ord
> >
> >
> > import Data.List
> >
> > main :: IO ()
> >
> > main = do
> >
> >
> > let list = [4,55,66,77,88,99,12,9,77,88,99,12,-99]
> >
> > let i = 9
> >
> > let b = iter_find list i
> >
> > putStrLn "b is, "
> > print b
> >
> >
> > is_sublist sublist list = sublist `isInfixOf` list
> >
> >
> > gen_fut_list list i j = take j $ drop (i-1) list
> >
> > gen_past_list list i = take (i-1) list
> >
> > find_in_list list i j = is_sublist (gen_fut_list list i j) (gen_past_list
> > list i)
> >
> > iter_find list i = map (find_in_list list i) [1..n]
> > where n = (length list) - i +1
> >
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
>
>
> --
> =====================
> Patrick LeBoutillier
> Rosemère, Québec, Canada
> _______________________________________________
> 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/20101011/c6b1d7df/attachment-0001.html
------------------------------
Message: 4
Date: Mon, 11 Oct 2010 13:57:28 +0200
From: Lorenzo Isella <[email protected]>
Subject: Re: [Haskell-beginners] While Condition in Haskell
To: Thomas Miedema <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Thanks.
Actually, your example looks remarkably compact.
Cheers
Lorenzo
On 10/11/2010 01:46 PM, Thomas Miedema wrote:
> Hi Lorenzo,
>
> what Patrick said was correct. Here's your program a bit more the
> Haskell way. Also see:
> http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-List.html
>
>
> import Data.List
>
> main = do
> let (past, future) = splitAt 8 [4,55,66,77,88,99,12,9,77,88,99,12,-99]
> subfutures = tail . inits $ future
> print . takeWhile (`isInfixOf` past) $ subfutures
>
>
> Regards,
> Thomas
>
>
> On Mon, Oct 11, 2010 at 4:09 AM, Patrick LeBoutillier
> <[email protected] <mailto:[email protected]>>
> wrote:
>
> Lorenzo,
>
> On Sun, Oct 10, 2010 at 4:15 PM, Lorenzo Isella
> <[email protected] <mailto:[email protected]>> wrote:
> > ...
> > In order to find the length of the longest list in the future
> which has
> > already been seen in the past, I could select the "true" values
> in the
> > output of function
> >
> > iter_find list i
> >
> > but this is a waste of CPU: I simply would like a while condition
> to tell my
> > function to stop checking new sublists as soon as it finds one
> which has not
> > occurred in the past and I would like a counter (a kind of i++)
> telling me
> > how many times the process has been iterated.
>
> I'm still a beginner myself, but here's my take on it. Since Haskell
> is lazy, the results
> will only be generated as they are needed. For example, if you change:
>
> let b = iter_find list i
>
> for
>
> let b = takeWhile id $ iter_find list i
>
> Haskell will stop generating the list as soon as it sees a False result.
> Then you can take the length of b as your answer.
>
> Patrick
>
>
> > Any suggestion is helpful.
> > Cheers
> >
> > Lorenzo
> >
> > -------------------------------------------------------------------
> >
> > import Data.Ord
> >
> >
> > import Data.List
> >
> > main :: IO ()
> >
> > main = do
> >
> >
> > let list = [4,55,66,77,88,99,12,9,77,88,99,12,-99]
> >
> > let i = 9
> >
> > let b = iter_find list i
> >
> > putStrLn "b is, "
> > print b
> >
> >
> > is_sublist sublist list = sublist `isInfixOf` list
> >
> >
> > gen_fut_list list i j = take j $ drop (i-1) list
> >
> > gen_past_list list i = take (i-1) list
> >
> > find_in_list list i j = is_sublist (gen_fut_list list i j)
> (gen_past_list
> > list i)
> >
> > iter_find list i = map (find_in_list list i) [1..n]
> > where n = (length list) - i +1
> >
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected] <mailto:[email protected]>
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
>
>
> --
> =====================
> Patrick LeBoutillier
> Rosemère, Québec, Canada
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 5
Date: Mon, 11 Oct 2010 16:14:11 +0200
From: Thorsten Hater <[email protected]>
Subject: [Haskell-beginners] Memory tuning
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Hello everyone,
I'm trying to reduce the memory footprint (and possibly execution time,
too) of a small program
I wrote. It's from the project Euler context, produces the correct
result, but it is unsatifiing as
indicated by a profiling run:
2,633,675,888 bytes allocated in the heap
1,221,377,976 bytes copied during GC
27,240,696 bytes maximum residency (22 sample(s))
1,576,200 bytes maximum slop
80 MB total memory in use (1 MB lost due to fragmentation)
Generation 0: 5002 collections, 0 parallel, 1.38s, 1.36s elapsed
Generation 1: 22 collections, 0 parallel, 0.62s, 0.64s elapsed
INIT time 0.00s ( 0.00s elapsed)
MUT time 1.76s ( 1.80s elapsed)
GC time 1.99s ( 2.00s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 3.76s ( 3.81s elapsed)
%GC time 53.1% (52.6% elapsed)
Alloc rate 1,494,074,809 bytes per MUT second
Productivity 46.9% of total user, 46.3% of total elapsed
Not only is the actual memory consumption quite high, but the
GC/computation ratio is almost 1:1.
I assume that there is something to be done regarding strictness, but a
series of experiments with
BangPatterns return no yields at all (using foldr instead of foldl'
produces stack overflow)
Here is my program:
-- project euler p75.hs
import qualified Data.Map as Map
import Data.List -- strict foldl
circum :: Int -> Int -> Int
circum a b = 2*a*(a+b)
count :: Int -> (Map.Map Int Int) -> Int -> (Map.Map Int Int)
count lmt mp c = foldl' step mp $ takeWhile (<=lmt) [k*c | k <- [1..] ]
where step m x = Map.insertWith' (+) x 1 m
solve :: Int -> Int
solve l = Map.size $ Map.filter (== 1) primitive
where lmt = floor $ sqrt $ (fromIntegral l)/2
cand = [c | m <- [2..lmt],
n <- [1..m],
odd (m+n),
1 == gcd m n,
let c = circum m n,
l >= c]
primitive = foldl' (count l) (Map.empty) cand
main = do print $ solve 1500000
Any advice and or tips welcome, as I'm quite new to Haskell.
Best regards.
------------------------------
Message: 6
Date: Mon, 11 Oct 2010 16:56:58 +0200
From: Lorenzo Isella <[email protected]>
Subject: [Haskell-beginners] Reading Multiple Files and Iterate
Function Application
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Dear All,
Another I/O question.
Let us say that you are given a list of files file1.dat,
file2.dat...file10.dat and so on (i.e. every file is indexed by a number
and every file is a single column where every entry is a string without
spaces).
In the snippet below I read file1.dat, convert it to a list and then
print out its length.
Now, how can I iterate the process on file1.dat, file2.dat and file3.dat
and store the lengths in a list?
I would like to map the file reading and following operations on the
list [1,2.3], but that is giving me a headache.
It is relatively easy to create the file name
filename="file"++(show i)++".dat" , for i=1,2,3
but it the the iteration part that is giving me troubles.
Any suggestion is appreciated.
Cheers
Lorenzo
-- #####################################################################
import Data.Ord
import Data.List
main :: IO ()
main = do
list_t <- readFile "file1.dat"
let list = lines list_t
let b = length list
putStrLn "length of list is, "
print b
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 19
*****************************************