関数型プログラミングの基礎 リスト repeat関数を独自に実装、cycle関数、サブシーケンス、take関数、drop関数、要素の前半部分、length関数、elem関数
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT1(関数型プログラミングの基礎)、LESSON 6(リスト)、6.5(練習問題)、Q6-1、Q6-2、Q6-3の解答を求めてみる。
コード
-- Q6-1
myRepeat :: a -> [a]
myRepeat x = cycle [x]
-- Q6-2
subseq :: Int -> Int -> [a] -> [a]
subseq start end aList = take (end - start) (drop start aList)
-- Q6-3
inFirstHalf :: Eq a => a -> [a] -> Bool
inFirstHalf x aList = x `elem` take (div (length aList) 2) aList
-- Q6-2
subseq2 :: Int -> Int -> [a] -> [a]
subseq2 start end aList = drop start (take end aList)
入出力結果(Terminal, Zsh)
% ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
macro 'doc' overwrites builtin command. Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /.../.ghc/ghci.conf
Prelude
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.03 secs,)
*Main
λ> repeat 1
<interactive>:2:1: error:
Ambiguous occurrence ‘repeat’
It could refer to
either ‘Prelude.repeat’,
imported from ‘Prelude’ at sample1.hs:1:1
(and originally defined in ‘GHC.List’)
or ‘Main.repeat’, defined at sample1.hs:2:1
(0.00 secs,)
*Main
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> myRepeat 1
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 … ,1,^C,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,Interrupted.
*Main
λ> myRepeat 'x'
"xxxxxxxxxxxxxxxxxxx…^CxxxxxxxxxxxxxxxxxxxxxxxxxxxxxInterrupted.
*Main
λ> myRepeat "x"
["x","x","x","x","x"…"x","x^C,"x","x","x","x","x","x","x","x","x","x","x","x","x","Interrupted.
*Main
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> subseq 2 5 [1 .. 10]
[3,4,5,6,7]
it :: (Num a, Enum a) => [a]
(0.01 secs, 68,424 bytes)
*Main
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> subseq 2 5 [1 .. 10]
[3,4,5]
it :: (Num a, Enum a) => [a]
(0.00 secs, 65,176 bytes)
*Main
λ> subseq 2 7 "a puppy"
"puppy"
it :: [Char]
(0.00 secs, 65,296 bytes)
*Main
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.07 secs,)
*Main
λ> inFirstHalf 1 [1..10]
True
it :: Bool
(0.00 secs, 63,032 bytes)
*Main
λ> inFirstHalf 5 [1..10]
True
it :: Bool
(0.00 secs, 62,920 bytes)
*Main
λ> inFirstHalf 6 [1..10]
False
it :: Bool
(0.00 secs, 63,992 bytes)
*Main
λ> inFirstHalf 6 [1..9]
False
it :: Bool
(0.00 secs, 63,488 bytes)
*Main
λ> inFirstHalf 5 [1..9]
False
it :: Bool
(0.00 secs, 63,488 bytes)
*Main
λ> inFirstHalf 4 [1..9]
True
it :: Bool
(0.00 secs, 62,792 bytes)
*Main
λ> :quit
Leaving GHCi.
% ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
macro 'doc' overwrites builtin command. Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /.../.ghc/ghci.conf
Prelude
λ> :load sample1
[1 of 1] Compiling Main ( sample1.hs, interpreted )
Ok, one module loaded.
(0.03 secs,)
*Main
λ> subseq2 2 5 [1..10]
[3,4,5]
it :: (Num a, Enum a) => [a]
(0.01 secs, 67,552 bytes)
*Main
λ> subseq2 2 7 "a puppy"
"puppy"
it :: [Char]
(0.00 secs, 65,344 bytes)
*Main
λ> :quit
Leaving GHCi.
%