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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:56:41+00:00 2026-06-12T06:56:41+00:00

I’m working through Learn You A Haskell in order to come up to speed

  • 0

I’m working through Learn You A Haskell in order to come up to speed with the basics of Haskell. I’m very comfortable with both functional programming and pattern matching, but the latter more so with how Mathematica does it.

In the same spirit as the naïve implementation of head in Chapter 4.1, I proceeded with a naïve implementation of last as:

last1 :: [a] -> a
last1 (_:x:[]) = x

However, calling last1 [1,2,3,4] gave an error Exception: ... Non-exhaustive patterns in function last1. I understand that this error implies that the pattern specified does not cover all possible inputs and usually, a catch-all pattern is necessary (which I’ve not provided). However, I’m not exactly sure why I get this error for my input.

Question 1: My understanding (of my incorrect approach) is that the first element is captured by _ and the rest get assigned to x, which isn’t exactly what I had intended. However, shouldn’t this give a type error, because I specified [a] -> a, but x is now a list?

Note that this is not about how to write a working last function — I know I can write it as (among other possibilities)

last2 :: [a] -> a
last2 [x] = x
last2 (_:x) = last2 x

Question 2: Along the same theme of better understanding pattern matching in Haskell, how can I use pattern matching to pick out the last element or more generally, the nth element from a given list, say, [1..10]?

This answer suggests that you can bind the last element using pattern matching with the ViewPatterns extension, but it seems strange that there isn’t an analogous “simple” pattern like for head

In Mathematica, I would probably write it as:

Range[10] /. {Repeated[_, {5}], x_, ___} :> x
(* 6 *)

to pick out the 6th element and

Range[10] /. {___, x_} :> x
(* 10 *)

to pick out the last element of a non-empty list.

I apologize if this is covered later in the text, but I’m trying to relate each topic and concept as I come across them, to how it is handled in other languages that I know so that I can appreciate the differences and similarities.

  • 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-12T06:56:42+00:00Added an answer on June 12, 2026 at 6:56 am

    To make sense of the result of your first attempt, you need to see how the
    list data is defined. Lists enjoy a somewhat special syntax, but you would
    write it something like this.

    data List a = (:) a (List a)
                | []
    

    So, your list [1 .. 10] is actually structured as

    (1 : (2 : (3 : (4 : []))))
    

    In addition, due to the right associativity of the (:) operator, your pattern
    for last1 actually looks like

    last1 :: [a] -> a
    last1 (_:(x:[])) = x
    

    That is why ‘x’ has same type as an element of your list; it is the first
    argument to the (:) constructor.

    Pattern matching allows you to deconstruct data structures like lists, but you
    need to know what “shape” they have to do so. That is why you cannot directly
    specify a pattern that will extract the last element of a list, because there
    are an infinite number of lengths a list can have. That is why the working
    solution (last2) uses recursion to solve the problem. You know what pattern
    a list of length one has and where to find the final element; for everything
    else, you can just throw away the first element and extract the last element
    of the resulting, shorter, list.

    If you wanted, you could add more patterns, but it would not prove that
    helpful. You could write it as

    last2 :: [a] -> a
    last2 (x:[])     = x
    last2 (_:x:[])   = x
    last2 (_:_:x:[]) = x
            ...
    last2 (x:xs) = last2 xs
    

    But without an infinite number of cases, you could never complete the function
    for all lengths of input lists. Its even more dubious when you consider the fact that
    lists can actually be infinitely long; what pattern would you use to match that?

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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 want to count how many characters a certain string has in PHP, but

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.