Re: [Haskell-cafe] Function to cast types

2009-03-23 Thread Richard O'Keefe


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?

 
  
___

Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread John Dorsey
Rafael Cunha de Almeida wrote:

> This e-mail is offtopic, but I suppose being a little offtopic every now
> and then isn't that bad, specially if the subject is interesting. So,
> here I go.

It's the cafe'.  Very little is strictly off-topic here, and certainly
not this.

I'm happy to help anyone who's trying to learn, but I will not willingly
subvert anyone's classroom rules, as a matter of academic honesty.  I
think that matters at any level of school.  I accept that it can't be
enforced perfectly, or anywhere near so.

I didn't mean to accuse the original poster, and I trust I didn't come
accross that way.  I don't think it's a bad idea to point out that an
anonymous question like this one may look suspicious, or that many
instructors follow this forum.  None of that should give offense.

But I also provided some hints.  I think a good and fun way to help with
problems like that is to iteratively guide someone to their answer,
instead of giving it to them straight-up.

Just to be clear, I also have no criticism of the other replies.  Anyone
giving voluntary help should and will apply their own rules.

> The only reason people would want the answer of an assignment instead of
> actually doing it themselves is if they don't care about actually doing
> it. That is, they don't want to learn haskell, they just want to pass a
> class. In that case, do we really want to _make_ them learn?

In that case I have no interest in forcing them to learn or in helping
them get a grade.  But I'm always optimistic that someone's willing to
learn.  I've been proven wrong, but not often.

Cheers,
John

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread Rafael Cunha de Almeida
John Dorsey wrote:
> Anonymous One,
> 
>> I'm new to haskell, I'm wondering how can you write a function that will do
>> the following:
>> fromIntToString :: Int -> String
> 
> Is this a homework assignment, perchance?  Please take a look at
> http://www.haskell.org/haskellwiki/Homework_help
> Everyone here is glad to help, but we're also sensitive on academic
> honesty.
> 

This e-mail is offtopic, but I suppose being a little offtopic every now
and then isn't that bad, specially if the subject is interesting. So,
here I go.

The only reason people would want the answer of an assignment instead of
actually doing it themselves is if they don't care about actually doing
it. That is, they don't want to learn haskell, they just want to pass a
class. In that case, do we really want to _make_ them learn?

You could argue that helping someone pass some class without learning is
not worth your time. I wouldn't try to convince you otherwise. But if
you thought the assignment was interesting and you did it yourself, I
see no problem with sharing that with the person who asked it. The
bottom line is that I don't believe in making people learn things they
are not interested in learning in the first place.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread Anonymous Anonymous
Hello,

an answer from me, this is NOT the homework assignment. For my own homework
assignment, I had to create custom data types and convert a string
containing my datatype into my datatype. I will not expose my datatype, but
it is something with logical operators.

