如何在Haskell中做一个三角形数组

时间:2017-01-02 17:39:54

标签: haskell

我想做点什么

array ((0,0), (25, 25)) [((i,j), 1) | i <- [0..25], j <- [i..25]]
您可以通过数组索引看到的

仅在i <= j时定义。但是,当我尝试在ghci中打印出来时,我收到一个错误,因为它会因为数组边界而尝试打印(1,0)之类的内容。

((1,0),*** Exception: (Array.!): undefined array element

我可以让数组为正方形,并在这些条目中添加类似0的东西,但我认为这不是最理想的。有没有办法可以将这个数组的边界设置为“三角形”?

1 个答案:

答案 0 :(得分:7)

一个简单的上三角索引可以定义为:

plugins {

  compile ":spring-security-core:1.2.7.3"

}

即使数组平方,也可以验证package applevel import grails.test.* import org.junit.* import org.springframework.web.context.request.RequestContextHolder import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*; import static org.springframework.security.test.web.servlet.result.SecurityMockMvcResultMatchers.*; import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; import org.springframework.security.test.context.support.*; @RunWith(SpringJUnit4ClassRunner.class)// unable to resolve @ContextConfiguration //unable to resolve @WebAppConfiguration //unable to resolve class ReportControllerTests extends GroovyTestCase{ def springSecurityService private WebApplicationContext context; private MockMvc mvc; //unable to resolve MockMvc @Before void setUp() { // Setup logic here mvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) 1 .build(); } @After void tearDown() { // Tear down logic here } @Test //@WithMockUser(username="testAdmin", roles={"ADMIN"})->unable to resolve void testIndex() { mvc.perform(get("/report").with(user("testAdmin").password("_testpass_").roles("ADMIN"))) } import Data.Ix (Ix, range, index, inRange) data UpperTriagIndex = Int :. Int deriving (Show, Ord, Eq) instance Ix UpperTriagIndex where range (a :. b, c :. d) = concatMap (\i -> (i :.) <$> [max i b..d]) [a..c] inRange (a :. b, c :. d) (i :. j) = a <= i && i <= c && b <= j && j <= d index pr@(a :. b, c :. d) ix@(i :. j) | inRange pr ix = f a - f i + j - i | otherwise = error "out of range!" where f x = let s = d + 1 - max x b in s * (s + 1) `div` 2 往返。例如:

range

index