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. generating by mapping (Szilveszter Juhos)
2. Re: generating by mapping (Stephen Tetley)
3. Defining 'words' in terms of 'span' (Roger Whittaker)
4. Re: Defining 'words' in terms of 'span' (Ozgur Akgun)
5. Re: Defining 'words' in terms of 'span' (Daniel Fischer)
6. Re: generating by mapping (Tim Perry)
7. Re: Defining 'words' in terms of 'span' (Tim Attwood)
8. Reading in the contents of a variable number of files (Tim Perry)
----------------------------------------------------------------------
Message: 1
Date: Tue, 16 Mar 2010 09:57:46 +0100
From: Szilveszter Juhos <[email protected]>
Subject: [Haskell-beginners] generating by mapping
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I have something like:
data Stuff = Stuff {
aNum :: Int
, anStr :: String
} deriving (Show)
let first = Stuff 123 "qwe"
let second = Stuff 321 "asd"
print first
print second
and works fine. What is the right "map" statement if I want to
generate these stuff using lists like:
let strs = [ "qwe", "asd", "zxc" ]
let nums = [ 123, 321, 345]
?
Cheers:
Szilva
------------------------------
Message: 2
Date: Tue, 16 Mar 2010 09:09:51 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] generating by mapping
To: Szilveszter Juhos <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Szilveszter
zipWith is probably what you are after...
> zipWith Stuff nums strs
[Stuff {aNum = 123, anStr = "qwe"},Stuff {aNum = 321, anStr =
"asd"},Stuff {aNum = 345, anStr = "zxc
"}]
Note zipWith (and the function zip which it generalizes) go 'short' -
i.e. if one of the input lists is shorter than the other - the size of
the result list will the size of of the shorter one:
> zipWith Stuff [1] strs
[Stuff {aNum = 1, anStr = "qwe"}]
Best wishes
Stephen
------------------------------
Message: 3
Date: Tue, 16 Mar 2010 17:08:15 +0000
From: Roger Whittaker <[email protected]>
Subject: [Haskell-beginners] Defining 'words' in terms of 'span'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I found some exam papers linked from this page:
http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html
And I have been trying some questions from them.
I'm currently baffled by question 2(b) on this one:
http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/tenta2000-04.ps
A word is a sequence of alphabetic characters, which you can recognise
using the standard function
isAlpha :: Char -> Bool
Using span, define a function words :: String -> [String] which
finds a list of the words occurring in a string. For example,
words "Now is the winter of our discontent!"
== ["Now","is","the","winter","of","our","discontent"]
words "2+3" == []
words "1 by 1" == ["by"]
Can anyone give me a clue how to start?
--
========================
Roger Whittaker
[email protected]
http://disruptive.org.uk
========================
------------------------------
Message: 4
Date: Tue, 16 Mar 2010 17:23:33 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Defining 'words' in terms of 'span'
To: Roger Whittaker <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
You might start with looking into
span<http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Prelude.html#v:span>
.
span :: (a -> Bool) -> [a] -> ([a], [a])
For example try these,
span ('a'==) "abcd"
span ('a'==) "aaabcd"
span ('c'==) "abcd"
span ('c'/=) "abcd"
span ('c'/=) "abcdcdcd"
On 16 March 2010 17:08, Roger Whittaker <[email protected]> wrote:
> I found some exam papers linked from this page:
> http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html
>
> And I have been trying some questions from them.
>
> I'm currently baffled by question 2(b) on this one:
> http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/tenta2000-04.ps
>
> A word is a sequence of alphabetic characters, which you can recognise
> using the standard function
>
> isAlpha :: Char -> Bool
>
> Using span, define a function words :: String -> [String] which
> finds a list of the words occurring in a string. For example,
>
> words "Now is the winter of our discontent!"
> == ["Now","is","the","winter","of","our","discontent"]
>
> words "2+3" == []
>
> words "1 by 1" == ["by"]
>
> Can anyone give me a clue how to start?
>
>
>
>
> --
> ========================
> Roger Whittaker
> [email protected]
> http://disruptive.org.uk
> ========================
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100316/f92173f7/attachment-0001.html
------------------------------
Message: 5
Date: Tue, 16 Mar 2010 19:36:04 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Defining 'words' in terms of 'span'
To: [email protected], Roger Whittaker <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Dienstag 16 März 2010 18:23:33 schrieb Ozgur Akgun:
> You might start with looking into
> span<http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Prel
>ude.html#v:span> .
>
> span :: (a -> Bool) -> [a] -> ([a], [a])
>
And then, using span, write a function that splits the first word off a
String. And a function to use when the String doesn't start with a word
(like "", " made glorious summer by this son of York.", "2+3", ...).
>
> For example try these,
>
> span ('a'==) "abcd"
> span ('a'==) "aaabcd"
> span ('c'==) "abcd"
> span ('c'/=) "abcd"
> span ('c'/=) "abcdcdcd"
>
> On 16 March 2010 17:08, Roger Whittaker <[email protected]> wrote:
> > I found some exam papers linked from this page:
> > http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html
> >
> > And I have been trying some questions from them.
> >
> > I'm currently baffled by question 2(b) on this one:
> > http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/tenta2000-04.p
> >s
> >
> > A word is a sequence of alphabetic characters, which you can
> > recognise using the standard function
> >
> > isAlpha :: Char -> Bool
> >
> > Using span, define a function words :: String -> [String] which
> > finds a list of the words occurring in a string. For example,
> >
> > words "Now is the winter of our discontent!"
> > == ["Now","is","the","winter","of","our","discontent"]
> >
> > words "2+3" == []
> >
> > words "1 by 1" == ["by"]
> >
> > Can anyone give me a clue how to start?
> >
> >
> >
> >
> > --
> > ========================
> > Roger Whittaker
> > [email protected]
> > http://disruptive.org.uk
> > ========================
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 6
Date: Tue, 16 Mar 2010 11:40:33 -0700 (PDT)
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] generating by mapping
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Stephen's version is cleaner, but this works too:
map (\(x, y) -> Stuff x y) $ zip nums strs
----- Original Message ----
From: Stephen Tetley <[email protected]>
To: Szilveszter Juhos <[email protected]>
Cc: [email protected]
Sent: Tue, March 16, 2010 2:09:51 AM
Subject: Re: [Haskell-beginners] generating by mapping
Hi Szilveszter
zipWith is probably what you are after...
> zipWith Stuff nums strs
[Stuff {aNum = 123, anStr = "qwe"},Stuff {aNum = 321, anStr =
"asd"},Stuff {aNum = 345, anStr = "zxc
"}]
Note zipWith (and the function zip which it generalizes) go 'short' -
i.e. if one of the input lists is shorter than the other - the size of
the result list will the size of of the shorter one:
> zipWith Stuff [1] strs
[Stuff {aNum = 1, anStr = "qwe"}]
Best wishes
Stephen
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 7
Date: Tue, 16 Mar 2010 13:54:24 -0700
From: "Tim Attwood" <[email protected]>
Subject: [Haskell-beginners] Re: Defining 'words' in terms of 'span'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original
"Roger Whittaker" <[email protected]> wrote in message
news:[email protected]...
>I found some exam papers linked from this page:
> http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html
>
> And I have been trying some questions from them.
>
> I'm currently baffled by question 2(b) on this one:
> http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/tenta2000-04.ps
>
> A word is a sequence of alphabetic characters, which you can recognise
> using the standard function
>
> isAlpha :: Char -> Bool
>
> Using span, define a function words :: String -> [String] which
> finds a list of the words occurring in a string. For example,
>
> words "Now is the winter of our discontent!"
> == ["Now","is","the","winter","of","our","discontent"]
>
> words "2+3" == []
>
> words "1 by 1" == ["by"]
>
> Can anyone give me a clue how to start?
words' = filter (all isAlpha) . words
...but since they specifically want span in the answer I'd guess they want
some sort of recursive function
words' s = wGen s [] where
wGen s a =
let (n,ws) = span (\x -> not (isAlpha x)) s
(w,ss) = span (isAlpha) ws
in case (w=="",ss="") of
True, _ -> reverse a
False, True -> reverse (w:a)
False, False -> wGen ss (w:a)
something ugly like that maybe.
------------------------------
Message: 8
Date: Tue, 16 Mar 2010 14:10:46 -0700 (PDT)
From: Tim Perry <[email protected]>
Subject: [Haskell-beginners] Reading in the contents of a variable
number of files
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I need to read in and then combine a variable number of files. I am
trying to figure out how to get the contents of all the files into an
array.
let fileContentsArray = getData ["dataFile1.txt",
"dataFile2.txt"]
getData :: [String] -> [String]
getData [] = []
getData (f:files) = do
fileData
<- readFile f
fileData : (getData files)
I figured I could use readFile to open the files. But, I need to use
<- to get the data out of the IO Action and into a value. I thought I could
write a function like the following, but I'm getting no-where. I
suspect I'm missing something basic about IO in Haskell. Can anybody
point me to a good resource....
Thanks,
Tim
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 17
*****************************************