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.  Showing control characters (Russ Abbott)
   2.  Re: Showing control characters (Russ Abbott)
   3. Re:  Friend Modules? (Brent Yorgey)
   4. Re:  Re: Showing control characters (Brent Yorgey)
   5.  Typeclass Hierarchies on Gtk2Hs (John Smith)
   6.  How to select items from one list depending on   another
      (C K Kashyap)
   7. Re:  How to select items from one list depending  on another
      (David Virebayre)


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

Message: 1
Date: Tue, 9 Nov 2010 09:40:47 -0800
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] Showing control characters
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Is there some way to include control characters in a Show string so that
they take effect when the element is Shown?

For example, if I define a String "a\nb" and use putStrLn on it, the \n
causes a return, but when I simply return the string from a function, the \n
is shown as \n.  Is there a way to get the \n to execute when it's embedded
in a returned value?

Thanks
*
-- Russ Abbott
_____________________________________________*
*  Professor, Computer Science
  California State University, Los Angeles

  Google voice: 424-235-5752 (424-cell-rja)
  blog: http://russabbott.blogspot.com/
  vita:  http://sites.google.com/site/russabbott/
_____________________________________________*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101109/cf03f536/attachment-0001.html

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

Message: 2
Date: Tue, 9 Nov 2010 11:44:42 -0800
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] Re: Showing control characters
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I wasn't at a computer with Haskell when I wrote the earlier message.
 Here's how it seems to work.

File:

*data *MyType = MyType

*instance *Show MyType *where*
    show MyType = "a\nb"



Load the above file. Then:

> MyType        -- This does what I want it to do after all.
a
b

> show MyType
"a\nb"

> putStrLn (show MyType)
a
b

> print MyType
a
b



*-- Russ Abbott
_____________________________________________*
*  Professor, Computer Science
  California State University, Los Angeles

  Google voice: 424-235-5752 (424-cell-rja)
  blog: http://russabbott.blogspot.com/
  vita:  http://sites.google.com/site/russabbott/
_____________________________________________*



On Tue, Nov 9, 2010 at 9:40 AM, Russ Abbott <[email protected]> wrote:

> Is there some way to include control characters in a Show string so that
> they take effect when the element is Shown?
>
> For example, if I define a String "a\nb" and use putStrLn on it, the \n
> causes a return, but when I simply return the string from a function, the \n
> is shown as \n.  Is there a way to get the \n to execute when it's embedded
> in a returned value?
>
> Thanks
> *
> -- Russ Abbott
> _____________________________________________*
> *  Professor, Computer Science
>   California State University, Los Angeles
>
>   Google voice: 424-235-5752 (424-cell-rja)
>   blog: http://russabbott.blogspot.com/
>   vita:  http://sites.google.com/site/russabbott/
> _____________________________________________*
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20101109/22efcbb3/attachment-0001.html

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

Message: 3
Date: Tue, 9 Nov 2010 22:45:40 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Friend Modules?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Tue, Nov 09, 2010 at 07:00:59PM +0200, John Smith wrote:
> I have a module which defines data structures and their functions for
> a program. I have another module which defines show methods for the
> structures. Both modules have a substantial amount of code, and I
> would prefer to keep them separate. However, the show methods require
> access to internal data structures, which would not normally be
> exported. Furthermore, many modules do not require the show
> functions. Is there any solution to this, other than an intermediate
> module which imports the data module, and only exports the required
> names?

I have wanted this, too (in the context of writing tests, where I want
to test the unexported internals of a module).  But unfortunately
there is no way to do it other than the method you cite (to have a
module which just imports the other module and only exports certain
things).  In several of my projects I follow the pattern of having a
bunch of modules that export everything, and then a single module that
imports all the other modules and only re-exports selected things; the
intention is that users will just import the single module.

-Brent


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

Message: 4
Date: Tue, 9 Nov 2010 22:56:33 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Re: Showing control characters
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Tue, Nov 09, 2010 at 11:44:42AM -0800, Russ Abbott wrote:
> I wasn't at a computer with Haskell when I wrote the earlier message.
>  Here's how it seems to work.
> 
> File:
> 
> *data *MyType = MyType
> 
> *instance *Show MyType *where*
>     show MyType = "a\nb"

There's nothing too magical going on here. Remember that show just
converts a value to a String.  How that String is then displayed
depends on what you do with it, not with the definition of show.

> 
> 
> 
> Load the above file. Then:
> 
> > MyType        -- This does what I want it to do after all.
> a
> b

ghci essentially does "putStrLn . show" on the results of expressions
typed at the prompt.  So the call to show results in the string
consisting of an a, a newline, and a b, and putStrLn prints that
string to the screen (with the newline interpreted appropriately).

> 
> > show MyType
> "a\nb"

'show MyType' is an expression of type String.  Doing putStrLn . show
on it causes show to be called on the string, which converts it to a
form surrounded by quotes and with control characters escaped.  The
resulting string is then printed to the screen.

> 
> > putStrLn (show MyType)
> a
> b

This is the same as the first example, since IO actions entered at the
ghci prompt are simply run.

> 
> > print MyType
> a
> b

This is also the same, since print = putStrLn . show.


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

Message: 5
Date: Wed, 10 Nov 2010 11:02:50 +0200
From: John Smith <[email protected]>
Subject: [Haskell-beginners] Typeclass Hierarchies on Gtk2Hs
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I'm learning Gtk2Hs, and there is an interesting pattern to the types. Each 
type of widget has its own typeclass, e.g. 
Button and ButtonClass, Window and WindowClass. Is this idiomatic Haskell? Are 
there other ways to achieve such a 
hierarchy? (OO languages support such inheritance without requiring an 
interface for every class.)



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

Message: 6
Date: Wed, 10 Nov 2010 20:35:26 +0530
From: C K Kashyap <[email protected]>
Subject: [Haskell-beginners] How to select items from one list
        depending on    another
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,
I have this problem at hand -

Given two lists

list1 = [0,1,2,0,1,4]

list2 = [1,2,3,4,5,6]

I need to take items from the second list only when the corresponding
item in list1 is non-zero.

list3 = map snd $ filter takeValid $ zip list1 list2 where
                       takeValid (a,b) = a /= 0

Is there a shorter way?


-- 
Regards,
Kashyap


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

Message: 7
Date: Wed, 10 Nov 2010 16:13:39 +0100
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] How to select items from one list
        depending       on another
To: C K Kashyap <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

2010/11/10 C K Kashyap <[email protected]>:
> Hi,
> I have this problem at hand -
>
> Given two lists
>
> list1 = [0,1,2,0,1,4]
>
> list2 = [1,2,3,4,5,6]
> I need to take items from the second list only when the corresponding
> item in list1 is non-zero.

Another wayOne way to do it, although not shorter :

list3 = catMaybes $ zipWith (\a b -> if a > 0 then Nothing else Just
b) list1 list2

David.


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

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


End of Beginners Digest, Vol 29, Issue 12
*****************************************

Reply via email to