haskell - 如何打印出二维数组haskell
问题描述
我想打印出二维数组,例如
data a = b [[a]]
instances Show a where
show (b array) = "Array:\n" ++ show array
array = [[(1, 2), (3, 4)],[(5, 6), (7, 8)]]
(我希望这个伪代码是可读的)
以这种方式:
[(1, 2), (3, 4)]\n
[(5, 6), (7, 8)]
我得到的唯一结果是:
[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
我需要使用实例显示。
解决方案
我在 StackOverflow 上找到了一个关于list type 的重载显示函数的有趣链接。
代码:
{-# LANGUAGE FlexibleInstances #-}
module Main where
array = [[(1, 2), (3, 4)],[(5, 6), (7, 8)]]
main = putStrLn $ Main.show array
instance {-# OVERLAPPING #-} Show [[a]] where
show (a:x) = Prelude.show a ++ "\n" ++ Main.show x
show [] = ""
编译输出(不幸的是没有警告):
[1 of 1] Compiling Main ( main.hs, main.o )
main.hs:6:30: warning: [-Wmissing-methods]
* No explicit implementation for
either `showsPrec' or `Prelude.show'
* In the instance declaration for `Show [[a]]'
|
6 | instance {-# OVERLAPPING #-} Show [[a]] where
| ^^^^^^^^^^
Linking main ...
输出:
[(1,2),(3,4)]
[(5,6),(7,8)]
推荐阅读
- google-sheets - 如何使用带有 OFFSET 的 ARRAYFORMULA?
- javascript - 海洋协议反应教程 npm 启动失败
- reactjs - 如何通过搜索实现 fetch API
- sql - 禁止破坏行字段的序列(无间隙)
- node.js - 从 MongoDB 集合中查找并删除最后输入的条目
- sql - 在没有子查询的情况下查找比特定行更年轻/更老的行
- r - ifelse() 在多列上与 all() 一起使用
- reactjs - 从父组件获取 Formik 状态
- java - 在 Tomcat 10 上部署 Spring Boot 应用程序时获取 HTTP 404 状态
- sql - 不同日期的 SQL 相同列