firstname gklsodascb
firstname gklsodascb

Reputation: 121

Convert a list of position,value tuples into a single list

I am writing some code to work with arbitrary radix numbers in haskell. They will be stored as lists of integers representing the digits.

I almost managed to get it working, but I have run into the problem of converting a list of tuples [(a_1,b_1),...,(a_n,b_n)] into a single list which is defined as follows:

for all i, L(a_i) = b_i. if there is no i such that a_i = k, a(k)=0

In other words, this is a list of (position,value) pairs for values in an array. If a position does not have a corresponding value, it should be set to zero.

I have read this (https://wiki.haskell.org/How_to_work_on_lists) but I don't think any of these methods are suitable for this task.

baseN :: Integer -> Integer -> [Integer]
baseN n b = convert_digits (baseN_digits n b)

chunk :: (Integer, Integer) -> [Integer]
chunk (e,m) = m : (take (fromIntegral e) (repeat 0))

-- This is broken because the exponents don't count for each other's zeroes
convert_digits :: [(Integer,Integer)] -> [Integer]
convert_digits ((e,m):rest) = m : (take (fromIntegral (e)) (repeat 0)) 
convert_digits []       = []

-- Converts n to base b array form, where a tuple represents (exponent,digit).
-- This works, except it ignores digits which are zero. thus, I converted it to return (exponent, digit) pairs.
baseN_digits :: Integer -> Integer -> [(Integer,Integer)]

baseN_digits n b | n <= 0 = [] -- we're done.
          | b <= 0 = [] -- garbage input. 
          | True   = (e,m) : (baseN_digits (n-((b^e)*m)) b)
                     where e = (greedy n b 0)      -- Exponent of highest digit
                           m = (get_coef n b e 1)  -- the highest digit

-- Returns the exponent of the highest digit.
greedy :: Integer -> Integer -> Integer -> Integer
greedy n b e | n-(b^e) <  0  = (e-1) -- We have overshot so decrement.
             | n-(b^e) == 0  = e     -- We nailed it. No need to decrement. 
             | n-(b^e) >  0  = (greedy n b (e+1)) -- Not there yet.

-- Finds the multiplicity of the highest digit
get_coef :: Integer -> Integer -> Integer -> Integer -> Integer
get_coef n b e m | n - ((b^e)*m) < 0  = (m-1) -- We overshot so decrement.
                 | n - ((b^e)*m) == 0 = m    -- Nailed it, no need to decrement.
                 | n - ((b^e)*m) > 0  = get_coef n b e (m+1) -- Not there yet.

You can call "baseN_digits n base" and it will give you the corresponding array of tuples which needs to be converted to the correct output

Upvotes: 0

Views: 84

Answers (1)

moonGoose
moonGoose

Reputation: 1510

Here's something I threw together.

 f = snd . foldr (\(e,n) (i,l') -> ( e , (n : replicate (e-i-1) 0) ++ l')) (-1,[])
 f . map (fromIntegral *** fromIntegral) $ baseN_digits 50301020 10 = [5,0,3,0,1,0,2,0]

I think I understood your requirements (?)

EDIT: Perhaps more naturally,

f xs = foldr (\(e,n) fl' i -> (replicate (i-e) 0) ++ (n : fl' (e-1))) (\i -> replicate (i+1) 0) xs 0

Upvotes: 1

Related Questions