Stream: ideas

Topic: zipWith and other list operations


view this post on Zulip Andrew Brent Gossage (Aug 06 2022 at 04:40):

Has anyone implemented a zipWith function or similar for working with two lists at once? I am a data scientist and most of my work requires list manipulation but I haven't been able to find an easy way to do that. A good example of why you might want to do that is to multiply two lists together so that you can make a dot product.

view this post on Zulip Andrew Brent Gossage (Aug 06 2022 at 06:14):

I am brand new to this language but here is some basic stuff I was able to come up with on my own. I would like to see if anyone else has something they have done like this too.

range =\ acc, start, stop ->
    if start < stop then
        List.append acc start
        |> range (start+1) stop
    else
        acc

doAtIndex=\ a,b, ind, opp ->
    opp (Result.withDefault (List.get a ind) 0) (Result.withDefault (List.get b ind) 0)


zipAtIndex =\ a, b, ind ->
    [(Result.withDefault (List.get a ind) 0), (Result.withDefault (List.get b ind) 0)]

zip =\ a, b ->
    List.map (range [] 0 (List.len a)) \ ind -> zipAtIndex a b ind

zipWith =\ a,b,opp -> List.map (range [] 0 (List.len a)) \ ind -> doAtIndex a b ind opp


dot =\ a, b, bias ->
    (List.walk (zipWith a b Num.mul)  { sum: 0} \state, elem ->
        { state & sum: state.sum + elem }).sum + bias

view this post on Zulip Brian Carroll (Aug 06 2022 at 07:10):

The standard library has map2. I think it's the same as zipWith, or at least similar.

view this post on Zulip Brian Carroll (Aug 06 2022 at 07:11):

There's also a List.range in the standard library.

view this post on Zulip Andrew Brent Gossage (Aug 06 2022 at 14:37):

Thanks


Last updated: Jun 16 2026 at 16:19 UTC