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

  • Home
  • SEARCH
  • 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 7858015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:13:52+00:00 2026-06-02T21:13:52+00:00

Brief: This is a past exam question from a Miranda exam but the syntax

  • 0

Brief: This is a past exam question from a Miranda exam but the syntax is very similar to Haskell.

Question: What is the type of the following expression and what does it do? (The definitions
of the functions length and swap are given below).

(foldr (+) 0) . (foldr ((:) . length . (swap (:) [] )) [])

length [] = 0

length (x:xs) = 1 + length xs

swap f x y = f y x

Note:

Please feel free to reply in haskell syntax – sorry about putting using the stars as polytypes but i didn’t want to translate it incorrectly into haskell. Basically, if one variable has type * and the other has * it means they can be any type but they must both be the same type. If one has ** then it means that it can but does not need to have the same type as *. I think it corresponds to a,b,c etc in haskell usuage.

My working so far

From the definition of length you can see that it finds the length of a list of anything so this gives

length :: [*] -> num.

From the definition I think swap takes in a function and two parameters and produces the function with the two parameters swapped over, so this gives

swap :: (* -> ** -> ***) -> ** -> [*] -> ***

foldr takes a binary function (like plus) a starting value and list and folds the list from right to left using that function. This gives

foldr :: (* -> ** -> **) -> ** -> [*] -> **)

I know in function composition it is right associative so for example everything to the right of the first dot (.) needs to produce a list because it will be given as an argument to the first foldr.

The foldr function outputs a single value ( the result of folding up the list) so I know that the return type is going to be some sort of polytype and not a list of polytype.

My problem

I’m unsure where to go from here really. I can see that swap needs to take in another argument, so does this partial application imply that the whole thing is a function? I’m quite confused!

  • 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-02T21:13:55+00:00Added an answer on June 2, 2026 at 9:13 pm

    You’ve already got the answer, I’ll just write down the derivation step by step so it’s easy to see all at once:

    xxf xs = foldr (+) 0 . foldr ((:) . length . flip (:) []) [] $ xs
           = sum         $ foldr ((:) . length . (: []))      []   xs
           = sum         $ foldr (\x -> (:) (length [x]))     []   xs
           = sum         $ foldr (\x r ->    length [x]:r)    []   xs
           = sum         $ map   (\x   ->    length [x]  )         xs
           = sum                            [length [x]  |    x <- xs]  
           = sum                            [ 1          |    x <- xs]
    --     = length xs
    xxf :: (Num n) => [a] -> n
    

    So that, in Miranda, xxf xs = #xs. I guess its type is :: [*] -> num in Miranda syntax.

    Haskell’s length is :: [a] -> Int, but as defined here, it is :: (Num n) => [a] -> n because it uses Num‘s (+) and two literals, 0 and 1.

    If you’re having trouble visualizing foldr, it is simply

    foldr (+) 0 (a:(b:(c:(d:(e:(...:(z:[])...))))))
          =      a+(b+(c+(d+(e+(...+(z+ 0)...)))))
          = sum [a, b, c, d, e, ..., z]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've done a brief search of this site, and googled this, but can't seem
I know this is a low-level question but, not being a database person, I
I know this question has been asked and answered here on StackOverflow.. but I
I've this brief snippet: String target = baseFolder.toString() + entryName; target = target.substring(0, target.length()
I'll keep this brief. I am trying to keep a map between strings and
I'll keep this brief: can anyone tell me why this doesn't work? The else
This has been pending for a long time in my list now. In brief
Here is my brief HTML document. Why is Chrome Console noting this error: Uncaught
Uhm I'm not sure if anyone has encountered this problem a brief description is
All the automated, online converters weren't able to convert this code. Unfortunately my brief

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.