[Haskell-cafe] Function to cast types

Richard O'Keefe ok at cs.otago.ac.nz
Mon Mar 23 17:40:17 EDT 2009


On 23 Mar 2009, at 2:20 am, Anonymous Anonymous wrote:

> Hello,
>
> I'm new to haskell, I'm wondering how can you write a function that  
> will do the following:
>
> fromIntToString :: Int -> String
>
> this is a cast function to cast an Int to a String.

It cannot be.  What could it possibly mean to "cast" an Int
to anything, let alone a string?  Haskell isn't C.  (Nor is
it PL/I.)

What to do depends on what you _want_ to do.  For example,
	fromIntToString n = replicate n 'I'
will convert 1 to "I", 2 to "II", 3 to "III", and so on.

Assuming that you mean that you want a decimal representation
of the integer, Read The Fine Manual to find out what 'show'
will do.

This may well be a homework question, in which case consider:
   you want to construct an element of a recursively defined
   data type (list of character).
   do you *have* a recursively defined data type to start from?
   If you first distinguish between negative and non-negative
   integers, do you have a recursively defined data type then?
   How could you use `div` and `mod` to treat non-negative
   integers _as if_ they formed a recursively defined data type?
   What would the base case be?  What would the step case be?

  
   


More information about the Haskell-Cafe mailing list