generating by mapping

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

generating by mapping

Szilveszter Juhos
Hi,

I have something like:

data Stuff = Stuff {
                     aNum :: Int
                   , anStr :: String
                   } deriving (Show)

let first = Stuff 123 "qwe"
let second = Stuff 321 "asd"

print first
print second

and works fine. What is the right "map" statement if I want to
generate these stuff using lists like:

let strs = [ "qwe", "asd", "zxc" ]
let nums = [ 123, 321, 345]

?
Cheers:
Szilva
Reply | Threaded
Open this post in threaded view
|

generating by mapping

Stephen Tetley-2
Hi Szilveszter

zipWith is probably what you are after...

> zipWith Stuff nums strs
[Stuff {aNum = 123, anStr = "qwe"},Stuff {aNum = 321, anStr =
"asd"},Stuff {aNum = 345, anStr = "zxc
"}]

Note zipWith (and the function zip which it generalizes) go 'short' -
i.e. if one of the input lists is shorter than the other - the size of
the result list will the size of of the shorter one:


> zipWith Stuff [1] strs
[Stuff {aNum = 1, anStr = "qwe"}]


Best wishes

Stephen
Reply | Threaded
Open this post in threaded view
|

generating by mapping

Tim Perry-2

Stephen's version is cleaner, but this works too:
map (\(x, y) -> Stuff x y) $ zip nums strs




----- Original Message ----
From: Stephen Tetley <[hidden email]>
To: Szilveszter Juhos <[hidden email]>
Cc: [hidden email]
Sent: Tue, March 16, 2010 2:09:51 AM
Subject: Re: [Haskell-beginners] generating by mapping

Hi Szilveszter

zipWith is probably what you are after...

> zipWith Stuff nums strs
[Stuff {aNum = 123, anStr = "qwe"},Stuff {aNum = 321, anStr =
"asd"},Stuff {aNum = 345, anStr = "zxc
"}]

Note zipWith (and the function zip which it generalizes) go 'short' -
i.e. if one of the input lists is shorter than the other - the size of
the result list will the size of of the shorter one:


> zipWith Stuff [1] strs
[Stuff {aNum = 1, anStr = "qwe"}]


Best wishes

Stephen
_______________________________________________
Beginners mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/beginners

Reply | Threaded
Open this post in threaded view
|

generating by mapping

Ozgur Akgun
If you're ready to write a lambda expression why not you just say:

zipWith (\ x y -> Stuff x y ) nums strs

Yet now this one naturally reduces to Stephen's version. Go with that one :)


On Tuesday, March 16, 2010, Tim Perry <[hidden email]> wrote:

>
> Stephen's version is cleaner, but this works too:
> map (\(x, y) -> Stuff x y) $ zip nums strs
>
>
>
>
> ----- Original Message ----
> From: Stephen Tetley <[hidden email]>
> To: Szilveszter Juhos <[hidden email]>
> Cc: [hidden email]
> Sent: Tue, March 16, 2010 2:09:51 AM
> Subject: Re: [Haskell-beginners] generating by mapping
>
> Hi Szilveszter
>
> zipWith is probably what you are after...
>
>> zipWith Stuff nums strs
> [Stuff {aNum = 123, anStr = "qwe"},Stuff {aNum = 321, anStr =
> "asd"},Stuff {aNum = 345, anStr = "zxc
> "}]
>
> Note zipWith (and the function zip which it generalizes) go 'short' -
> i.e. if one of the input lists is shorter than the other - the size of
> the result list will the size of of the shorter one:
>
>
>> zipWith Stuff [1] strs
> [Stuff {aNum = 1, anStr = "qwe"}]
>
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>

--
Ozgur Akgun