
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.

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?
I have no Idea, but this one works: intOfChar c | c >= '0' && c <= '9' = fromEnum c - fromEnum '0' intOfChar _ = undefined intOfString = sum . map (uncurry (*)) . zip (map (10^) [0..]) . map intOfChar . reverse ...assuming that 0..9 are consecutive in the Char Enum, which they are. Reading intOfString backwards, you get: reverse the string, change each digit to its integer value, pair it up with the right power of 10, multiply those pairs, and finally sum everything up. You can replace map f . zip with zipWith f , but that doesn't change much. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

Achim Schneider
Anonymous Anonymous
wrote: fromIntToString :: Int -> String
intOfString = sum . map (uncurry (*)) . zip (map (10^) [0..]) . map intOfChar . reverse
/me hides under a stone and tries again, this time with the correct problem: import Data.List charOfInt :: Int -> Char charOfInt c | c >= 0 && c <= 9 = toEnum (c + fromEnum '0') charOfInt _ = undefined stringOfInt :: Int -> String stringOfInt = let f (-1) = Nothing f n = let (r,c) = n `divMod` 10 in Just $ if r == 0 then (c, (-1)) else (c, r) in map charOfInt . reverse . unfoldr f That -1 thing is clearly inelegant, but I don't feel like struggling with off-by-one errors, right now. Negative numbers are left as an exercise. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

On Sun, 22 Mar 2009, Achim Schneider wrote:
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. I know such function exist, however let's assume it didn't exist. How would I write such function?
I have no Idea, but this one works:
intOfChar c | c >= '0' && c <= '9' = fromEnum c - fromEnum '0' intOfChar _ = undefined
this is 'digitToInt' from Char module

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.

Anonymous Anonymous
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

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

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

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.

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

It never looked like a homework assignment to me, if someone puts as much original thought into a mail than the OP, it's hard to believe he wouldn't have mentioned the fact in case it was indeed homework. Speaking of it, I don't really care whether something is homework or not, what I care about is the willingness to figure out stuff by oneself. I just don't like talking to TV sets. Not being able to explain "your own" code and/or not being able to describe how you came up with it is a dead give-away to capable instructors, anyway. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

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?
participants (8)
-
Achim Schneider
-
Anonymous Anonymous
-
gsan@stillpsycho.net
-
Henning Thielemann
-
John Dorsey
-
Rafael Cunha de Almeida
-
Richard O'Keefe
-
Roel van Dijk