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. Haskell Array Index out of range (James)
2. Re: I don't understand mapM in mapM id (Just 1, Nothing,
Just 3) (Haisheng Wu)
3. My notes on a YouTube video presentation on Haskell
(Costello, Roger L.)
4. Points-free style (Alexander Shendi)
5. Re: Points-free style (Lyndon Maydwell)
6. Re: Points-free style (Daniel Fischer)
7. Re: Points-free style (Ozgur Akgun)
----------------------------------------------------------------------
Message: 1
Date: Sun, 05 Jun 2011 19:35:54 +0800
From: James <[email protected]>
Subject: [Haskell-beginners] Haskell Array Index out of range
To: Haskell Beginer <[email protected]>
Message-ID: <1307273754.1561.10.camel@ivy-laptop>
Content-Type: text/plain; charset="utf-8"
Dear all,
Today I spend nearly one day's time to debug this exception, but still
haven't got a solution. My code is pasted [here][1].
The program throws an exception when evaluating this line:
points = assert (len > 0) $ A.listArray (1::Int, len) $ map (\(u,i,r)
-> [u,i]) rts
Below is my ghci debug session. I still don't understand why it has a
range of (0, -193459561) when the 'len' binding is actually 90570.
*Main> :break 125
Breakpoint 4 activated at SVMCF.hs:125:13-86
*Main> :trace main
Stopped at SVMCF.hs:125:13-86
_result :: UA.Array Int [User] = _
len :: Int = 90570
rts :: [RTuple] = (1,1,5.0) : (1,2,3.0) : (1,3,4.0) : (1,4,3.0) :
(1,5,3.0) : ....
[SVMCF.hs:125:13-86] *Main> :lis
124 points :: A.Array Int [Int]
125 points = assert (len > 0) $ A.listArray (1::Int, len) $ map
(\(u,i,r) -> [u,i]) rts
126 values :: UA.UArray Int Double
[SVMCF.hs:125:13-86] *Main> :ste
Stopped at SVMCF.hs:125:13-28
_result :: UA.Array Int [User] -> UA.Array Int [User] = _
len :: Int = 90570
[SVMCF.hs:125:13-28] *Main> :ste
Stopped at SVMCF.hs:125:21-27
_result :: Bool = _
len :: Int = 90570
[SVMCF.hs:125:21-27] *Main> :ste
Stopped at SVMCF.hs:125:32-86
_result :: UA.Array Int [User] = _
len :: Int = 90570
rts :: [RTuple] = (1,1,5.0) : (1,2,3.0) : (1,3,4.0) : (1,4,3.0) :
(1,5,3.0) : ....
[SVMCF.hs:125:32-86] *Main> :ste
Stopped at SVMCF.hs:125:32-56
_result :: [[User]] -> UA.Array Int [User] = _
len :: Int = 90570
[SVMCF.hs:125:32-56] *Main> :lis
124 points :: A.Array Int [Int]
125 points = assert (len > 0) $ A.listArray (1::Int, len) $ map
(\(u,i,r) -> [u,i]) rts
126 values :: UA.UArray Int Double
[SVMCF.hs:125:32-56] *Main> len
90570
[SVMCF.hs:125:32-56] *Main> :ste
Stopped at SVMCF.hs:125:60-86
_result :: [[User]] = _
rts :: [RTuple] = (1,1,5.0) : (1,2,3.0) : (1,3,4.0) : (1,4,3.0) :
(1,5,3.0) : ....
[SVMCF.hs:125:60-86] *Main> :ste
*** Exception: Ix{Int}.index: Index (1) out of range ((1,-193459561))
[1]: http://hpaste.org/47460/haskell_array_index_out_of_ran
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110605/10fb36e6/attachment-0001.htm>
------------------------------
Message: 2
Date: Sun, 5 Jun 2011 20:40:27 +0800
From: Haisheng Wu <[email protected]>
Subject: Re: [Haskell-beginners] I don't understand mapM in mapM id
(Just 1, Nothing, Just 3)
To: Arlen Cuss <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
By looking at sequence implementation in Hugs:
sequence (c:cs) = do x <- c
xs <- sequence cs
return (x:xs)
Apply it against a list [Just 2, Nothing], we will have:
sequence [Just 2, Nothing]
= do x <- Just 2
y <- sequence [Nothing]
return (x:y)
= return (2:Nothing)
The question is
Why/How `return (2:Nothing)` eval to `Nothing` ?
-Haisheng
On Sat, May 21, 2011 at 8:25 AM, Arlen Cuss <[email protected]> wrote:
>
>
> > mapM id [Just 1, Just 2, Just 3]
> > result: Just [1,2,3]
>
> >
> > mapM :: (a -> m b) -> [a] -> m [b]
> > So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2,
> > Just 3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
>
> Right! So note here that 'm' is Maybe and 'b' is 'Int', thus mapM's
> return value is 'm [b]', i.e. 'Maybe [Int]'. The implication is that it
> somehow yields a Maybe of [Int], but no Maybe Int.
>
> > mapM id [Just 1, Nothing, Just 3]
> > result: Nothing.
> > My first guess for the result: Just [Just 1, Nothing, Just 3]
>
> This is contingent of the semantics of the Maybe monad. First, mapM's
> definition:
>
> > mapM f as = sequence (map f as)
>
> So the list is mapped onto the (monadic!) function, then sequenced:
>
> > sequence :: Monad m => [m a] -> m [a]
> > sequence = foldr mcons (return [])
> > where
> > mcons p q = p >>= \x -> q >>= \y -> return (x : y)
>
> Note that consecutive values are bound, so seeing this example should
> clarify why a single Nothing causes mapM to return Nothing for the lot:
>
> > Just 1 >> Just 2
> Just 2
> > Nothing >> Just 2
> Nothing
> >
>
> This falls simply out of Maybe's Monad instance definition for bind:
>
> > instance Monad Maybe where
> > (Just x) >>= k = k x
> > Nothing >>= k = Nothing
>
> HTH.
>
> Arlen
>
> _______________________________________________
> 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/20110605/70436352/attachment-0001.htm>
------------------------------
Message: 3
Date: Sun, 5 Jun 2011 10:35:22 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] My notes on a YouTube video presentation
on Haskell
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi Folks,
Here are my notes on a YouTube video on Haskell
(http://www.youtube.com/watch?v=cXY4fSA7DnM)
If you wish to view the video, I recommend skipping over the first part. The
Haskell presentation starts at 3:10
Here's what the speaker (Sasha Rush) says:
Haskell is an avant-garde programming language. It's not something that you
will see a lot of companies using these days. If you want to get a job, you
should be a really good C programmer. If you want to program the future, you
should learn Haskell.
Haskell is fast like C. It's not as fast, but it's getting there. It on the
same order of magnitude. Here's a comparison of the speeds of several languages:
(Optimized) Haskell is 1.5x slower than C
Schema 9.4x
Python 16x
PHP 23x
Ruby 57x
Let's take an example -- the Fibonacci sequence.
Recall that the Fibonacci sequence begins with the number 1, then another 1,
then the following numbers are the sum of the previous two numbers:
1, 1, 2, 3, 5, 8, 13, 21, ...
Here's the Fibonacci sequence implemented in Java:
List <int> fib(int n) {
List <int> seq = new ArrayList(n)
seq[0] = 1;
seq[1] = 1;
for (int i = 2; i < n, i++) {
seq[i] = seq[i-2] + seq[i-1];
}
return seq;
}
Read as: The function, fib, takes an integer, n, and returns a list of
integers. First thing to do is to declare the list. Then set the first value,
then the second value. Then do a for-loop.
Here's the Fibonacci sequence implemented in Haskell:
fib = 1 : 1 : zipWith (+) fib (tail fib)
Read as: The Fibonacci sequence is a list: the first item is the number 1, then
the number 1, and then the following numbers are the zipping together (adding
values at the same list position) of the Fibonacci sequence and the tail of the
Fibonacci sequence. I created a graphic to illustrate:
http://www.xfront.com/Haskell/Fibonacci-Sequence-in-Haskell.png
/Roger
------------------------------
Message: 4
Date: Sun, 05 Jun 2011 17:09:17 +0200
From: Alexander Shendi <[email protected]>
Subject: [Haskell-beginners] Points-free style
To: [email protected]
Message-ID: <1307286557.3770.12.camel@alex-n220>
Content-Type: text/plain; charset="UTF-8"
Hi folks,
I am working my way through "Learn You a Haskell for Great Good" and
have reached page 84, where the book recommends that you define your
functions in a "points-free style", using the "." and "$" operators.
Now I have:
sumProducts' :: Num a => [a] -> [a] -> a
sumProducts' x y = sum (zipWith (*) x y)
I would like to eliminate the "x" and the "y" in the definition, but all
I have managed to contrive is:
sumProducts :: Num a => [a] -> [a] -> a
sumProducts x = sum . zipWith (*) x
How do I proceed from here? Any advice is welcome :)
Many thanks in advance,
/Alexander
------------------------------
Message: 5
Date: Sun, 5 Jun 2011 23:39:39 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] Points-free style
To: Alexander Shendi <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
You can use the fun (.).(.) operator :D
sumProducts :: Num a => [a] -> [a] -> a
sumProducts = ((.).(.)) sum (zipWith (*))
In all seriousness though, points-free isn't always the most readable way to go.
On Sun, Jun 5, 2011 at 11:09 PM, Alexander Shendi
<[email protected]> wrote:
> Hi folks,
>
> I am working my way through "Learn You a Haskell for Great Good" and
> have reached page 84, where the book recommends that you define your
> functions in a "points-free style", using the "." and "$" operators.
>
> Now I have:
>
> sumProducts' :: Num a => [a] -> [a] -> a
> sumProducts' x y = sum (zipWith (*) x y)
>
> I would like to eliminate the "x" and the "y" in the definition, but all
> I have managed to contrive is:
>
> sumProducts :: Num a => [a] -> [a] -> a
> sumProducts x = sum . zipWith (*) x
>
> How do I proceed from here? Any advice is welcome :)
>
> Many thanks in advance,
>
> /Alexander
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 6
Date: Sun, 5 Jun 2011 17:40:19 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Points-free style
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="us-ascii"
On Sonntag, 5. Juni 2011, 17:09, Alexander Shendi wrote:
> Hi folks,
>
> I am working my way through "Learn You a Haskell for Great Good" and
> have reached page 84, where the book recommends that you define your
> functions in a "points-free style", using the "." and "$" operators.
>
> Now I have:
>
> sumProducts' :: Num a => [a] -> [a] -> a
> sumProducts' x y = sum (zipWith (*) x y)
>
> I would like to eliminate the "x" and the "y" in the definition, but all
> I have managed to contrive is:
>
> sumProducts :: Num a => [a] -> [a] -> a
> sumProducts x = sum . zipWith (*) x
>
> How do I proceed from here? Any advice is welcome :)
>
> Many thanks in advance,
>
> /Alexander
First, (mentally) insert parentheses:
sumProducts x = sum . (zipWith (*) x)
Now, write the composition as a prefix application of (.),
sumProducts x = (.) sum (zipWith (*) x)
= ((.) sum) (zipWith (*) x)
which has the form f (g x), with f = (.) sum and g = zipWith (*), so it's
(f . g) x, which expands to
(((.) sum) . (zipWith (*))) x
and now the argument can easily be dropped, giving
sumProducts = ((.) sum) . (zipWith (*))
now to make it look a little nicer, we can remove unnecessary parentheses
and write (.) sum as a section,
sumProducts = (sum .) . zipWith (*)
Generally, pointfreeing goes
f (g x) ~> f . g
f (g x y) ~> (f .) . g
f (g x y z) ~> ((f .) .) . g
Play with it and get a little experience, but don't overuse it.
Nobody really wants to come across a pointfree version of
\a b c d e f g h -> foo (bar a b) (baz c d e) f (quux g h)
(and that's not even repeating or flipping arguments)
------------------------------
Message: 7
Date: Sun, 5 Jun 2011 17:09:21 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Points-free style
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 5 June 2011 16:40, Daniel Fischer <[email protected]>wrote:
> \a b c d e f g h -> foo (bar a b) (baz c d e) f (quux g h)
Just for the fun of it:
$ pointfree "\a b c d e f g h -> foo (bar a b) (baz c d e) f (quux g h)"
flip flip quux . ((flip . ((flip . ((flip . ((flip . (((.) . (.)) .)) .))
.)) .)) .) . flip flip baz . (((.) . (.) . (.) . foo) .) . bar
using: http://hackage.haskell.org/package/pointfree
Cheers,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110605/fa5e87d7/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 11
*****************************************