haskell笔记(2)

来源:互联网 发布:app store安装软件 编辑:程序博客网 时间:2024/05/17 23:15

–module Picture where

–或module Picture {
Picture,
above,
sideByside,
filpV,
filpH,
pic1,pic2,
printPic
} where

foo :: Int -> Int
foo x = x ^mod ^2

–[foo x | x <- [2,3,2,4,3,3,12]] <==> map foo
–[f x | x <- xs]

–高阶函数,highorder
my_map :: (a -> b) -> [a] -> [b]
–my_map f xs = [f x | x <- xs]

my_map f [] = []
my_map f (x : xs) = (f x) : map f xs

boo :: Float -> Bool
boo x = x^2 > 10

–map boo [1..10]
–map boo [True, False]

–p :: a -> Bool 类型的函数可以表达某种性质
–另一种计算模式:[x | x <- xs, p x]

isEven :: Int -> Bool
isEven x = x ^mod ^2 == 0

my_filter :: (n -> Bool) -> [a] -> [a]
–my_filter p xs = [x | x <- xs, p x]

–my_filter isEven [1..10]

my_filter p [] = []
my_filter p (x : xs)
| p x = x : my_filter p xs
| otherwise = my_filter p xs

–图形及图形上的函数

type Picture = [String]

picl = {
” # ”
” # # ”
” # # ”
” ####### ” }

–用”\n”换行

helpF :: Picture -> String
helpF [] = []
helpF (x : xs) = x ++ “\n” ++ heloF xs

printPic :: Picture -> IO ()
printPic pic = putStr (unlines pic)

sideByside :: Picture -> Picture -> Picture
sideByside pic1 pic2 = [x ++ y |(x,y) <- zip pic1 pic2]

above :: Picture -> Picture -> Picture
above pic1 pic2 = pic1 ++ pic2

flipV :: Picture -> Picture
filpV pic = [ reverse line | line <- pic]
filpV pic = map reverse pic

flipH :: Picture -> Picture
flipH :: pic = reverse pic

mys :: Picture -> Picture
mys pic = above pic1 pic1
where
pic1 = sideByside pic pic

0 0
原创粉丝点击