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 86723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:17:27+00:00 2026-05-10T22:17:27+00:00

A bit of a neophyte haskell question, but I came across this example in

  • 0

A bit of a neophyte haskell question, but I came across this example in Haskell’s tutorial examples. For ‘find the last element of a list’ there are some obvious versions, like

last' [x] = x last' (_:xs) = last' xs 

But I can’t make sense of an alternate version presented:

myLast' = foldr1 (const id) 

So, in trying to make sense of what the application of the id function is doing, I tried in ghci:

const id 1 2 -> gives 2 

This binds like this:

(const id) 1 2 -> gives 2 

And not like this:

 const (id 1) 2 -> gives 1  

But I’m not making sense of this. (const id) should translate to something like

`(\x y->x) (\x->x)`  

Shouldn’t this return a function that simply returns the id of its first element? Or, how is the function order making (const id) behave differently than const?

  • 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. 2026-05-10T22:17:28+00:00Added an answer on May 10, 2026 at 10:17 pm

    The definition of const is

    const x = \_ -> x 

    Hence, (const id) is a function which takes one argument and always returns id and

    const id 1 2 = (\_ -> id) 1 2              = id 2              = 2 

    The definition of foldr1 is

    foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) 

    If we have

    myLast' = foldr1 (const id) 

    then

    myLast' [x] = foldr1 (const id) [x]               {- definition of foldr1 -}             = x 

    and

    myLast' (x:xs) = foldr1 (const id) (x:xs)                  {- definition of foldr1 -}                = (const id) x (foldr1 (const id) xs)                  {- definition of const -}                  = (\_ -> id) x (foldr1 (const id) xs)                  {- function application -}                  = id (foldr1 (const id) xs)                  {- definition of id -}                  = foldr1 (const id) xs                  {- definition of myLast' -}                  = myLast' xs 

    which agrees with the definition of last'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of a basic question but I cant seem to find a straight answer
Bit tricky question but this saves lot of time if you have to modify
Bit of an unusual question, but I have setup a field inside a MySQL
Bit of a javascript newbie so not sure if this question is easy or
Bit of a strange question here i know. but i wanted to know if
Bit of an odd question but is there a way of seeing what objects
Bit of a bizarre question, but does anyone know the actual limit to the
Bit of an edge case, but any idea why &&= would behave this way?
Bit of a dumb question, but I'm wondering what the accepted way of passing
Bit of an odd question but I'm hoping someone can point me in the

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.