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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:05:29+00:00 2026-06-12T01:05:29+00:00

I have a problem with one of the Haskell basics: Fold + anonymous functions

  • 0

I have a problem with one of the Haskell basics: Fold + anonymous functions

I’m developing a bin2dec program with foldl.
The solution looks like this:

bin2dec :: String -> Int
bin2dec = foldl (\x y -> if y=='1' then x*2 + 1 else x*2) 0

I understand the basic idea of foldl / foldr but I can’t understand what the parameters x y stands for.

  • 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-12T01:05:30+00:00Added an answer on June 12, 2026 at 1:05 am

    See the type of foldl

    foldl :: (a -> b -> a) -> a -> [b] -> a

    Consider foldl f z list

    so foldl basically works incrementally on the list (or anything foldable), taking 1 element from the left and applying f z element to get the new element to be used for the next step while folding over the rest of the elements. Basically a trivial definition of foldl might help understanding it.

     foldl f z []     = z
     foldl f z (x:xs) = foldl f (f z x) xs
    

    The diagram from Haskell wiki might help building a better intuition.

    foldl f z

    Consider your function f = (\x y -> if y=='1' then x*2 + 1 else x*2) and try to write the trace for foldl f 0 "11". Here "11" is same as ['1','1']

      foldl f 0 ['1','1'] 
    = foldl f (f 0 '1') ['1']
    

    Now f is a function which takes 2 arguments, first a integer and second a character and returns a integer.
    So In this case x=0 and y='1', so f x y = 0*2 + 1 = 1

    = foldl f 1 ['1']
    = foldl f (f 1 '1') []
    

    Now again applying f 1 '1'. Here x=1 and y='1' so f x y = 1*2 + 1 = 3.

    = foldl f 3 [] 
    

    Using the first definition of foldl for empty list.

    = 3 
    

    Which is the decimal representation of “11”.

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

Sidebar

Related Questions

I started studying Haskell one week ago and have one strange problem. I created
I have a problem with implementing FP (Backus) interpreter in Haskell. FP functions look
I'm trying to migrate django apps, and i have problem with one. When i
I have one problem with parsing *.docx document with OpenXML (C#). So, here's my
I have one problem with Javascript Nodes.I want to find out what button was
i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I have one problem , I want to get some data from XML file
I have one problem, I learn how work with socket and I write programm
i have one problem when i sort the NSMutablearray using date and time wise.
I have one problem with android calendar programming, so If there are 2 months

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.