美文网首页
haskell示例

haskell示例

作者: bez | 来源:发表于2014-08-24 11:08 被阅读0次

1 排版规则

反引号能够将一个命令的标准输出插在一个命令行中任何位置

A `plus` asd

Unix 使用/ 左斜杠 windows使用\斜杠

和等号对齐非常重要;

以下是错误例子 未对齐;

常用关键字

导入模块

:module +Data.List

import Data.Char (digitToInt)

:module +Data.Char

:unset +m 取消输入多行;

:t add 可以得到函数类型

:set +m 可以输入多行;

Cmd 命令行下 runghc file.hs

\

列表和元组的区别 即

[1,2,3] 和 (1,”2”, 三)的区别..

[1,2..5] 省略号定义数组二个冒号;

1:[1,2] 前增扩展数组。。

IO

<- 将 io string 转换为string .

:cd d:\haskexam

Prelude> saf<-readFile "output.txt"

saf :: String

saf

"E23E4QWREWQRWQR\n"

ad<-getLine 可以让用户输入ad变量的值

读取文件内容并显示;

readFile "D:\\haskexam\\1.txt"

putStrLn "fas"

fas

LET

let ad="asd" 给变量赋值;

prelude

lines "asd\nadsad"

根据换行符将一个字符串分为多个字符串

length [1,2] 数组的长度

show 1234 将数字转换为字符显示

"1234"

print 213 显示

213

Compare 比较大小

Head 、 tail 、take、drop 、last列表的操作‘

Fst 、snd 元组的操作

定义函数;

定义一个新的数据结构

定义枚举。

data Roygbiv = Red

| Orange

| Yellow

| Green

| Blue

| Indigo

| Violet

deriving (Eq, Show)

报错

error "dasdfas"

*** Exception: dasdfas

Break :分割列表

碰到奇数分为二个列表;

all &any

all odd [1,3,5]

True

isPrefixOf [2] [2,4]

isSuffixOf "24" "2342t4"

zip [1,2,3] [4,5]

[(1,4),(2,5)]

:module -Data.List

