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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:03:04+00:00 2026-05-29T21:03:04+00:00

I’m attempting to create a method in Haskell which I would think is very

  • 0

I’m attempting to create a method in Haskell which I would think is very intuitive. I have a list of lists [of lists]* that is arbitrarily deep. I’d like to pull out a specific atom in the list regardless of the depth of the recursion. Here’s my effort:

type List = [Array]
data Array = Int | List

drill :: Array -> [Int] -> Array
drill xs (index:[]) = xs !! index
drill xs (index:is) = drill (xs !! index) is

However I receive the following when loading in ghci:

drill.hs:5:23:
Couldn't match expected type `[Array]' with actual type `Array'
In the first argument of `(!!)', namely `xs'
In the expression: xs !! index
In an equation for `drill': drill xs (index : []) = xs !! index

What I’ve written seems intuitive to me but clearly Haskell is having type issues. And as a Haskell newbie, I’m not the best at interpreting type errors. I would think that the return type of the function could be either an atom: Int, or a list:[Int]. Can anybody help me decipher this and suggest a solution?

  • 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-29T21:03:05+00:00Added an answer on May 29, 2026 at 9:03 pm

    When you say

    data Array = Int | List
    

    this doesn’t mean “any Int is an Array, and any List is an Array“. It instead means that Array has two constructors, Int and List, both of which take no arguments. The confusion arises from the fact that they have the same name as types; they’re not related to the types beyond sharing a name. That is, your declaration is basically the same as

    data Array = Foo | Bar
    

    Instead, we need to give explicit constructor names for each alternative:

    type List = [Array]
    data Array = Elem Int | Nest List
    
    drill :: Array -> [Int] -> Array
    drill xs [] = xs
    drill (Nest xs) (index:is) = drill (xs !! index) is
    

    This means that Elem takes an Int and returns an Array, and Nest takes a List and returns an Array. We have to adjust the drill function to explicitly take the list of Arrays out of the Nest case.

    The error you’re getting is basically saying that you’re trying to use xs as a list, but it’s an Array. This is because you didn’t pattern-match on it at all: just tried to use your argument (an Array) directly as a list.

    Note that you’ll still have to explicitly pattern-match on the result of drill to tell whether it’s a single element or a nested list. There’s no way to make a type whose values can literally be either integers or nested lists in Haskell; Haskell’s unions are tagged (i.e. the constructors are explicit), and it has no subtyping.

    For more information, I suggest reading this introduction to algebraic data types from Learn You a Haskell.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.