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.  How to remove leading and trailing non-alpha characters, and
      multiple consecutive spaces? (Costello, Roger L.)
   2. Re:  How to remove leading and trailing non-alpha characters,
      and multiple consecutive spaces? (Denis Kasak)
   3. Re:  How to remove leading and trailing non-alpha characters,
      and multiple consecutive spaces? (Tim Perry)
   4. Re:  How to remove leading and trailing non-alpha characters,
      and multiple consecutive spaces? (Denis Kasak)
   5. Re:  How to remove leading and trailing non-alpha characters,
      and multiple consecutive spaces? (Denis Kasak)
   6. Re:  How to remove leading and trailing non-alpha characters,
      and multiple consecutive spaces? (Tim Perry)
   7.  Type for lists with one or more elements (Frerich Raabe)


----------------------------------------------------------------------

Message: 1
Date: Thu, 6 Jun 2013 22:36:27 +0000
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

I have a string that contains a person's name. 

Prior to the person's name there may be some non-alpha characters. 

After the person's name there may be some non-alpha characters.

Between the person's first name and last name there should be only one space.

I want to remove the leading and trailing non-alpha characters and remove the 
extra spaces.

Here is an example string:

s = "     \"    John     Doe    \"    "

After processing, I should have:

        John Doe

Below is my implementation. Is there is a shorter and more efficient 
implementation? 

----------------------------------
import Data.Char
import Data.List

s = "     \"    John     Doe    \"    "

-- remove leading non-alpha characters

t1 = dropWhile (not . isAlpha) s                -- returns "John     Doe    \"  
  "

-- break the string up into a list of words, 
-- delimited by white space

t2 = words t1                           -- returns ["John","Doe","\""]

-- create a string consisting of the first 
-- name, space, last name

t3 = t2!!0 ++ " " ++ t2!!1              -- returns "John Doe"

-- Put it all together:

t4 = ((words . dropWhile (not . isAlpha)) s)!!0 ++ " " ++ ((words . dropWhile 
(not . isAlpha)) s)!!1





------------------------------

Message: 2
Date: Fri, 7 Jun 2013 01:06:55 +0200
From: Denis Kasak <[email protected]>
Subject: Re: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <canjrnzde3tp87rsip9vevkwz+da5rwgcui4oimulrp9ggcc...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 7 June 2013 00:36, Costello, Roger L. <[email protected]> wrote:
> Hi Folks,
>
> I have a string that contains a person's name.
[snip]
> Here is an example string:
>
> s = "     \"    John     Doe    \"    "
>
> After processing, I should have:
>
>         John Doe
>
> Below is my implementation. Is there is a shorter and more efficient 
> implementation?
[snip code]

How about this?

import Data.Char
import Control.Monad.Reader

isAlphaOrSpace = liftM2 (||) isAlpha isSpace

-- alternatively,
-- isAlphaOrSpace x = isAlpha x || isSpace x
-- if you find this more readable

s = "     \"    John     Doe    \"    "

fs = unwords
   . words
   . takeWhile isAlphaOrSpace
   . dropWhile (not . isAlpha) $ s

--
Denis Kasak



------------------------------

Message: 3
Date: Thu, 6 Jun 2013 16:14:34 -0700
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAFVgASXz82dMh0jX5XbNyZcFHdyDvYT-8J5OtQ2dfe_a_5=y...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Denis' version doesn't work for names containing hyphens or apostrophes.
The original works for both....  However, the original explicitly assumes
there are always at least two names and gives erroneous data if there are
more than two. Output below shows the failure on hyphenated names.

Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ "    \"   John Doe   \"  "
"John Doe"
Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ "    \"   John Doe-Smith   \"
 "
"John Doe"



Tim Perry
(916) 505-3634


On Thu, Jun 6, 2013 at 4:06 PM, Denis Kasak <[email protected]> wrote:

> On 7 June 2013 00:36, Costello, Roger L. <[email protected]> wrote:
> > Hi Folks,
> >
> > I have a string that contains a person's name.
> [snip]
> > Here is an example string:
> >
> > s = "     \"    John     Doe    \"    "
> >
> > After processing, I should have:
> >
> >         John Doe
> >
> > Below is my implementation. Is there is a shorter and more efficient
> implementation?
> [snip code]
>
> How about this?
>
> import Data.Char
> import Control.Monad.Reader
>
> isAlphaOrSpace = liftM2 (||) isAlpha isSpace
>
> -- alternatively,
> -- isAlphaOrSpace x = isAlpha x || isSpace x
> -- if you find this more readable
>
> s = "     \"    John     Doe    \"    "
>
> fs = unwords
>    . words
>    . takeWhile isAlphaOrSpace
>    . dropWhile (not . isAlpha) $ s
>
> --
> Denis Kasak
>
> _______________________________________________
> 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/20130606/0eaffcf9/attachment-0001.htm>

------------------------------

Message: 4
Date: Fri, 7 Jun 2013 01:21:52 +0200
From: Denis Kasak <[email protected]>
Subject: Re: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <canjrnzduvk6nykkg+q38xztzifzn2cfwnb37n340de-4vbh...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 7 June 2013 01:14, Tim Perry <[email protected]> wrote:
> Denis' version doesn't work for names containing hyphens or apostrophes. The
> original works for both....  However, the original explicitly assumes there
> are always at least two names and gives erroneous data if there are more
> than two. Output below shows the failure on hyphenated names.

Well, yes, I explicitly did not want to dwell on my (potential)
implicit assumptions and just handled the cases that where visible in
the problem example or were made explicit by the original poster.
Adding additional special behaviour for hypens and apostrophes would
be trivial, though, by further modifying the isAlphaOrSpace predicate
to include the new special characters.

-- 
Denis Kasak



------------------------------

Message: 5
Date: Fri, 7 Jun 2013 01:31:45 +0200
From: Denis Kasak <[email protected]>
Subject: Re: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <canjrnzf_pdkfg4f0n-xjyjfhweaz5g7upzvfvfoqb9wtz+1...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 7 June 2013 01:21, Denis Kasak <[email protected]> wrote:
> On 7 June 2013 01:14, Tim Perry <[email protected]> wrote:
>> Denis' version doesn't work for names containing hyphens or apostrophes. The
>> original works for both....  However, the original explicitly assumes there
>> are always at least two names and gives erroneous data if there are more
>> than two. Output below shows the failure on hyphenated names.
>
> Well, yes, I explicitly did not want to dwell on my (potential)
> implicit assumptions and just handled the cases that where visible in
> the problem example or were made explicit by the original poster.
> Adding additional special behaviour for hypens and apostrophes would
> be trivial, though, by further modifying the isAlphaOrSpace predicate
> to include the new special characters.

For instance, from a ghci session:
> let s = "    \"   John Doe-Smith   \"  "
> let (|||) = liftM2 (||)
> let predicate = isAlpha ||| isSpace ||| (== '-') ||| (== '\'')
> let fs = unwords . words . takeWhile predicate . dropWhile (not . isAlpha) $ s
> fs
"John Doe-Smith"

-- 
Denis Kasak



------------------------------

Message: 6
Date: Thu, 6 Jun 2013 16:44:31 -0700
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] How to remove leading and trailing
        non-alpha characters, and multiple consecutive spaces?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAFVgASWe2hSBtPJ2RpNHCf8yA8r3Vs5Q3eDAwNRhUDMOLHGG=w...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

That would certainly work. I'd be tempted to just use:

unwords . words . dropWhile (not . isAlpha) $ "unclean-first unclean-last"

Tim


On Thu, Jun 6, 2013 at 4:31 PM, Denis Kasak <[email protected]> wrote:

> On 7 June 2013 01:21, Denis Kasak <[email protected]> wrote:
> > On 7 June 2013 01:14, Tim Perry <[email protected]> wrote:
> >> Denis' version doesn't work for names containing hyphens or
> apostrophes. The
> >> original works for both....  However, the original explicitly assumes
> there
> >> are always at least two names and gives erroneous data if there are more
> >> than two. Output below shows the failure on hyphenated names.
> >
> > Well, yes, I explicitly did not want to dwell on my (potential)
> > implicit assumptions and just handled the cases that where visible in
> > the problem example or were made explicit by the original poster.
> > Adding additional special behaviour for hypens and apostrophes would
> > be trivial, though, by further modifying the isAlphaOrSpace predicate
> > to include the new special characters.
>
> For instance, from a ghci session:
> > let s = "    \"   John Doe-Smith   \"  "
> > let (|||) = liftM2 (||)
> > let predicate = isAlpha ||| isSpace ||| (== '-') ||| (== '\'')
> > let fs = unwords . words . takeWhile predicate . dropWhile (not .
> isAlpha) $ s
> > fs
> "John Doe-Smith"
>
> --
> Denis Kasak
>
> _______________________________________________
> 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/20130606/18d9fc34/attachment-0001.htm>

------------------------------

Message: 7
Date: Thu, 06 Jun 2013 17:27:11 -0700
From: Frerich Raabe <[email protected]>
Subject: [Haskell-beginners] Type for lists with one or more elements
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

is there an existing (or conventional) way to model lists of one or more 
items? I often deal with functions with don't make sense with empty 
lists, and the caller already verified that the lists are not empty. So 
to avoid getting warnings from ghc, I'd like to have a dedicated type 
for this case.

I could do something like

   data List a = Singleton a | Cons a (List a)

but before I go ahead I wonder: is there an existing Haskell package for 
this? Maybe even with prettier names than what I wrote above, and 
convenient functions for transforming from/to plain [] lists? It would 
probably be straightforward to write it myself, but because of that, I 
suspect that somebody else already did it. :-)

-- 
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 60, Issue 11
*****************************************

Reply via email to