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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:21:50+00:00 2026-06-14T20:21:50+00:00

I have the following function in Haskell agreeLen :: (Eq a) => [a] ->

  • 0

I have the following function in Haskell

agreeLen :: (Eq a) => [a] -> [a] -> Int
agreeLen x y = length $ takeWhile (\(a,b) -> a == b)  (zip x y)

I’m trying to learn how to write ‘idiomatic’ Haskell, which seem to prefer using . and $ instead of parenthesis, and also to prefer pointfree code where possible. I just can’t seem to get rid of mentioning x and y explicitly. Any ideas?

I think I’d have the same issue with pointfreeing any function of two arguments.

BTW, this is just in pursuit of writing good code; not some “use whatever it takes to make it pointfree” homework exercise.

Thanks.


(Added comment)
Thanks for the answers. You’ve convinced me this function doesn’t benefit from pointfree. And you’ve also given me some great examples for practicing transforming expressions. It’s still difficult for me, and they seem to be as essential to Haskell as pointers are to C.

  • 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-14T20:21:51+00:00Added an answer on June 14, 2026 at 8:21 pm

    and also to prefer pointfree code where possible.

    Not “where possible”, but “where it improves readability (or has other manifest advantages)”.

    To point-free your

    agreeLen x y = length $ takeWhile (\(a,b) -> a == b)  (zip x y)
    

    A first step would be to move the ($) right, and replace the one you have with a (.):

    agreeLen x y = length . takeWhile (\(a,b) -> a == b) $ zip x y
    

    Now, you can move it even further right:

    agreeLen x y = length . takeWhile (uncurry (==)) . zip x $ y
    

    and there you can immediately chop off one argument,

    agreeLen x = length . takeWhile (uncurry (==)) . zip x
    

    Then you can rewrite that as a prefix application of the composition operator,

    agreeLen x = (.) (length . takeWhile (uncurry (==))) (zip x)
    

    and you can write

    f (g x)

    as

    f . g $ x
    

    generally, here with

    f = (.) (length . takeWhile (uncurry (==)))
    

    and g = zip, giving

    agreeLen x = ((.) (length . takeWhile (uncurry (==)))) . zip $ x
    

    from which the argument x is easily removed. Then you can transform the prefix application of (.) into a section and get

    agreeLen = ((length . takeWhile (uncurry (==))) .) . zip
    

    But, that is less readable than the original, so I don’t recommend doing that except for practicing the transformation of expressions into point-free style.

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

Sidebar

Related Questions

Trying to learn Haskell. I am trying to write a simple function to remove
I have the following function: func fitrange(a, x, b int) int { if a
I have the following function in a PHP script, which returns and API call:
I have two Haskell functions, both of which seem very similar to me. But
Say I have the following custom data type and function in Haskell: data Person
I have the following problem with Haskell's type system: I am trying to declare
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
I'm looking for some attractive Haskell syntax for the following I have a function
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus

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.