开发者

nat2::[(Integer,Integer)] definition

开发者 https://www.devze.com 2023-03-06 02:01 出处:网络
Define a list: nat2::[(Integer, Integer)] that contains all pairs of nonnegative integers ordered by the relation known

Define a list:

nat2::[(Integer, Integer)]

that contains all pairs of nonnegative integers ordered by the relation known from the proof of Cantor theorem:

(x1,y1) < (x2,y2) <=> x1+y1 < x2+y2 v (x1+y1=x2+y2 ^ x1 < x2)

[^- means alternative开发者_C百科]

such that:

nat2 = [(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(0,3),(1,2),(2,1),(3,0),...]

Hint:

definition should fit in one line and be shorter than 45 characters. Notice that the sum of coordinates of points laying on on the same diagonal is constant.

I made some definition, but am not sure if it is correct, could you check/repair/give tips:

nat2::[(Integer,Integer)]
nat2=[(a,b-a)|b<-[0...],a<-[0...b]]

EDIT: CHANGED TO:

nat2 :: [(Integer,Integer)] 
nat2 = [(a,b-a) | b <- [0..], a <- [0..b]]

with result:

Prelude> :load "nat2.hs"
[1 of 1] Compiling Main             ( nat2.hs, interpreted )
Ok, modules loaded: Main.
*Main> take 10 nat2
[(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(0,3),(1,2),(2,1),(3,0)]


You have a syntax error (did you try running it and checking the output?)

Prelude> [(a,b-a)|b<-[0...],a<-[0...b]]

<interactive>:1:14:
    A section must be enclosed in parentheses thus: (0 ...)

Because you only need two .. in the list enums:

Prelude> take 10 [(a,b-a)|b<-[0..],a<-[0..b]]
[(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(0,3),(1,2),(2,1),(3,0)]    

Looks reasonable, but you are the best to judge.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号