## Split a number into its digits with Haskell

haskell list of digits to int

Given an arbitrary number, how can I process each digit of the number individually?

Edit I've added a basic example of the kind of thing `Foo` might do.

For example, in C# I might do something like this:

```static void Main(string[] args)
{
int number = 1234567890;
string numberAsString = number.ToString();

foreach(char x in numberAsString)
{
string y = x.ToString();
int z = int.Parse(y);
Foo(z);
}
}

void Foo(int n)
{
Console.WriteLine(n*n);
}
```

Have you heard of div and mod?

You'll probably want to reverse the list of numbers if you want to treat the most significant digit first. Converting the number into a string is an impaired way of doing things.

```135 `div` 10 = 13
135 `mod` 10 = 5
```

Generalize into a function:

```digs :: Integral x => x -> [x]
digs 0 = []
digs x = digs (x `div` 10) ++ [x `mod` 10]
```

Or in reverse:

```digs :: Integral x => x -> [x]
digs 0 = []
digs x = x `mod` 10 : digs (x `div` 10)
```

This treats `0` as having no digits. A simple wrapper function can deal with that special case if you want to.

Note that this solution does not work for negative numbers (the input `x` must be integral, i.e. a whole number).

int -> [int] convert, Possible Duplicate: Split a number into its digits with Haskell Here's a general algorithm that you should be able to apply in Haskell: Convert the Convert each character back to an integer, while appending it to the end of a list. Good luck. digits' 0 = [] digits' n = n `rem` 10 : digits (n `quot` 10) digits n = reverse (​digits' n). Stack Overflow Public questions and answers Teams Private questions and answers for your team Enterprise Private self-hosted questions and answers for your enterprise

```digits :: Integer -> [Int]
digits = map (read . (:[])) . show
```

or you can return it into `[]`:

```digits :: Integer -> [Int]
digits = map (read . return) . show
```

or, with Data.Char.digitToInt:

```digits :: Integer -> [Int]
digits = map digitToInt . show
```

the same as Daniel's really, but point free and uses Int, because a digit shouldn't really exceed `maxBound :: Int`.

Split a number into its digits with Haskell - haskell - php, You'll probably want to reverse the list of numbers if you want to treat the most significant digit first. Converting the number into a string is an impaired way of  num = 123. output. 3. 2. 1. Let’s design a logic. num = 12. First let us think, how to split the last digit 2 from the number 12. Since the numbers are decimal (base 10), each digit’s range should be 0 to 9.

You could also just reuse `digits` from Hackage.

Data.Digits, The number to convert to digit form. -> Maybe [n]. Nothing or Just the digits of the number in list form. Returns the digits  sum of digits of 10fg (base 17) = 32 sum of digits of deadbeef (base 16) = 104 sum of digits of 1101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 (base 2) = 45 sum of digits of thequickbrownfoxjumpsoverthelazydog (base 36) = 788

Using the same technique used in your post, you can do:

```digits :: Integer -> [Int]
digits n = map (\x -> read [x] :: Int) (show n)
```

See it in action:

```Prelude> digits 123
[1,2,3]
```

Does that help?

Here is my solution, to keep me fresh in haskell (I rarely have the , re: Write a script to find "Happy Numbers" VIEW POST [Integer] -> Integer squareSum = sum . map (^2) -- Splitting n into its digits splitNum  Break or split number into individual digits with Kutools for Excel. The Split Cells utility of Kutools for Excel helps you easily split all selected number cells into individual digits at once. Please do as follows.

Textbook unfold

```import qualified Data.List as L
digits = reverse . L.unfoldr (\x -> if x == 0 then Nothing else Just (mod x 10, div x 10))
```

🧗 Haskell, Haskell - How to split a number into a list for further processing? I have Int that I want to divide into it separate numbers, which ideally will b The RealFrac subclass of Fractional and Real provides a function properFraction, which decomposes a number into its whole and fractional parts, and a collection of functions that round to integral values by differing rules: properFraction :: (Fractional a, Integral b) => a -> (b,a) truncate, round,

2 Day 1: Getting up and running, Alright, so, here's the thing: I'm not very good at Haskell. In fact, I A function to split a number into its digits in reverse order called, not surprisingly, toRevDigits. split 1234 into ('1', '2', '3', '4') P: 6. ss30. Hi, I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4'). Also, is there a way to test whether an element of a string (or list) is an integer.

[haskell] number of numbers in a string, I am busy learning Haskell, but I ran into the following problem: A sample the number of numbers in this string, which is 2 in this example Let's break it down:​. I don't want the number to turn a scalar into an array eg x = 1994 = 1 9 9 4 I want it the scalar x = 1994 to split into multiple scalars. I hope this makes sense and your help will be much appreciated

First Steps, The smallest and largest Int value depends on the programming language and Real numbers cannot be represented accurately on a computer with a fixed  2) Column function is used to return the column number of a reference. Let’s take an example to understand how we can separate the number into digits. Column A contains a list of numbers. Separating the number in A2 into digits, so that each of cells B2:G2 contains one digit. Follow below given steps:-Write the formula in cell B2.