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. Reversing the lines read from stdin (Tim Perry)
2. Re: Reversing the lines read from stdin (Rahul Kapoor)
3. Re: Reversing the lines read from stdin (Krzysztof Skrz?tnicki)
4. Re: Reversing the lines read from stdin (Daniel Fischer)
5. how to specify type class in type of function? (george young)
6. Re: how to specify type class in type of function?
(Jorden Mauro)
7. Re: how to specify type class in type of function?
(Daniel Fischer)
8. subset (Luca Ciciriello)
9. subset - a little add (Luca Ciciriello)
10. Could not deduce (Matrix m (Maybe a)) from the context
(Matrix m a) (Lyndon Maydwell)
----------------------------------------------------------------------
Message: 1
Date: Thu, 28 Jan 2010 12:44:04 -0800 (PST)
From: Tim Perry <[email protected]>
Subject: [Haskell-beginners] Reversing the lines read from stdin
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
I wrote a simple program: it reads the contents from standard in and reverses
the order of the lines. I imagine there are many cleaner ways to do this.
Anybody willing to post some rewrites?
Thanks,
Tim
main = interact reverseFile
reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
combineStr s1 s2 = s2 ++ "\n" ++ s1
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100128/9659ac7b/attachment-0001.html
------------------------------
Message: 2
Date: Thu, 28 Jan 2010 15:53:12 -0500
From: Rahul Kapoor <[email protected]>
Subject: Re: [Haskell-beginners] Reversing the lines read from stdin
To: Tim Perry <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
> Anybody willing to post some rewrites?
> main = interact reverseFile
> reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
> combineStr s1 s2 = s2 ++ "\n" ++
I personally find the point-free version easier to read..
reverseFile = unlines . lines . foldr combineStr [] . lines
Vs
reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
------------------------------
Message: 3
Date: Thu, 28 Jan 2010 21:57:55 +0100
From: Krzysztof Skrz?tnicki <[email protected]>
Subject: Re: [Haskell-beginners] Reversing the lines read from stdin
To: Tim Perry <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Jan 28, 2010 at 21:44, Tim Perry <[email protected]> wrote:
>
> I wrote a simple program: it reads the contents from standard in and
> reverses the order of the lines. I imagine there are many cleaner ways to do
> this. Anybody willing to post some rewrites?
>
> Thanks,
> Tim
>
>
> main = interact reverseFile
> reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
> combineStr s1 s2 = s2 ++ "\n" ++ s1
>
What about this version:
main = interact (unlines . reverse . lines)
Best regards
Krzysztof SkrzÄtnicki
------------------------------
Message: 4
Date: Thu, 28 Jan 2010 21:56:43 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Reversing the lines read from stdin
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Donnerstag 28 Januar 2010 21:44:04 schrieb Tim Perry:
> I wrote a simple program: it reads the contents from standard in and
> reverses the order of the lines. I imagine there are many cleaner ways
> to do this. Anybody willing to post some rewrites?
>
> Thanks,
> Tim
>
>
> main = interact reverseFile
> reverseFile s = unlines $ lines $ foldr combineStr [] (lines s)
> combineStr s1 s2 = s2 ++ "\n" ++ s1
main = interact (unlines . reverse . lines)
perhaps?
------------------------------
Message: 5
Date: Thu, 28 Jan 2010 21:06:36 +0000 (UTC)
From: george young <[email protected]>
Subject: [Haskell-beginners] how to specify type class in type of
function?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
[complete newbie using ghci 6.10.4
I understand that I should indicate that 'a' is in class Ord, since I use
lessthan. But what is the syntax? Other style suggestions are welcome.
mysort :: [a] -> [a]
mysort [] = []
mysort [x] = [x]
mysort [x, y] | (x <= y) = [x, y]
| otherwise = [y, x]
mysort l =
(mysort s1) ++ (mysort s2)
where
(s1, s2) = splitAt (div (length l) 2) l
Could not deduce (Ord a) from the context ()
arising from a use of `<=' at /home/gry/foo.hs:53:17-22
Possible fix:
add (Ord a) to the context of the type signature for `mysort'
In the expression: (x <= y)
In a stmt of a pattern guard for
the definition of `mysort':
(x <= y)
In the definition of `mysort':
mysort [x, y]
| (x <= y) = [x, y]
| otherwise = [y, x]
------------------------------
Message: 6
Date: Thu, 28 Jan 2010 16:21:56 -0500
From: Jorden Mauro <[email protected]>
Subject: Re: [Haskell-beginners] how to specify type class in type of
function?
To: george young <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
The syntax is thus:
mysort :: (Ord a) => [a] -> [a]
On Thu, Jan 28, 2010 at 4:06 PM, george young <[email protected]> wrote:
> [complete newbie using ghci 6.10.4
> I understand that I should indicate that 'a' is in class Ord, since I use
> lessthan. But what is the syntax? Other style suggestions are welcome.
>
> mysort :: [a] -> [a]
> mysort [] = []
> mysort [x] = [x]
> mysort [x, y] | (x <= y) = [x, y]
> | otherwise = [y, x]
> mysort l =
> (mysort s1) ++ (mysort s2)
> where
> (s1, s2) = splitAt (div (length l) 2) l
>
> Could not deduce (Ord a) from the context ()
> arising from a use of `<=' at /home/gry/foo.hs:53:17-22
> Possible fix:
> add (Ord a) to the context of the type signature for `mysort'
> In the expression: (x <= y)
> In a stmt of a pattern guard for
> the definition of `mysort':
> (x <= y)
> In the definition of `mysort':
> mysort [x, y]
> | (x <= y) = [x, y]
> | otherwise = [y, x]
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 7
Date: Thu, 28 Jan 2010 22:26:28 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] how to specify type class in type of
function?
To: [email protected]
Cc: george young <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 28 Januar 2010 22:06:36 schrieb george young:
> [complete newbie using ghci 6.10.4
> I understand that I should indicate that 'a' is in class Ord, since I
> use lessthan. But what is the syntax? Other style suggestions are
> welcome.
>
> mysort :: [a] -> [a]
mysort :: Ord a => [a] -> [a]
Generally,
function :: (Class1 a, Class2 b) => a -> b -> c
The part of the type signature before the '=>' is called the context, if it
consits of only one constraint, the parentheses can be omitted.
> mysort [] = []
> mysort [x] = [x]
> mysort [x, y] | (x <= y) = [x, y]
>
> | otherwise = [y, x]
>
> mysort l =
> (mysort s1) ++ (mysort s2)
Doesn't work,
mysort [3,2,1,4] ~> [2,3,1,4]
you want merge here instead of (++)
(how to write merge is left as an exercise).
> where
> (s1, s2) = splitAt (div (length l) 2) l
If you have a long list, all those calls to length will take a loong time.
Consider passing it to a helper function which takes the length of the list
as a parameter.
>
> Could not deduce (Ord a) from the context ()
> arising from a use of `<=' at /home/gry/foo.hs:53:17-22
> Possible fix:
> add (Ord a) to the context of the type signature for `mysort'
> In the expression: (x <= y)
> In a stmt of a pattern guard for
> the definition of `mysort':
> (x <= y)
> In the definition of `mysort':
> mysort [x, y]
>
> | (x <= y) = [x, y]
> | otherwise = [y, x]
------------------------------
Message: 8
Date: Fri, 29 Jan 2010 07:07:06 +0000
From: Luca Ciciriello <[email protected]>
Subject: [Haskell-beginners] subset
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi All.
Does exist a simple way to check if a lists xs is a subset of another list ys?
Thanks in advance
Luca.
_________________________________________________________________
Tell us your greatest, weirdest and funniest Hotmail stories
http://clk.atdmt.com/UKM/go/195013117/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100129/ea654a33/attachment-0001.html
------------------------------
Message: 9
Date: Fri, 29 Jan 2010 07:36:35 +0000
From: Luca Ciciriello <[email protected]>
Subject: [Haskell-beginners] subset - a little add
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Just a little add to may previous mail.
The solution I've found from myself is:
subset :: [String] -> [String] -> Bool
subset xs ys = and [elem x ys | x <- xs]
My question is if exists a more elegant way to do that.
Luca.
_________________________________________________________________
Do you have a story that started on Hotmail? Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100129/ce4cda35/attachment-0001.html
------------------------------
Message: 10
Date: Fri, 29 Jan 2010 16:46:01 +0800
From: Lyndon Maydwell <[email protected]>
Subject: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
from the context (Matrix m a)
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Hi Beginners.
I'm trying to write a matrix class, for a game of life implementation,
and am having trouble with the vicinityMatrix function.
When I define it inside the class I get the error "Could not deduce
(Matrix m (Maybe a)) from the context (Matrix m a)".
However when I define it inside ghci, there is no problem: :t \m x y
-> fromRows $ vicinityRows m x y
\m x y -> fromRows $ vicinityRows m x y
:: forall (m :: * -> *) (m1 :: * -> *) a.
(Matrix m (Maybe a), Matrix m1 a) =>
m1 a -> Integer -> Integer -> m (Maybe a)
How would I modify my class to allow the definition?
The class is defined below.
module Matrix (Matrix) where
import Data.Array
import Data.Maybe (catMaybes)
import Control.Monad (guard)
class Matrix m a
where
fromRows :: [[a]] -> m a
toList :: m a -> [a]
rows :: m a -> Integer
columns :: m a -> Integer
row :: m a -> Integer -> [a]
column :: m a -> Integer -> [a]
at :: m a -> Integer -> Integer -> a
(!!!) :: m a -> Integer -> Integer -> a
vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
neighbours :: m a -> Integer -> Integer -> [a]
toList m = do
x <- [0 .. columns m - 1]
y <- [0 .. rows m - 1]
return $ at m x y
row m n = [at m x n | x <- [0 .. columns m - 1]]
column m n = [at m n y | y <- [0 .. rows m - 1]]
at = (!!!)
(!!!) = at
vicinityRows m x y = do
x' <- [x - 1 .. x + 1]
return $ do
y' <- [y - 1 .. y + 1]
return cell where
cell
| x < 0 = Nothing
| y < 0 = Nothing
| x >= columns m = Nothing
| y >= rows m = Nothing
| otherwise = Just $ at m x y
vicinityMatrix m x y = fromRows $ vicinityRows m x y
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 19, Issue 29
*****************************************