- とにかく写経してGS.hsなるファイルを作る
- Stackがデフォルトで作ったLib.hsをまねて作る
- 作成した「わり切れるかの判定」関数も外から参照可能にする
- Lib.hsと同じくsrcディレクトリに置く
- app以下にあるMain.hsにGS.hsをインポートさせ、GS.hsのsomeFuncGS関数を実行させる
- .cabalファイルにGS.hsのありかを教える必要もある
module GS
( someFuncGS
, divides
, ld
, ldf
) where
someFuncGS :: IO ()
someFuncGS = putStrLn "GS"
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0
ld' :: Integer -> Integer
ld' n = ldf' 2 n
ldf' :: Integer -> Integer -> Integer
ldf' k n | divides k n = k
| otherwise = ldf' (k+1) n
ld :: Integer -> Integer
ld n = ldf 2 n
ldf :: Integer -> Integer -> Integer
ldf k n | divides k n = k
| k^2 > n = n
| otherwise = ldf (k+1) n
prime0 :: Integer -> Bool
prime0 n | n < 1 = error "non positive"
| n == 1 = False
| otherwise = ld n == n
minIntList :: [Int] -> Int
minIntList [] = error "empty"
minIntList [x] = x
minIntList (x:xs) = min x (minIntList xs)
removeFst :: Int -> [Int] -> [Int]
removeFst m [] = []
removeFst m (x:xs) | x == m = xs
| otherwise = x:(removeFst m xs)
srtInts :: [Int] -> [Int]
srtInts [] = []
srtInts xs = m : (srtInts (removeFst m xs)) where m = minIntList xs
srtInts' :: [Int] -> [Int]
srtInts' [] = []
srtInts' xs = let
m = minIntList xs
in m : (srtInts' (removeFst m xs))
average :: [Int] -> Rational
average [] = error "no element"
average xs = toRational (sum' xs) / toRational (length' xs)
length' :: [Int] -> Int
length' [] = 0
length' (x:xs) = 1 + length' xs
sum' :: [Int] -> Int
sum'[] = 0
sum' (x:xs) = x + sum' xs
count :: Char -> String -> Int
count _ [] = 0
count x (y:ys) | x== y = (1 + (count x ys))
| otherwise = count x ys
take 20 (suuretsus (+2) 2)
-}
suuretsu :: (Int -> Int) -> Int -> Int -> Int
suuretsu f x 0 = x
suuretsu f x y = f (suuretsu f x (y-1))
suuretsus :: (Int -> Int) -> Int -> [Int]
suuretsus f x = map (suuretsu f x) [0..]
module Main where
import Lib
import GS
main :: IO ()
main = someFuncGS
- mathBookStack.cabalファイルの変更
library
hs-source-dirs: src
exposed-modules: Lib
, GS
build-depends: base >= 4.7 && < 5
default-language: Haskell2010
stack install
mathBookStack-exe
GS
- ghciでGS.hsを使ってみる
- ..../mathBookStack で(他で、でもよいが)
stack ghci
:load ./src/GS.hs
*GS> divides 3 6
True
*GS> divides 3 6 --- True
True
*GS> divides 3 -6 --- error
<interactive>:10:1: error:
• Non type-variable argument in the constraint: Num (a -> Bool)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num (a -> Bool), Integral a) => a -> Bool
*GS> divides 3 (-6) --- True
True
*GS> divides 3 $ -6 --- True
True
*GS> 3 `divides` 6 --- Truedivides 3 6 --- True
True
||
-その他の知識
--関数オブジェクト(小文字始まり)の型確認
>||
*GS> :type divides
divides :: Integral a => a -> a -> Bool
-
- aをIntegralのそれとしたときに、dividesは引数としてaを2つ取り、Boolを返す
- Integralは大文字始まりなので、型とかクラスとかそういうもののはずだが、実際、何かと言えば
*GS> :info Integral
class (Real a, Enum a) => Integral a where
quot :: a -> a -> a
rem :: a -> a -> a
div :: a -> a -> a
mod :: a -> a -> a
quotRem :: a -> a -> (a, a)
divMod :: a -> a -> (a, a)
toInteger :: a -> Integer
{-# MINIMAL quotRem, toInteger #-}
-- Defined in ‘GHC.Real’
instance Integral Word -- Defined in ‘GHC.Real’
instance Integral Integer -- Defined in ‘GHC.Real’
instance Integral Int -- Defined in ‘GHC.Real’
-
- classであることがわかり、Realであって Enum(列挙可能)であるaがあったとして、quot以下、toIntegerまでの7つの関数を持つ。ただし、Realがすでに定義を持つので、quotRem,toIntegerのみの定義が必要。また、IntegralクラスにはWord(符号なし整数), Integer, Intなるタイプがあることもわかる