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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:25:00+00:00 2026-06-08T04:25:00+00:00

I have written this small function to retrieve the tail of a list: let

  • 0

I have written this small function to retrieve the tail of a list:

let getTail l = if length l > 0 then tail l else "empty list"

Passing []to getTail returns empty list but passing [1,2,3] gives the following error:

<interactive>:1:14:
No instance for (Num Char)
  arising from the literal `3'
Possible fix: add an instance declaration for (Num Char)
In the expression: 3
In the first argument of `getTail', namely `[1, 2, 3]'
In the expression: getTail [1, 2, 3]

I can’t understand what that error means. What is the problem? Using GHCi 7.0.4

  • 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-06-08T04:25:03+00:00Added an answer on June 8, 2026 at 4:25 am

    Let’s think about the type of your getTail function. Initially, we can imagine l to be any list. The return type, however, has to be a String because you sometimes return "empty list". Since that’s part of an if statement where you might also return tail l, it means l has to be a String.

    So your getTail function has type String -> String.

    When you call getTail with [1,2,3], it expects a String, which is just a [Char]. Remember that numeric literals are overloaded: [1,2,3] is equivalent to calling [fromInteger 1, fromInteger 2, fromInteger 3]. So, given this list, Haskell is trying to get a number from a Char. Since Chars are not part of the Num class, this fails, giving you your error.

    A good solution would be to return a Maybe type and give Nothing for the error rather than returning "empty list". So rewrite your function as:

    let getTail l = if length l > 0 then Just (tail l) else Nothing
    

    Now its type is [a] -> Maybe [a]. Using this will force anybody using your function to check whether getTail succeeded before being able to use the result.

    Another option is to return [] in place of “empty list”. In this case, your getTail function will work exactly like drop 1.

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

Sidebar

Related Questions

So I am obviously not a very good programmer. I have written this small
I have written this function to auto correct gender to M or F from
I have written a small function, which uses ElementTree and xpath to extract the
I am still new to Javascript and JQuery. I have written a small function
I have written a small function in C# which isn't my main launguage so
I have written a small jQuery plugin with the following structure: (function($) { //
I have written a small function in Emacs that does a git stash git
I have written a small function in JQuery, took me a little while but
have written this little class, which generates a UUID every time an object of
I have written this code inside a servlet to delete certain records from three

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.