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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:44:46+00:00 2026-05-29T09:44:46+00:00

I implemented a binary to decimal function in Haskell and am currently working on

  • 0

I implemented a binary to decimal function in Haskell and am currently working on a function that would convert a decimal into a binary value. (I’m aware that these functionalities are available somewhere although they’re not part of Prelude.hs)

I came up with the following code for a C-type procedural language, but I have trouble adapting it into the functional paradigm.

while (n > 0)
{
    if (n % 2 == 1)
        str = str + "1";
    else
        str = str + "0";
    n = n / 2;
}

I ventured into functional programming in Haskell only recently so I’m quite new to the functional way of thinking. I attempted the above using both recursion and list comprehension, but I’m not sure on how to place the guards and the logic properly since this involves multiple conditions. I use an Int list to hold the separate binary bits.

--Decimal to binary
toBin:: Int -> [Int]
toBin 0 = [0]
toBin n | (n % 2 == 1) =
        |(n % 2 == 0) = 

I’ve understood that the above pattern would let the program choose either guard and end evaluating the function. Am I on the wrong track here?

Below is what I came up with primitive recursion to convert any base (less than 10, in place of the 2) to decimal.

toDecimal :: [Int] -> Int
toDecimal [] = 0
toDecimal (x:xs) = (x * 2 ^(length xs)) + bin xs
  • 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-05-29T09:44:47+00:00Added an answer on May 29, 2026 at 9:44 am

    There’s no % operator; you’re probably looking for `mod` instead:

    toBin 0 = [0]
    toBin n | n `mod` 2 == 1 = ...
            | n `mod` 2 == 0 = ...
    

    Guards let you choose between multiple branches of a function. In this case, each ... will be the result of toBin n if its corresponding condition is true. To append two lists together, you can use the ++ operator, and `div` corresponds to integer division:

    toBin 0 = [0]
    toBin n | n `mod` 2 == 1 = toBin (n `div` 2) ++ [1]
            | n `mod` 2 == 0 = toBin (n `div` 2) ++ [0]
    

    However, this has a few problems. For a start, it always starts the result with 0, which is redundant; additionally, using ++ [1] is slow, since it has to go through the entire list to add an element on to the end; it would be better to prepend each element as we go, and then reverse the result at the end.

    To fix both these things, we’ll split toBin up into a main function and a helper function:

    toBin 0 = [0]
    toBin n = reverse (helper n)
    
    helper 0 = []
    helper n | n `mod` 2 == 1 = 1 : helper (n `div` 2)
             | n `mod` 2 == 0 = 0 : helper (n `div` 2)
    

    In this version, we use the : operator, which takes a value and a list, and returns the list with the value prepended to the beginning. We also return an empty result for 0 in our helper, and handle the 0 case in toBin instead, so that there’s no more 0s than necessary in the result.

    We can simplify helper‘s code by skipping the guards altogether, since we just write the result of n `mod` 2 again on the right-hand side:

    helper 0 = []
    helper n = (n `mod` 2) : helper (n `div` 2)
    

    Finally, there’s a function that does a div and a mod in one go, which can be more efficient:

    helper 0 = []
    helper n = let (q,r) = n `divMod` 2 in r : helper q
    

    As an additional note, this doesn’t really convert decimal to binary, it converts an integer to binary; Haskell implementations are unlikely to store integers in decimal format, although they are written and printed in that format. To write a full conversion of decimal to binary, a function that parses a decimal string into an integer would be required.

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

Sidebar

Related Questions

I have an iPhone application that I am currently converting to a universal binary
I'm working in a language that can only do binary math on 16-bit numbers
I implemented binary tree data structure in Haskell. My code: module Data.BTree where data
I've written a working program in Python that basically parses a batch of binary
Does anyone know how the standard binary search function is implemented? This is the
I have a Binary Search Tree that I'm making and I implemented the Insert
How would I implement a binary search using just an array?
I've been trying to implement a delete function for a Binary Search Tree but
How can I prevent a auto implemented property from being serialized by the binary
Construct is a DSL implemented in Python used to describe data structures (binary and

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.