My approach would be using "operator recognition", I've written a function
that can search those "operators". However I was wondering how they would do
it with intergers because intergers are in a infinity set! But I couldn't
figure it out =(.

Thanks for answering you all!

2009/3/22 John Dorsey 

> Anonymous One,
>
> > I'm new to haskell, I'm wondering how can you write a function that will
> do
> > the following:
> > fromIntToString :: Int -> String
>
> Is this a homework assignment, perchance?  Please take a look at
> http://www.haskell.org/haskellwiki/Homework_help
> Everyone here is glad to help, but we're also sensitive on academic
> honesty.
>
> With that said, you've done the first step, which is writing down the
> function's type.  You don't want to iterate over all possible Int
> values, so maybe a recursive definition is in order.  What are the cases
> to consider?  Like, what happens if you have a negative number...
>
> > this is a cast function to cast an Int to a String. I know such function
>
> By the way, "cast" is probably the wrong word for what you want.  You're
> building a string representation of an Integer; in my view it's not "just"
> a type conversion.
>
> Good luck,
> John
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread Anonymous Anonymous
Thank you all for your answers, you've all been a great help to me!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread John Dorsey
Anonymous One,

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

Is this a homework assignment, perchance?  Please take a look at
http://www.haskell.org/haskellwiki/Homework_help
Everyone here is glad to help, but we're also sensitive on academic
honesty.

With that said, you've done the first step, which is writing down the
function's type.  You don't want to iterate over all possible Int
values, so maybe a recursive definition is in order.  What are the cases
to consider?  Like, what happens if you have a negative number...

> this is a cast function to cast an Int to a String. I know such function

By the way, "cast" is probably the wrong word for what you want.  You're
building a string representation of an Integer; in my view it's not "just"
a type conversion.

Good luck,
John

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread Gökhan San
Anonymous Anonymous  writes:

> fromIntToString :: Int -> String

...

> PS: if possible please do not use any casting functions that are predefined.

This is a rather simplified version of the Show instance of Int from the
libs:

itos :: Int -> String
itos x | x < 0 = '-' : itos (negate x)
   | x < 10= "0123456789" !! x : ""
   | otherwise = let (q, r) = x `quotRem` 10
 in itos q ++ itos r

-- 

Gökhan San
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Function to cast types

2009-03-22 Thread Roel van Dijk
> 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.

I'll assume that by a cast you mean a conversion from one type to
another where no information is lost.

-- Let's assume that we can represent every Int using a String
intToString :: Int -> String
intToString n = ...

-- Not every member of String can be interpreted as an Int, for
-- example "aabc" is nonsense when viewed as an Int.
stringToInt :: String -> Maybe Int
stringToInt s = ...

-- This property should hold for every Int. It states that given some
-- Int called n we can convert it to a string and back without losing
-- information.
prop_intString :: Int -> Bool
prop_intString n = maybe False (== n) . stringToInt . intToString $ n

Using such a property can be really useful to test your implementation
of intToString and stringToInt.

> I know such function
> exist, however let's assume it didn't exist. How would I write such
> function? Cause I have no idea, because there are infinity possibilities if
> I do it like:
>
> fromIntToString x | x == 1 = "1"
>  | x == 2 = "2"
> -- And so on...
It might be useful to first write this function first:

intToDigits :: Int -> [Int]
intToDigits n = ...

It should convert an integer to a list of its digits (using normal base 10):

intToDigits 123 == [1, 2, 3]

Then you can write a function which takes such a list and converts it
to a string. (Hint: use 'map' in combination with a function of type
'Int -> Char')

You have to take extra care if you also want to support negative numbers.

> I've also thinked about defining the defining the data types Int and String
> (I know you cannot redefine them, at least I think so), however I've no
> succes.
You can create your own datatypes which behave roughly the same as Int
and String. You will never be able to create something which behaves
exactly as a String because it is treated a bit special (it has its
own syntax).

import Data.String

data MyInt = ...
data MyString = ...

instance Num MyInt where ...
instance IsString MyString where ...

By creating an instance of Num for your own type MyInt you'll be able
to use the familiar operators +, - etc... If you want all the
functionality that normal Ints have for your own type you'll also need
instances of Bounded, Enum, Eq, Integral, Num, Ord, Read, Real, Show
and Ix. I might have forgotten a few :-) Some of those can be
automatically derived for you. (data MyInt = ... deriving Show, for
example)

I hope some of this is useful to you.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Function to cast types

2009-03-22 Thread Anonymous Anonymous
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. I know such function
exist, however let's assume it didn't exist. How would I write such
function? Cause I have no idea, because there are infinity possibilities if
I do it like:

fromIntToString x | x == 1 = "1"
 | x == 2 = "2"
-- And so on...

I've also thinked about defining the defining the data types Int and String
(I know you cannot redefine them, at least I think so), however I've no
succes.

Thank you in advance for answering my question!

PS: if possible please do not use any casting functions that are predefined.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe