Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:56:08+00:00 2026-06-17T20:56:08+00:00

Background: Beginner level at SML My assignment requires me to use ListPair.foldr and only

  • 0

Background: Beginner level at SML

My assignment requires me to use ListPair.foldr and only this function to implement the zipWith function.

ListPair.foldr : ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
zipWith : ('a * 'b -> 'c) -> 'a list -> 'b list -> 'c list

ListPair.foldr returns a single ‘c element, while zipWith returns a ‘c list, so naturally my approach would use ListPair.foldr repeatedly to spill out individual ‘c elements which I then put in my ‘c list.
ListPair.foldr takes a pair of lists and folds them over each other according to the supplied function, so the only way to get the desired effect would be to take one element from each list at a time, feed it to ListPair.foldr as a pair of lists, take the result, and concatenate it to the next round.
I’d also have to convert the function from (‘a*’b->’c) to (‘a*’b*’c->’c).
Like so:

fun zipWith f [] l2 = []
| zipWith f l1 l2 = 
let val f2 = fn (a,b,c)=> f(a,b)+c   (* converting the function *)
    val xh = [hd(l1)]      (*first element of 'a list, as a list itself *)
    val yh = [hd(l2)]      (*first element of 'b list, as a list itself *)
    val xt = tl(l1)        (*taking the tail of 'a list*)
    val yt = tl(l2)        (*taking the tail of 'b list*)
in
    ListPair.foldr f2 0 (xh, yh)::    (*perform the operation with the two heads*)
    zipWith f xt yt                   (*recursively call zipWith with the remainder*)
end;

This works.

- zipWith (fn (x,y)=>x+y) [1,2,3] [10,20,30];
val it = [11,22,33] : int list

But now the tricky part: I’m not supposed to do this recursively, that is, I can’t call zipWith within my zipWith function.
Is this even possible? From what I read, the actual zipWith function in Haskell is defined recursively; how do I do this non-recursively?

I can’t imagine the professor urging us to do this in an object-oriented way with while loops and such (I tried anyway, my level is not adequate for even that).

Am I in the completely wrong direction? How should I approach this question?

—————–Answered—————-

I actually tried pad’s solution at first:

fun zipWith f l1 l2 = 
let val f2 = fn (a,b,c)=> f(a,b)::c
in
    ListPair.foldr f2 0 l1 l2
end;

But it didn’t work because I was appending it to 0 instead of []. The types didn’t work out and I couldn’t figure it out!

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:56:09+00:00Added an answer on June 17, 2026 at 8:56 pm

    Your approach is correct but unnecessarily complex. Function zipWith is recursive but you can define it non-recursively because ListPair.foldr already has recursive nature.

    To get closer to zipWith, you need a specialized version of ListPair.foldr with the following signature

    ListPair.foldr : 
       ('a * 'b * 'c list -> 'c list) -> 'c list -> 'a list * 'b list -> 'c list
    

    It means you pass an empty list as an accumulator and build up bigger lists along the way. In zipWith f xs ys, f has 'a * 'b -> 'c signature, so it is very easy to adapt:

    fun zipWith f xs ys =
        ListPair.foldr (fn (a, b, cs) => f(a, b)::cs) [] (xs, ys)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a beginner level task. I am trying to implement a calendar widget
I apologize for yet, another beginner question. I have a .Net background and this
Little background: this is an absolute beginner's website. I am a Film major haha
I am a beginner to C++/CLI as I come from a C# background. I
Here's my background: Decent experience with PHP/MySql. Beginner's experience with OOP Why I want
Okay here is a softball beginner WPF question. By default the background of the
Im a beginner programmer, and my question is: Which image format shall i use
I want to implement an application in Flex/Rails in Background as I know these
I am a beginner in this field. I created a HTML page and on
I'm a beginner with this and trying to understand how WPF and XAML work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.