码迷,mamicode.com
首页 > 其他好文 > 详细

Haskell示例

时间:2014-10-11 18:58:36      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   sp   div   log   ad   bs   

i :: Int
i = 5

--add, sub :: Int -> Int -> Int
add, sub :: (Num a) => a -> a -> a
add a b = a + b
sub a b = a - b

f :: (Num a) => a -> a
f x = 4 * x + 1

--出错
--area :: (Num x) => Floating -> x -> x
area :: Floating a => a -> a
area r = pi * (r ^ 2)

fun :: (Num a) => (a, a) -> a
fun (x,y) = 4 * x + 5 * y + 1

fun :: (Num a) => a -> a-> a
fun x y = 4 * x + 5 * y + 1

--Lambda表达式
--fun‘‘ = \x -> \y -> 4 * x + 5 * y + 1
fun‘‘ :: Num a => a -> a ->a
fun‘‘ = \x y -> 4 * x + 5*y + 1

plus :: Num a => a -> a -> a
plus = (+)

--参数绑定
--area_tri :: Float -> Float -> Float -> Float
area_tri ::  Floating a => a -> a -> a -> a
-- let ... in
area_tri a b c = let p = (a+b+c)/2 in  sqrt (p * (p-a) * (p-b) * (p-c))

area_tri :: Double -> Double -> Double -> Double
-- where
area_tri a b c = 
    sqrt (p * (p-a) * (p-b) * (p-c))
    where 
    p = ((a + b + c) / 2.0)

--条件表达式
isTwo :: Int -> Bool
isTwo x = if x == 2 then True else False

isTwo :: Int -> Bool
isTwo = (==2)

 

Haskell示例

标签:style   blog   color   ar   sp   div   log   ad   bs   

原文地址:http://www.cnblogs.com/wucg/p/4019284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!