HI && TypeCast
Iavor Diatchki
diatchki@cse.ogi.edu
Wed, 06 Aug 2003 21:42:11 +0000
hello,
Fredrik Petersson wrote:
> Hi there!
> Iam new to the list so feel free to shout at me when i do wrong! :)
> Software-designer from sweden, likes fast bikes and metal, thats me, and hi
> to you all!
welcome
> Yeah ok to the problem,
> i have this stupid code,
> [c | (c,i) <- l]
>
> Where (c,i) are a tuple from a (Char,Int) and l is a [(Char,Int)]
> So far everthings nice but i would like to cast the c to a Sting and add :
> in front,
> So if i fry with a list like [('d',3)('f',3)]
> I end up with
> "fg" but i want it to look like
> ":f :g"
>
> piece of cake, huh?
> Give me a hit or a webpage to read more from...?
> How do you TypeCast in haskell?
no type casting in haskell. and we think of that as a feature, not a
bug :-)
having said that, you can write functions that convert values of one
type into values of another. for example:
toString :: Char -> String
toString c = [c]
in Haskell the type String is the same as [Char], i.e. a string is a
list of characters.
another useful predefined function is:
concat :: [[a]] -> [a]
that given a list of lists will produce a "flattened" list that contains
all the elements, e.g. concat [[1,2],[3,4]] = [1,2,3,4].
in particular when you take 'a' above to be Char you get:
concat :: [String] -> String
which takes a list of strings and gives you back a single string
containing all the input strings.
now you can achieve what you wanted as:
concat [ ":" ++ toString c ++ " " | (c,i) <- l ]
++ joins two lists into a single list. there are a lot of useful
functions in the Prelude.
hope this helped. by the way a better list to post questions when you
are stuck with a program is probably haskell-cafe@haskell.org, but
people will usually reply on either list.
bye
iavor
--
==================================================
| Iavor S. Diatchki, Ph.D. student |
| Department of Computer Science and Engineering |
| School of OGI at OHSU |
| http://www.cse.ogi.edu/~diatchki |
==================================================