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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:58:47+00:00 2026-05-13T09:58:47+00:00

I made a function similar to numpy’s array . It converts lists to arrays,

  • 0

I made a function similar to numpy’s array. It converts lists to arrays, lists of lists to 2d arrays, etc.

It works like this:

ghci> arrFromNestedLists ["hello", "world"] :: Array (Int, (Int, ())) Char
array ((0,(0,())),(1,(4,()))) [((0,(0,())),'h'),((0,(1,())),'e'),((0,(2,())),'l'),((0,(3,())),'l'),((0,(4,())),'o'),((1,(0,())),'w'),((1,(1,())),'o'),((1,(2,())),'r'),((1,(3,())),'l'),((1,(4,())),'d')]

(Int, (Int, ())) and not (Int, Int) because I don’t know of a programatic way to increase the length of a tuple. (side question: is there such way?)

The coding of it was awkward and I had to do a “workaround” (passing around dummy arguments to functions) for it to work. I wonder if there’s a better way.

So here’s the code, interrupted with details of the ugly workarounds:

{-# LANGUAGE FlexibleInstances, ScopedTypeVariables, TypeFamilies #-}

type family ListOfIndex i a
type instance ListOfIndex () a = a
type instance ListOfIndex (Int, i) a = [ListOfIndex i a]

class Ix i => ArrConv i where
  acBounds :: a -> ListOfIndex i a -> (i, i)
  acFlatten :: i -> ListOfIndex i a -> [a]

acBounds “should” be :: ListOfIndex i a -> (i, i). And similarly for acFlatten. Each is given a dummy variable (undefined is always the value given) because otherwise I couldn’t get it to compile 🙁

arrFromNestedLists :: forall i a. ArrConv i => ListOfIndex i a -> Array i a
arrFromNestedLists lst =
  listArray
  (acBounds (undefined :: a) lst)
  (acFlatten (undefined :: i) lst)

Above is the dummy undefined argument passing at work. It tells the GHC which instance of ListOfIndex to use.

instance ArrConv () where
  acBounds _ = const ((), ())
  acFlatten _ = (: [])

The below function should have been the acBounds function in an instance of ArrConv, and is declared outside only because I need to use ScopedTypeVariables and I don’t know how I can do it in a function in an instance definition..

acSucBounds
  :: forall a i. ArrConv i
  => a -> [ListOfIndex i a] -> ((Int, i), (Int, i))
acSucBounds _ lst =
  ((0, inStart), (length lst - 1, inEnd))
  where
    (inStart, inEnd) = acBounds (undefined :: a) (head lst)

instance ArrConv i => ArrConv (Int, i) where
  acBounds = acSucBounds
  acFlatten _ = concatMap (acFlatten (undefined :: i))
  • 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-13T09:58:48+00:00Added an answer on May 13, 2026 at 9:58 am

    The reason that the extra arguments to acBounds and acFlatten are necessary is that the types a and i cannot be recovered from ListOfIndex i a -> (i, i) and ListOfIndex i a -> [a] respectively. One workaround is to combine the two methods into one method acArgs of type ListOfIndex i a -> ((i, i), a). Now the only problem is to use it in the instance of (Int, i) in a way which prevents the typechecker from generalizing its type too much causing the same problem as before (for example, we cannot simply use fst . acArgs).

    {-# LANGUAGE TypeFamilies, FlexibleInstances #-}
    
    import Data.Array
    
    type family ListOfIndex i a
    type instance ListOfIndex () a = a
    type instance ListOfIndex (Int, i) a = [ListOfIndex i a]
    
    class Ix i => ArrConv i where
      acArgs :: ListOfIndex i a -> ((i, i), [a])
    
    instance ArrConv () where
      acArgs x = (((), ()), [x])
    
    instance ArrConv i => ArrConv (Int, i) where
      acArgs lst =
        (((0, inStart), (length lst - 1, inEnd)), args >>= snd)
        where
          args = map acArgs lst
          (inStart, inEnd) = fst (head args)
    
    arrFromNestedLists :: ArrConv i => ListOfIndex i a -> Array i a
    arrFromNestedLists = uncurry listArray . acArgs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this function to verify a user's twitter credentials. Its running on two
Hi have made this function which is made to replicate an error that I
I made this little function from code snippets around the net. It does what
I had tabs with preloaded content like this: $(function () { $('div.tabs ul.tabNavigation a').click(function
I want to add a similar function on my blog like on http://techcrunch.com/2011/08/04/groupon-acquires-software-devlopment-startup-obtiva/ when
There are questions LIKE this one, but they are not similar enough to my
This question is similar to a previous one I made here: randomly sum values
I made a function which displays date on the webpage,, and i uploaded the
I've made a function dump_text(std::ostream &) that dumps some text in a standard output
I have made one function in which I am passing a matrix and returning

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.