Prelude Data.List>zipWith (+) [1,2,3] [4

[5]

words "das asd asd"

["das","asd","asd"]

unwords ["jumps", "over", "the", "lazy", "dog"]

"jumps over the lazy dog"

let ad = map toLower "D A"

map toUpper "dasd ads"

map negate [1,2,3]

[-1,-2,-3]

map (dropWhile isSpace) [" a","f"," e"]

["a","f","e"]

zip3 "foo" "bar" "quux"

[('f','b','q'),('o','a','u'),('o','r','u')]

map (+3) [24,36]

[27,39]

(`elem` ['a'..'z']) 'f'

True

(init.tail) "fas"

"a"

(init.tails) "fas"

["fas","as","s"]

map isUpper "fdfD"

[False,False,False,True]

isUpper (head "ad")

--多行注释 单行注释;

Prelude> {-

Prelude| asd

Prelude| asd

Prelude| -}

//

:show imports

标准引导库 Prelude.hs

error "fa"

*** Exception: fa

div 4 2

2

Mod

Negate

Abs

'\98'

'b'

1.2e3

1200.0

[2*n | n<-[2..14] , odd n]

[2*n | n<-[2..14] , n>5]

[m+n | (m,n)<-[(1,2),(3,4)] ]

复制;

replicate 3 'f'

"fff"

it :: [Char]

(0.02 secs, 0 bytes)

1:[1]

[1,1]

[1]++[2]

[1,2]

[1,2]!!0

1

concat [[1,2,33],[4,5]]

[1,2,33,4,5]

or [True, False]

True

let x=5 in x^2+2*x

35

let squartwo n m = sqn+sqm

Prelude| where

Prelude| sqn = n*n

Prelude| sqm = m*m

Prelude|

squartwo :: Num a => a -> a -> a

(0.00 secs, 0 bytes)

Prelude> squartwo 2 3

13

sum [2..6]

20

concat ["1","2"]

"12"

let doubleAll xs = [2*x|x<-xs]

Prelude|

doubleAll :: Num t => [t] -> [t]

(0.00 secs, 0 bytes)

Prelude> doubleAll[2,3]

[4,6]

map (+3) [1,2,3]

[4,5,6]

let add a b = a+b

map (add 2) [1,2]

[3,4]

map (replicate 2) [1,2,3]

[[1,1],[2,2],[3,3]]

map fst [(1,2),(3,5),(6,3),(2,6),(2,5)]

[1,3,6,2,2]

[x+3|x<-[1,2,3]]

[4,5,6]

Data.List;

intersperse '_' "faf"

"f_a_f"

intercalate "fs" ["12","34"]

"12fs34"

transpose [[1,2],[3,4],[6,7]]

[[1,3,6],[2,4,7]]

map sum $ transpose [[0,3,5,9],[10,0,0,9],[8,5,1,-1]]

[18,8,6,17]

map sum (transpose [[0,3,5,9],[10,0,0,9],[8,5,1,-1]] )

sum [1,2,5]

8

concat ["24", "3"]

"243"

concat $ map (replicate 4) [1..3]

[1,1,1,1,2,2,2,2,3,3,3,3]

concatMap (replicate 4) [1..3]

[1,1,1,1,2,2,2,2,3,3,3,3]

take 6 $ iterate (*3) 2

[2,6,18,54,162,486]

splitAt 3 "heyman"

("hey","man")

takeWhile (>3) [6,5,4,3,2,1,2,3,4,5,4,3,2,1]

[6,5,4]

'f'/='g'

True

'f'=='g'

False

takeWhile (<10000) $ map (^3) [1..]

[1,8,27,64,125,216,343,512,729,1000,1331,1728,2197,2744,3375,4096,4913,5832,6859,8000,9261]

span (/='i') "this"

("th","is")

break (=='i') "thisis"

("th","isis")

sort [14,3,54]

[3,14,54]

reverse $sort [14,3,54]

[54,14,3]

group ["1","1", "3"]

[["1","1"],["3"]]

zip [1,2,5] [3,4]

[(1,3),(2,4)]

let x=[1,2] in sum x

3

isInfixOf "cat" "catda"

True

"hey" `isPrefixOf` "hey there!"

True

"hey" `isPrefixOf` "f hey there!"

False

"there!" `isSuffixOf` "oh hey there!"

True

"there!" `isSuffixOf` "oh hey there! f"

False

elem 1 [1,2,3]

True

elem 11 [1,2,3]

False

partition (>3) [1,3,5,6,3,2,1,0,3,7]

([5,6,7],[1,3,3,2,1,0,3])

let bign n = if n>0 then True else False

let ds = partition (bign) [-5,1,3,5,6,3,2,1,0,3,7]

ds

([1,3,5,6,3,2,1,3,7],[-5,0])

partition (`elem` "AB") "BOBsidneyMORGANeddy"

("BBA","OsidneyMORGNeddy")

find (=='f') "gasdf"

Just 'f'

find (=='f') "gasdgd"

Nothing

'f' `elem` "gas"

False

elemIndex 11 [1,2,3]

Nothing

elemIndex 1 [1,2,3]

Just 0

elemIndices 1 [1,2,3,1]

[0,3]

findIndices (=='f') "gasdgfdf"

[5,7]

findIndex (=='f') "gasdgfdf"

Just 5

zip [1,2] [3,4,5]

[(1,3),(2,4)]

zip [1,2,3,4,5] [3,4,5]

[(1,3),(2,4),(3,5)]

zip4 [2,3,3] [2,2,2] [5,5,3] [2,2,2]

[(2,2,5,2),(3,2,5,2),(3,2,3,2)]

zipWith (+) [1,2,3] [4,5,2,2]

[5,7,5]

lines "first line\nsecond line\nthird line"

["first line","second line","third line"]

unlines ["first line", "second line", "third line"]

"first line\nsecond line\nthird line\n"

words "first li ne"

["first","li","ne"]

words "first line"

["first","line"]

unwords ["first","li","ne"]

"first li ne"

nub [1,3,2,4,3,2,1,2,3,4,3,2,1]

[1,3,2,4]

nub "Lots of words and stuff"

"Lots fwrdanu"

delete 'h' "hey there ghang!"

"ey there ghang!"

delete 'h' $ delete 'h' $ "hey there ghang!"

[1,2] \\ [2,3,4]

[1]

[2,3,4] \\[1,2]

[3,4]

union [1,2,3] [2,3,4]

[1,2,3,4]

[1,2,3] `intersect` [2,3,4]

[2,3]

insert 4 [15,2,2,3,5,6,7]

[4,15,2,2,3,5,6,7]

insert 4 [2,2,3,5,6,7]

[2,2,3,4,5,6,7]

insert 3 [1,2,4,3,2,1]

[1,2,3,4,3,2,1]

groupBy登场! 它取一个含两个参数的函数作为参数来判定相等性.

let values = [1,2,3,-3,-2,1,2,3,-3,-2]

let compaire2Num x y = if (x*y > 0 ) then True else False

groupBy ( compaire2Num ) values

[[1,2,3],[-3,-2],[1,2,3],[-3,-2]]

groupBy :: (a -> a -> Bool) -> [a] -> [[a]]

functor a,a 返回Bool;

入参为【a】,出差为【【a】】

group [1,1,1,2,2,3]

[[1,1,1],[2,2],[3]]

let sortGT a b = if a< b then GT else LT

sortBy sortGT values

[3,3,2,2,1,1,-2,-2,-3,-3]

let xval = [13,12,3]

sortBy sortGT xval

Data.Char

Prelude Data.List> :m Data.Char

Prelude Data.Char> :t group

Prelude Data.Char> import Data.List

(0.00 secs, 0 bytes)

Prelude Data.Char Data.List>

isControl '、'

False

isSpace ' '

True

isLower 'a'

True

isUpper 'A'

True

isAlpha 'a'

True

isAlpha '4'

False

isAlphaNum '1'

True

isPrint 'f'

True

isDigit '2'

True

isOctDigit '7'

True

isOctDigit '8'

False

isHexDigit 'g'

False

isHexDigit 'e'

True

isLetter '$'

False

isNumber '1'

True

isPunctuation ';'

True

isSymbol '$'

True

isAscii '&'

True

all isLetter "gdff"

True

all isLetter "gd2f"

False

generalCategory 'a'

LowercaseLetter

map generalCategory "f1'$"

[LowercaseLetter,DecimalNumber,OtherPunctuation,CurrencySymbol]

map toUpper "fa2"

"FA2"

map toTitle "gfa das"

"GFA DAS"

digitToInt 'f'

15

intToDigit 15

'f'

ord '%'

37

chr 97

'a'

Prelude Data.Char Data.List> let encode shift msg = map chr $ map (+ shift) ords

Prelude Data.Char Data.List| where ords = map ord msg

Data.Map

Prelude Data.Char Data.List> import

(0.02 secs, 551624 bytes)

Prelude Data.Char Data.List Data.Map>

Prelude.filter odd [2,4,1,3,6,8,5,7]

:t Data.Map.filter

Data.Map.filter :: (a -> Bool) -> Map k a -> Map k a

fromList [("betty","555-2938"),("bonnie","452-2928"),("lucille","205-2928")]

Loading package array-0.4.0.1 ... linking ... done.

Loading package deepseq-1.3.0.1 ... linking ... done.

Loading package containers-0.5.0.0 ... linking ... done.

fromList [("betty","555-2938"),("bonnie","452-2928"),("lucille","205-2928")]

fromList [(1,2),(3,4),(3,2),(5,5)]

fromList [(1,2),(3,2),(5,5)]

去掉重复key的

import qualified Data.Map as Map

Map.insert 3 100 Map.empty

fromList [(3,100)]

Map.insert 5 600 (Map.insert 4 200 ( Map.insert 3 100 Map.empty))

fromList [(3,100),(4,200),(5,600)]

let am = Map.empty

Map.insert 3 100 am

fromList [(3,100)]

Map.null Map.empty

True

let am = Map.fromList [(2,4),(3,3),(4,2),(5,4),(6,4)]

Map.size am

5

Map.singleton 3 9 $ Map.insert 5 9

:368:1:

Couldn't match expected type `(Map k1 a1 -> Map k1 a1) -> t0'

with actual type `Map k0 a0'

The first argument of ($) takes one argument,

but its type `Map k0 a0' has none

In the expression: singleton 3 9 $ Data.Map.insert 5 9

In an equation for `it': it = singleton 3 9 $ Data.Map.insert 5 9

Map.insert 5 9 $ Map.singleton 3 9

fromList [(3,9),(5,9)]

am

fromList [(2,4),(3,3),(4,2),(5,4),(6,4)]

Map.lookup 2 am

Just 4

Map.member 2 am

True

Map.map (*100) am

fromList [(2,400),(3,300),(4,200),(5,400),(6,400)]

Map.map (>0) am

fromList [(2,True),(3,True),(4,True),(5,True),(6,True)]

Map.filter (>3) am

fromList [(2,4),(5,4),(6,4)]

Map.fromListWith max [(2,3),(2,4),(2,1)]

fromList [(2,4)]

Map.fromListWith (+) [(2,3),(2,4),(2,1)]

fromList [(2,8)]

Map.insertWith (+) 3 100 $ Map.fromList [(3,4),(5,103),(6,339)]

fromList [(3,104),(5,103),(6,339)]

Map.insertWith (-) 5 100 $ Map.fromList [(3,4),(5,103),(6,339)]

fromList [(3,4),(5,-3),(6,339)]

Data.Set

import qualified Data.Set as Set

let text1 = "I just had an anime dream,Anime..,Reality..,Are they so different?"

let set1 = Set.fromlist text1

set1

fromList " ,.?AIRadefhijlmnorstuy"

Set.intersection set1 set2

Set.difference set1 set2

Set.union set1 set2

Set.null Set.empty

True

Set.null set1

False

Set.insert 4 $ Set.fromList [9,3,8,1]

fromList [1,3,4,8,9]

Set.delete ' ' $Set.delete 'a' aa

Set.fromList [2,3,4] `Set.isSubsetOf` Set.fromList [1,2,3,4,5]

True

Set.fromList [1,2,3,4,5] `Set.isProperSubsetOf` Set.fromList [1,2,3,4,5]

Set.map (+1) $ Set.fromList [1,2,3,4,5]

Set.filter odd $ Set.fromList [3,4,5,6,7,2,3,4]

:cd d:\haskexam

:load geometry.hs

module Geometry

(

sphereVolume

,sphereArea

,cubeVolume

,cubeArea

,cuboidArea

,cuboidVolume

) where

sphereVolume :: Float -> Float

sphereVolume radius = (4.0 / 3.0) * pi * (radius ^ 3)

data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show)

let surface (Circle _ _ r) = pi * r ^ 2

surface $ Circle 10 20 10

314.15927

Data.List.map (Circle 10 20) [4,5,6,6]

[Circle 10.0 20.0 4.0,Circle 10.0 20.0 5.0,Circle 10.0 20.0 6.0,Circle 10.0 20.0 6.0]

//取得愿的半径

let radiusCircle (Circle _ _ r) = r

radiusCircle $ Circle 1 1 2

2.0

data Circle = Circle{x::Float,y::Float, radius::Float} deriving (Show)

x $Circle 2 3 4

2.0

data Person = Person {age::Int, name::String} deriving (Show,Eq)

read "1"::Int

1

compare 2 3

LT

compare True False

GT

True > False

True

deriving (Eq, Ord, Show, Read, Bounded, Enum)

小写的不行

data ab = abc| efg

:67:11: Not a data constructor: `abc'

首字母大写ok.

data AB = ABS| EFG

data AB = ABS | EFG

它也是Enmu的实例,可以得到前一天和后一天,并且可以对此使用List的区间。

ghci> succ Monday

Tuesday

ghci> pred Saturday

Friday

x<-getLine

foldr (*)3 [1 ,2,3]

18

相关文章

  • haskell示例

    1 排版规则 反引号能够将一个命令的标准输出插在一个命令行中任何位置 A `plus` asd Unix 使用/ ...

  • ReactiveCocoa 2.0学习笔记

    函数式反应型编程(FRP) 起源于Haskell社区 响应事件流和数据流的变化 区别(示例) actions, d...

  • 函数式的宗教-00: 认识lisp(clojure)与haske

    总体大纲: lisp与haskell简单介绍 lisp与haskell应用领域 lisp与haskell技术分析 ...

  • monad以及monad的四条定理

    haskell的范畴是hask范畴(haskell的所有类型隶属于hask范畴),所以haskell的所有函子都是...

  • 01 数据类型

    swift中结构体在haskell中的描述 枚举类型在haskell中的描述 枚举携带类型在haskell中描述 ...

  • Haskell学习-函数式编程初探

    原文地址:Haskell学习-函数式编程初探  为什么要学习函数式编程?为什么要学习Haskell?  .net到...

  • Haskell

    [TOC] Haskell GHCI 通过Tab可以自动补全 通过 :browser 模块名称,浏览该模块下的函数...

  • haskell

    我在这里只是表达此刻内心想到的一些事情,或者记录自己关于最近学习生活工作的想法。 从我这一周对haskell的学习...

  • [Haskell] $

    函数“$”称为function application operator,定义如下: 与函数调用不同的是,函数调用...

  • [Haskell] .

    函数“.”称为function composition,定义如下: 我们看到,函数f接受函数g的返回值作为参数。函...

网友评论

      本文标题:haskell示例

      本文链接:https://www.haomeiwen.com/subject/whvjtttx.html