Skip to content

Instantly share code, notes, and snippets.

@Uemmra3
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Uemmra3/9086890 to your computer and use it in GitHub Desktop.
Save Uemmra3/9086890 to your computer and use it in GitHub Desktop.
2014/2/19 #sugoih 読書会 2-2

オンライン版(英語): http://learnyouahaskell.com/types-and-typeclasses

#SS2 型を信じろ! ##SS2-1 明示的な型宣言

:t 'a'
:t True
:t "HELLO!"
:t (True, 'a')
:t 4 == 5

sample1

   removeNonUppercase :: [Char] -> [Char]  
   removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]   

sample2

   addThree :: Int -> Int -> Int -> Int  
   addThree x y z = x + y + z  

##SS2-2 一般的なHaskellの型

Int
Integer
Float
Double
Bool 
Char 

samples

   factorial :: Integer -> Integer  
   factorial n = product [1..n]  
   circumference :: Float -> Float  
   circumference r = 2 * pi * r
   
   circumference' :: Double -> Double  
   circumference' r = 2 * pi * r  

##SS2-3 型変数

:t head  
:t fst

##SS2-4 型クラス 初級講座 ###Eq Ord

:t (==) 
 5 == 5
 5 /= 5
 'a' == 'a'
 "Ho Ho" == "Ho Ho"
 3.432 == 3.432
 
 :t (>)  
 "Abrakadabra" < "Zebra"
 "Abrakadabra" `compare` "Zebra"
 5 >= 2
 5 `compare` 3

###Show Read

 show 3
 show 5.334
 show True

 read "True" || False
 read "8.2" + 3.8
 read "5" - 2
 read "[1,2,3,4]" ++ [3]
 
 # 型注釈明示的な型指定
 read "5" :: Int
 read "5" :: Float
 (read "5" :: Float) * 4
 read "[1,2,3,4]" :: [Int]
 read "(3, 'a')" :: (Int, Char)

###ENum Bounded

 ['a'..'e']
 [LT .. GT]
 [3 .. 5]
 succ 'B'
 
 minBound :: Int
 maxBound :: Char
 maxBound :: Bool
 minBound :: Bool
 
 maxBound :: (Bool, Int, Char)  

###Num Floating Integral

 :t 20
 20 :: Int
 20 :: Integer
 20 :: Float
 20 :: Double
 
 :t (*) 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment