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: Type for lists with one or more elements (Brent Yorgey)
2. Re: Type for lists with one or more elements (Frerich Raabe)
3. Re: How to remove leading and trailing non-alpha characters,
and multiple consecutive spaces? (Michael Peternell)
4. Re: How to remove leading and trailing non-alpha characters,
and multiple consecutive spaces? (David Virebayre)
----------------------------------------------------------------------
Message: 1
Date: Thu, 6 Jun 2013 20:56:09 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Type for lists with one or more
elements
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Jun 06, 2013 at 05:27:11PM -0700, Frerich Raabe wrote:
> 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. :-)
Yes, this exists in the semigroups package:
http://hackage.haskell.org/packages/archive/semigroups/0.9.2/doc/html/Data-List-NonEmpty.html
-Brent
------------------------------
Message: 2
Date: Thu, 6 Jun 2013 20:41:47 -0700
From: Frerich Raabe <[email protected]>
Subject: Re: [Haskell-beginners] Type for lists with one or more
elements
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
On Jun 6, 2013, at 5:56 PM, Brent Yorgey <[email protected]> wrote:
> On Thu, Jun 06, 2013 at 05:27:11PM -0700, Frerich Raabe wrote:
>> 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. :-)
>
> Yes, this exists in the semigroups package:
>
>
> http://hackage.haskell.org/packages/archive/semigroups/0.9.2/doc/html/Data-List-NonEmpty.html
Awesome, thanks a lot for pointing this out! semigroups? I don't even
know what that would be, pretty sure I wouldn't have found this by
myself. :-}
--
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing
------------------------------
Message: 3
Date: Fri, 7 Jun 2013 08:24:03 +0200
From: Michael Peternell <[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: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi,
how about
import Data.Char
let input = " 'John Doe-Smith' $"
unwords.words $ reverse . dropWhile (not.isAlpha) . reverse $ dropWhile
(not.isAlpha) input
Result: "John Doe-Smith"
-Michael
Am 07.06.2013 um 00:36 schrieb "Costello, Roger L." <[email protected]>:
> 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
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Fri, 7 Jun 2013 08:50:11 +0200
From: David Virebayre <[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:
<CAM_wFVvSZZCumAxBdDXHm+qHQS0FszP9K9C6Chm2dkKc6D=o...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
How about
import qualified Data.Text as T
import Data.Char
let s=" \" John Doe-Smith \" "
T.unpack . T.unwords . T.words . T.dropWhileEnd (not. isAlpha) .
T.dropWhile (not . isAlpha) . T.pack $ s
"John Doe-Smith"
Cheers,
David.
2013/6/7 Michael Peternell <[email protected]>:
> Hi,
>
> how about
>
> import Data.Char
> let input = " 'John Doe-Smith' $"
> unwords.words $ reverse . dropWhile (not.isAlpha) . reverse $ dropWhile
> (not.isAlpha) input
> Result: "John Doe-Smith"
>
> -Michael
>
> Am 07.06.2013 um 00:36 schrieb "Costello, Roger L." <[email protected]>:
>
>> 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
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 60, Issue 12
*****************************************