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! 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? Cheers! //FRedde
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 | ==================================================
On Wed, 6 Aug 2003 23:24:13 +0200 "Fredrik Petersson" <fredde@x-web.se> 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!
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"
":d :f", ay? ;)
piece of cake, huh? Give me a hit or a webpage to read more from...?
www.haskell.org/learning
How do you TypeCast in haskell?
You don't. Anyway's what you want isn't a "typecast" in any language. concat [[':',c] | (c,i) <- l] -- actually this will give you ":f:g" -- concat (intersperse " " [[':',c] | (c,i) <- l]) will give you ":f :g" -- I think intersperse is in module List (Data.List) by the way.
On Wed, 6 Aug 2003, 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!
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,
List comprehensions are overrated =) How about this: concatMap ( (':':) . (:[]) . fst ) [('d',3),('f',3)] concatMap maps a function to each element and then concatenates the list. So the function will first use fst to extract the first element from a tuple, then it will use (:[]) on the result to create a singleton list of the element, then it will use (':':) which will add ':' to the front of the list... Now what's not clear about THAT =) (okay so maybe list comprehensions would be clearer in this case)
How do you TypeCast in haskell?
BWUAHAHAHA!! HAHAHAA! *tears* =) -- ---------------------------------------- | Sebastian Sylvan | | ICQ: 44640862 | | Tel: 073-6818655 / 031-812 817 | | | | | | Hard Work Often Pays Off After Time | | But Laziness Always Pays Off Now! | ----------------------------------------
participants (4)
-
Derek Elkins -
Fredrik Petersson -
Iavor Diatchki -
Sebastian Sylvan