`
tangtong
  • 浏览: 62516 次
  • 来自: ...
社区版块
存档分类
最新评论

Section 2.1

阅读更多

2.1

{-data Ration x = R Integer Integer
    
instance (Show a) => Show (Ration a) where
    showsPrec _ (R n d)  = shows (n `div` g).showString " % ". shows (d `div` g)
        where g = gcd' n d
    
gcd' a b = if b==0 then a else gcd' b (a `mod` b)

makeRat a = show a
-}
makeRat x y = (x',y')
    where g = gcd x y
          s = signum (x*y)
          x'= s* abs (x `div` g)
          y'= abs (y `div` g)

 2.2

makePoint x y = (x,y)

xPoint (x,_) = x

yPoint (_,y) = y

makeSegment (a,b) (c,d) = ((a,b),(c,d))

startSegment ((a,b),_) = (a,b)

endSegment (_,(c,d)) = (c,d)

midpointSegment ((a,b),(c,d)) = ((a+c)/2 ,(b+d)/2)

printPoint (x,y) = do
    putStrLn $ show (x,y)
 

 2.3

makeSquare (a,b) (c,d) = ((a,b),(c,d))

getCirm ((a,b),(c,d)) = 2*((abs (c-a)) + (abs (d-b)))

getArea ((a,b),(c,d)) = abs (c-a) * abs (d-b)

  2.4~2.5 略

  2.6 不太懂,Church 数

one = \f -> \x -> f x

two = \f -> \x -> f (f x)

plus a b = a.b

  2-7~2-16 基本都是算法问题,不想做 TODO

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics