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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:53:57+00:00 2026-05-26T03:53:57+00:00

So here is a nested list [[1, 2], [3, 4]] I want to wrap

  • 0

So here is a nested list [[1, 2], [3, 4]]

I want to wrap it in a type called Matrix, and make it an instance of the classes Eq, Num, and Show

I have already created (add, sub, mul) operations for nested lists (matrices). How do I overload (+ – *) operators so that + maps to add, – maps to sub, and * maps to mul? So I can do this

> ma = Matrix [[1, 2], [3, 4]]
> mb = Matrix [[5, 6], [7, 8]]
> ma + mb
> ma - mb
> ma * mb

Thanks

EDIT

this is my attempt so far

> add = zipWith (zipWith (+))
> sub = zipWith (zipWith (-))
> data Matrix a = Matrix [[a]] deriving (Eq, Show)
> instance Num (Matrix a)
>   where
>   (+) x y = Matrix $ add x y
>   (-) x y = Matrix $ sub x y

This is what I get from ghci

 Couldn't match expected type `[[c0]]' with actual type `Matrix a'
    In the first argument of `sub', namely `x'
    In the second argument of `($)', namely `sub x y'
    In the expression: Matrix $ sub x y

EDIT #2
The last thing I need to figure out right now is

how to I print

1 2
3 4

Instead of Matrix [[1,2],[3,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-05-26T03:53:58+00:00Added an answer on May 26, 2026 at 3:53 am

    Are you having a problem with defining a Num instance for your type? Try this code:

    data Matrix a = Matrix [[a]]
                  deriving (Eq)
    
    plus_mat :: Num a => [[a]] -> [[a]] -> [[a]]
    plus_mat = zipWith (zipWith (+))
    
    instance Num a => Num (Matrix a)
      where
        (Matrix a) + (Matrix b) = Matrix $ plus_mat a b
        (-)                     = undefined
        (*)                     = undefined
        negate                  = undefined
        abs                     = undefined
        signum                  = undefined
        fromInteger             = undefined
    

    Testing:

    *Main> Matrix [[1,2],[3,4]] + Matrix [[5,6],[7,8]]
    Matrix [[6,8],[10,12]]
    

    Definitions of the remaining class methods are left as exercise.

    And here’s a Show instance for Matrix:

    import Data.List
    
    instance Show a => Show (Matrix a)
      where
        show (Matrix a) = intercalate "\n" $ map (intercalate " " . map show) a
    

    Testing:

    *Main Data.List> Matrix [[1,2,3], [4,5,6]]
    1 2 3
    4 5 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm talking about here are nested classes. Essentially, I have two classes that
I have a nested list, and therefore I want the label to be nested
I can't figure out what is going on here. I have some nested lists
Little help need with jquery sortable. I have a nested list like so: <div
great community :) I have category list which have nested child category list, which
HI, I have a list of coursesTaken with dates (returned by a nested class).
Let's say I have a list of values to which I want to apply
I have a nested list in say lst (all the elements are of class
Using VS 2008, I have a Repeater control with nested elements and want to
I have a nested list, something like this: List<Hotel> Hotels; public class Hotel {

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.