Function definition problems (No instance for … arising from)

I have defined the following function to find the penultimate element of a list of something (Int, string...)

myButLast :: [a] -> a myButLast [] = error "myButLast: empty list" myButLast [x, _] = x myButLast (_:xs) = myButLast xs

When I test it with hspec

it "returns an error for list of one element" $ do myButLast [42] `shouldThrow` anyException

I get the following error

No instance for (Num (IO a0)) arising from the literal 42' Possible fix: add an instance declaration for (Num (IO a0)) In the expression: 42 In the first argument ofmyButLast', namely [42]' In the first argument ofshouldThrow', namely `myButLast [42]'

What does it mean and how to fix it ? May be a constraint of class needed?

I want to handle String and list of anything in myButLast. All my other tests with multiples elements works.


shouldThrow has the type Exception e => IO a -> Selector e -> Expectation. This means that the first argument should be in the IO monad. In order to use pure functions, you can use the evaluate function:

evaluate (myButLast [42]) `shouldThrow` anyException

Incidentally, you might want to test for the specific error to make sure it doesn't get mistakenly changed at some point:

evaluate (myButLast [42]) `shouldThrow` errorCall "myButLast: empty list"


  • Haskell parsing prefix evaluation
  • SQL Server Nvarchar and Java prepared statement
  • Sort by a column in a union query in SqlAlchemy SQLite
  • SAXReader not re-ecape characters
  • ListItem.Attributes.Add not working
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Problem while Building a Setup Project for a windows Service?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • MVC3 Razor - ListBox pre-select not working
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Using variable in a value field in jMeter
  • Adding a button at the bottom of a table view
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • Is possible to count alias result on mysql
  • TFS: Get latest causes slow project reloading
  • Javascript Callbacks with Object constructor
  • Fill an image in a square container while keeping aspect ratio
  • JSON with duplicate key names losing information when parsed
  • Importing jscolor library in angular 2
  • How to make Safari send if-modified-since header?
  • To display the title for the current loaction in map in iphone
  • Jquery - Jquery Wysiwyg return html as a string
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Calling of Constructors in a Java
  • php design question - will a Helper help here?
  • PHP: When would you need the self:: keyword?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Reading document lines to the user (python)
  • UserPrincipal.Current returns apppool on IIS