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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:30:23+00:00 2026-05-28T05:30:23+00:00

How to create a function in Haskell that returns the fifth element from a

  • 0

How to create a function in Haskell that returns the fifth element from a list.

Something like this:

fifth [] = []!!4

Should return this:

*Main> fifth [1,2,3,20,30,40]
30
  • 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-28T05:30:23+00:00Added an answer on May 28, 2026 at 5:30 am

    Simply use:

    fifth :: [a] -> a
    fifth l = l !! 4
    

    Using fifth [] like you suggest is wrong since that will pattern match the list against the empty list — you simply want to bind a variable name to the full list so that you can use the !! function afterwards.

    You can even define the function as:

    fifth :: [a] -> a
    fifth = (!!4)
    

    Here we use partial application: you normally think of !! as a function taking two arguments: a list and an integer. We can provide it with one of the arguments and get a new function (fifth) that only takes a list. When we provide (!!4) with a list, it returns the fifth element:

    Prelude> let fifth = (!!4)
    Prelude> fifth [1,2,3,20,30,40]
    30
    

    The function is of course a partial function since it will fail for small lists:

    Prelude> (!!4) [1,2,3,20]
    *** Exception: Prelude.(!!): index too large
    

    That’s to be expected. If you want, you can make it safe by letting it return Maybe a instead of a::

    fifth :: [a] -> Maybe a
    fifth (a:b:c:d:e:rest) = Just e
    fifth _ = Nothing
    

    Here the first pattern will match lists of length 5 or more, and the second pattern matches anything not matched by the first. You use it like this:

    *Main> fifth [1,2,3,20,30,40]
    Just 30
    *Main> fifth [1,2,3,20]
    Nothing
    

    You have now forced yourself to always pattern match the result of fifth against either Just a or Nothing. This means that when you code calls fifth someList, then it must take into account that someList might be too short. That way you can ensure at compile time that there wont be any runtime errors from this function.

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

Sidebar

Related Questions

My 'create' function in my 'Message' controller is something like this: def create @message
I want to create a haskell interpreter that I can use from C++ on
I can create function pointers in Fortran 90, with code like real, external ::
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I am new to Prolog and I'm trying to to create function that will
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS
A few examples to show, just incase: Inline Table Valued CREATE FUNCTION MyNS.GetUnshippedOrders() RETURNS
recently I'm writing some functions that I take from Haskell and translate into Java.
I have a C function that creates a null terminated string and returns a

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.