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: Getting the Takusen example code to compile -failed
import problem (Bayley, Alistair)
2. Does this function already exist in one of the standard
modules? (I. J. Kennedy)
3. Re: Does this function already exist in one of the standard
modules? (Felipe Lessa)
4. Re: Does this function already exist in one of the standard
modules? (Daniel Fischer)
5. Re: Does this function already exist in one of the standard
modules? (I. J. Kennedy)
6. Re: Does this function already exist in one of the standard
modules? (Daniel Fischer)
7. Re: fmap fmap (Federico Brubacher)
8. some insights into functional programming (Michael Mossey)
9. unit tests (Michael Mossey)
----------------------------------------------------------------------
Message: 1
Date: Fri, 7 Aug 2009 08:47:48 +0100
From: "Bayley, Alistair" <[email protected]>
Subject: RE: [Haskell-beginners] Getting the Takusen example code to
compile -failed import problem
To: "Daniel Everett" <[email protected]>
Cc: [email protected]
Message-ID:
<125eacd0cae4d24abdb4d148c4593da911026...@gblonxmb02.corp.amvescap.net>
Content-Type: text/plain; charset="us-ascii"
> -----Original Message-----
> From: Daniel Everett [mailto:[email protected]]
>
> OK, this now compiles thanks to (a) hacking Setup.hs to refer
> to odbc_config rather than odbcconf and (b) unregistering the
> user packages with ghc-pkg --user unregister Takusen.
Please create a darcs patch for Setup.hs and send it to me. IIRC, you
were on Linux? I'm not sure if Takusen has been tested under Linux.
We've receieved patches for OSX, Oleg uses BSD, and I have Windows, so
it would be useful to get patches for Linux installs too.
> create table dummy (id int primary key);
> query1Iteratee :: (Monad m) => Int -> String -> Double ->
> IterAct m [(Int, String, Double)]
> query1Iteratee a b c accum = result' ((a, b, c):accum)
> r <- doQuery (sql "select id from dummy") query1Iteratee []
> liftIO(putStrLn(show r))
> )
> fromUTF8Ptr: zero byte found in string as position 8
>
> Is there any thing wrong with the above code?
Yes. The iteratee function expects three columns in the result-set (with
types Int, String, and Double, in that order), but your query only
provides one column (dummy, of type Int).
There is the beginnings of some code in a couple of the backends to
validate the iteratee against the actual result-set, but it's nowhere
near complete. So if you have a mismatch between iteratee and query,
then you are likely to get marshaling erros and/or garbage. Note that
it's OK to have a result-set return more columns than the iteratee
consumes, but obviously to converse cannnot work.
> Also, what does liftIO actually do?
The monad that your database session is in is a ReaderT wrapper over IO.
In order to perform actions that run in the IO monad, you need to "lift"
them.
http://www.haskell.org/haskellwiki/Monad_Transformers_Explained
Alistair
*****************************************************************
Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.
*****************************************************************
------------------------------
Message: 2
Date: Sat, 8 Aug 2009 15:01:34 -0500
From: "I. J. Kennedy" <[email protected]>
Subject: [Haskell-beginners] Does this function already exist in one
of the standard modules?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Does this function already exist in one of the standard modules?
It computes f(f(f(...f(x))). I suspect it is a common function, but I
can't find it, even using Hoogle.
applyMany :: Int -> (a -> a) -> a -> a
applyMany 0 f x = x
applyMany n f x = applyMany (n-1) f (f x)
------------------------------
Message: 3
Date: Sat, 8 Aug 2009 17:38:23 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Does this function already exist in
one of the standard modules?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, Aug 08, 2009 at 03:01:34PM -0500, I. J. Kennedy wrote:
> Does this function already exist in one of the standard modules?
> It computes f(f(f(...f(x))). I suspect it is a common function, but I
> can't find it, even using Hoogle.
>
> applyMany :: Int -> (a -> a) -> a -> a
> applyMany 0 f x = x
> applyMany n f x = applyMany (n-1) f (f x)
applyMany n f x = iterate f x !! n
--
Felipe.
------------------------------
Message: 4
Date: Sat, 8 Aug 2009 22:54:15 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Does this function already exist in
one of the standard modules?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Samstag 08 August 2009 22:01:34 schrieb I. J. Kennedy:
> Does this function already exist in one of the standard modules?
> It computes f(f(f(...f(x))). I suspect it is a common function, but I
> can't find it, even using Hoogle.
>
> applyMany :: Int -> (a -> a) -> a -> a
> applyMany 0 f x = x
> applyMany n f x = applyMany (n-1) f (f x)
Not directly, I think.
But we have
iterate :: (a -> a) -> a -> [a]
iterate f x = x:iterate f (f x)
so applyMany n f x = iterate f x !! n
Most of the time you either need different numbers of iterations for the same
starting
value, then iterate is what you want, or the same number of iterations for
different
values, then you use something like
let g = foldl (.) id (replicate n f) in ...
------------------------------
Message: 5
Date: Sat, 8 Aug 2009 17:17:46 -0500
From: "I. J. Kennedy" <[email protected]>
Subject: Re: [Haskell-beginners] Does this function already exist in
one of the standard modules?
To: Felipe Lessa <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
I'm surprised there's no standard function for this.
Using iterate f x !! n is ok I suppose, but:
If I try to calculate the millionth fibonacci number like this
fibStep (a,b) = (b,a+b)
iterate fibStep (0,1) !! 1000000
I get a stack overflow, but if I use applyMany
applyMany 1000000 fibStep (0,1)
it works.
On Sat, Aug 8, 2009 at 3:38 PM, Felipe Lessa<[email protected]> wrote:
> On Sat, Aug 08, 2009 at 03:01:34PM -0500, I. J. Kennedy wrote:
>> Does this function already exist in one of the standard modules?
>> It computes  f(f(f(...f(x))).  I suspect it is a common function, but I
>> can't find it, even using Hoogle.
>>
>> Â applyMany :: Int -> (a -> a) -> a -> a
>> Â applyMany 0 f x = x
>> Â applyMany n f x = applyMany (n-1) f (f x)
>
> applyMany n f x = iterate f x !! n
>
> --
> Felipe.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 6
Date: Sun, 9 Aug 2009 16:02:57 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Does this function already exist in
one of the standard modules?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Sonntag 09 August 2009 00:17:46 schrieb I. J. Kennedy:
> I'm surprised there's no standard function for this.
> Using iterate f x !! n is ok I suppose, but:
>
> If I try to calculate the millionth fibonacci number like this
>
> fibStep (a,b) = (b,a+b)
> iterate fibStep (0,1) !! 1000000
>
> I get a stack overflow, but if I use applyMany
>
> applyMany 1000000 fibStep (0,1)
>
> it works.
>
I can't reproduce that, I get a stack overflow for both (as it should be*).
Both work, with about the same performance, if I use stricter versions:
sfibStep :: (Integer,Integer) -> (Integer,Integer)
sfibStep (a,b) = let c = a+b in c `seq` (b,c)
applyMany' :: Int -> (a -> a) -> a -> a
applyMany' 0 _ x = x
applyMany' n f x = applyMany (n-1) f $! (f x)
iterate' :: (a -> a) -> a -> [a]
iterate' f x = x:(iterate' f $! f x)
With the lazy original fibStep, both strict versions (applyMany' and iterate')
work, but
take much longer (roughly 20 times).
[*] Both, iterate and the original applyMany build a thunk of one million
nested function
calls, that needs a larger stack than the default.
------------------------------
Message: 7
Date: Sun, 9 Aug 2009 13:39:08 -0300
From: Federico Brubacher <[email protected]>
Subject: Re: [Haskell-beginners] fmap fmap
To: Tony Morris <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
fmap . fmap is close to applicative ?
is it possible to do applicative instead of combining 2 fmaps ?
Thanks
On Wed, Aug 5, 2009 at 12:29 AM, Tony Morris <[email protected]> wrote:
> Michael P Mossey wrote:
> > How does one write
> >
> > fmap (fmap (*2)) xs
> >
> > without parenthesis? (Using . and $ instead.)
> >
> > I don't really understand . and $ well enough I guess. I tried a bunch
> > of stuff but nothing worked.
> >
> > -Mike
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> fmap . fmap $ (*2)
>
> or perhaps:
>
> (fmap . fmap) (*2)
>
> --
> Tony Morris
> http://tmorris.net/
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Federico Brubacher
www.fbrubacher.com
Colonial Duty Free Shop
www.colonial.com.uy
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090809/38c455d7/attachment-0001.html
------------------------------
Message: 8
Date: Sun, 09 Aug 2009 12:31:47 -0700
From: Michael Mossey <[email protected]>
Subject: [Haskell-beginners] some insights into functional programming
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I'm starting to figure out a few things that I didn't "get" about
functional programming and monads. I wanted to explain them. I'm not
looking for a particular response to this post, other than any elaboration
that seems natural.
There is an exercise here working with the trivial monad W:
<http://blog.sigfpe.com/2007/04/trivial-monad.html>
Write a function
g :: W a -> W a -> W a
such that
g (W x) (W y) = W (x+y)
except don't use pattern matching, but >>= instead. The answer is
g mx my = mx >>= (\x -> my >>= \y -> W (x+y))
There are a couple things here that threw me off. One is that I didn't
expect 'my' to be available inside the first lambda. I somehow thought of
lambda as isolated, sealed-off from the rest of the universe. But they
aren't. I believe this is the concept of closures, or related to it?
Secondly, I didn't expect >>= to be available inside the lambda. This is
related to the mistaken conception of >>= as a procedural statement rather
than an expression. In Python, where I have previously encountered lambdas,
no statements are allowed inside lambdas. Of course, >>= is actually an
expression and you can put any expression to the right of a lambda ->.
Maybe these are typical beginner misconceptions, or maybe they have more to
do with coming from Python and complete beginners actually find it more
natural.
Mike
------------------------------
Message: 9
Date: Sun, 9 Aug 2009 13:52:40 -0700 (PDT)
From: "Michael Mossey" <[email protected]>
Subject: [Haskell-beginners] unit tests
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain;charset=iso-8859-1
As I write Haskell code I find myself needing to write test code. If this
test code were organized and documented, I could probably call it a set of
unit tests. I'm trying to find some convention for naming functions and
variables used in unit testing, deciding on location (in same module or a
second module? same directory or sub-directory?), etc. Are there any
Haskell community conventions?
Thanks,
Mike
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 14, Issue 5
****************************************