𝖄𝕺🌎𝕿𝕽𝕺¥

𝖄𝕺🌎𝕿𝕽𝕺¥

𝕴 𝖉𝖔 𝖒𝖆𝖌𝖎𝖈
github

Haskellノート1 — 入門 算術演算子 未定義変数 リスト 文字列 型

モジュールの追加#

ghci> :module + Data.Ratio

算術演算子 + - * / ** ^ %#

基本的な演算#

ghci> 7.0 / 2.0
3.5
ghci> 7 / 2
3.5

ghci> 2 + 2
4
ghci> (+) 2 2
4

** と ^#

- ** は浮動小数点数のべき乗に使用できます
ghci> 2.2**3.3
13.489468760533386
- ^ は整数のべき乗にのみ使用できます
ghci> 2.2^3.3
<interactive> error:

%

分数の分子と分母は整数型である必要があります

ghci>11 % 29
11 % 29
ghci>it :: Ratio Integer
11 % 29

ghci>3.14 % 8
<interactive>:7:1: error:

特殊な演算 e.g. succ pred sqrt sin truncate round floor ceiling#

ghci>sqrt 16
4.0
ghci>succ 6
7
ghci>succ 7
8
ghci>pred 9
8
ghci>pred 8
7
ghci>sin (pi / 2)
1.0
ghci>truncate pi    --切り捨て
3
ghci>round 3.5    --四捨五入
4
ghci>round 3.4
3
ghci>floor 3.7
3
ghci>ceiling 3.3
4

演算子#

論理演算子 && ||#

ghci> True && False
False
ghci> False || True
True

0 は False を表し、0 以外は True を表します

値の比較 == <>>= <= /= not#

ghci> 1 == 1
True
ghci> 2 < 3
True
ghci> 4 >= 3.99
True
ghci> 1 /= 1    -- 不等号
False
ghci> not True  --notの使用例
False

優先順位 ()#

ghci> :info (+)     --+の優先順位を調べる
class (Eq a, Show a) => Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in GHC.Num
infixl 6 +      --優先順位は6

優先順位が高いほど優先されます

未定義の変数と変数の定義 e.g. pi , e#

ghci> pi
3.141592653589793

ghci> e
<interactive> Not in scope: `e'
eは存在しないため、自分で定義する必要があります
ghci> let e = exp 1
ghci> e
2.718281828459045

リスト#

リストの要素はすべて同じ型でなければなりません
リストの長さは任意です

[]
ghci> [3,1,3] ++ [3,7]
[3,1,3,3,7]
ghci> 1 : [2,3]
[1,2,3]
ghci> 1 : []
[1]

: はリストの先頭に要素を追加するために使用できます
空のリスト [] を受け入れることもできます

文字列と文字 putStrLn ""#

ghci> "This is a string."
"This is a string."
ghci> putStrLn "Here's a newline -->\n<-- See?"
Here's a newline -->
<-- See?

テキスト文字列は文字のリストです

ghci> let a = ['l', 'o', 't', 's', ' ', 'o', 'f', ' ', 'w', 'o', 'r', 'k']
ghci> a
"lots of work"
ghci> a == "lots of work"
True
ghci> "" == []
True
ghci> 'a':"bc"
"abc"
ghci> "foo" ++ "bar"
"foobar"

#

型名はすべて大文字で始まります
変数名はすべて小文字で始まります

:set +t は型を表示する機能です。これは ghci の補助機能です

:unset +t は型の表示をオフにします

:type は型をチェックします

Prelude Data.Ratio> :type 'a'
'a' :: Char

Prelude Data.Ratio> "foo"
"foo"

Prelude Data.Ratio> :type it
it :: [Char]
Prelude> :set +t

Prelude> 'c'    -- 式を入力
'c'             -- 出力値
it :: Char      -- 出力値の型

Prelude> "foo"
"foo"
it :: [Char]

整数の型は Integer です。 Integer 型の値の長さはシステムのメモリサイズに制限されます。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。