haskell learning1(2)

1.
[x^2| x<- [1..10],odd x]==[1,9,25,49,81]
[z| x <- [1..10],let z = x^2,z>50]  # kann Abkürzung für oben

2.[(wert,name)| wert <- [1..3],name <- ['a'..'b']]
result:
[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]
it :: (Enum t, Num t) => [(t, Char)]

3. type MyWeirdType = (Double,[(Bool,Integer)])

4.data MyTuple = MyTupelConstructor Int Int
##wie Tupel ohne Klammern und kommas;stattdessen mit vorangestellem Konstruktor

5.    data Frucht = Apfel (Int,Double)
                             | Birne Int Double
                             | Banane Int Int Double

6.
data Maybe a = Noting | Just a

data Frucht = Apfel {preis::Int,anzahl::Int}
   | Birne {preis::Int,anzahl::Int}
   |Banane {preis::Int,anzhal::Int
     ,krümmung:Double}
getKrümmung :: Frucht -> Maybe Double
getKrümmung Banane {krümmung=k} = Just k
getKrümmung _                   = Nothing


1. Maybe
example :
data Maybe a = Nothing | Just a
isJust      :: Maybe a -> Bool
isJust   Nothing  = False
isJust   _        = True

2.Either
example:
Module Data.Either where
 data Either a b = Left a | Right b
 isRight :: Either a b -> Bool
 isRight  (Left _) = False
 isRight  (Right _) = True
 lefts :: [Either a b]-> [a]
 lefts x = [a | Left a <- x]
 partitionEithers :: [Either a b]-> ([a],[b])
 partitionEithers [] = ([],[])
 partitionEithers (h:t)
  | Left l <- h = (l:ls,rs)
  | Right r <- h = (ls,r:rs)
  where (ls,rs) = partitionEithers t
#just example

Funktionsdefinition
double1 x = x+x
foo x y z = x +y *double z
add = \x y -> x +y
twice f x = f x x
double2 y = twice (+) y
double3   = twice (+)

2.Höhepunkt
Int -> Int -> Int gleich as Int->(Int -> Int)

3.
drop :: Int -> [a]->[a]
drop n xs  | n <= 0 = xs
drop _ []    = []
drop n (_:xs) = drop (n-1) xs

drop 3 [1,2,3,4,5]
4.
Einrückung ersetzt Klammerung
betragSumme :: Int -> Int -> Int
betragSumme x y =
 let x' = abs x
  y' = case y of 0 -> 0
      x| x>0  ->x
       | otherwise -> -x
  in x' + y'

5.
map :: (a->b)->[a]->[b]    # (a->b) funktion
map f [] = []
map f (x:xs) =
 f x : map f xs
 










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值