計算機科学のブログ

Haskell - コードの整理とプロジェクトのビルド - stackを使ってプロジェクトをビルトする - コードを記述する

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT6(コードの整理とプロジェクトのビルド)、LESSON 35(stackを使ってプロジェクトをビルトする)、35.3(コードを記述する)、Q35-3の解答を求めてみる。

軽量マークアップ言語

palindrome-checker/package.yaml

name:                palindrome-checker
version:             0.1.0.0
github:              "githubuser/palindrome-checker"
license:             BSD-3-Clause
author:              "kamimura"
maintainer:          "example@example.com"
copyright:           "2025 kamimura"

extra-source-files:
- README.md
- CHANGELOG.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/palindrome-checker#readme>

dependencies:
- base >= 4.7 && < 5
- text

ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src



executables:
  palindrome-checker-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker


tests:
  palindrome-checker-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker

軽量マークアップ言語

palindrome-checker/palindrome-checker.cabal

cabal-version: 2.2

-- This file has been generated from package.yaml by hpack version 0.37.0.
--
-- see: https://github.com/sol/hpack

name:           palindrome-checker
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/githubuser/palindrome-checker#readme>
homepage:       https://github.com/githubuser/palindrome-checker#readme
bug-reports:    https://github.com/githubuser/palindrome-checker/issues
author:         kamimura
maintainer:     example@example.com
copyright:      2025 kamimura
license:        BSD-3-Clause
license-file:   LICENSE
build-type:     Simple
extra-source-files:
    README.md
    CHANGELOG.md

source-repository head
  type: git
  location: https://github.com/githubuser/palindrome-checker

library
  exposed-modules:
      Lib
      Palindrome
  other-modules:
      Paths_palindrome_checker
  autogen-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      src
  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
  build-depends:
      base >=4.7 && <5
    , text
  default-language: Haskell2010

executable palindrome-checker-exe
  main-is: Main.hs
  other-modules:
      Paths_palindrome_checker
  autogen-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      app
  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
    , text
  default-language: Haskell2010

test-suite palindrome-checker-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_palindrome_checker
  autogen-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      test
  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
    , text
  default-language: Haskell2010

コード

palindrome-checker/app/Main.hs

{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Palindrome(isPalindrome)
import qualified Data.Text.IO as TIO
main :: IO ()
main = do
    TIO.putStrLn "Enter a word and I'll let you know if it's a palindrome!"
    text <- TIO.getLine
    let response = if isPalindrome text
                then "it is!"
                else "it's not!"
    TIO.putStrLn response

コード

palindrome-checker/src/Palindrome.hs

{-# LANGUAGE OverloadedStrings #-}
module Palindrome(isPalindrome) where

import qualified Data.Text as T
import Data.Char (toLower,isSpace,isPunctuation)

stripWhiteSpace :: T.Text -> T.Text
stripWhiteSpace = T.filter (not . isSpace)

stripPunctuation :: T.Text -> T.Text
stripPunctuation = T.filter (not . isPunctuation)

preProcess :: T.Text -> T.Text
preProcess = stripWhiteSpace . stripPunctuation . T.toLower

isPalindrome :: T.Text -> Bool
isPalindrome text = cleanText == T.reverse cleanText
    where cleanText = preProcess text

入出力結果(Terminal, Zsh)

% stack run
palindrome-checker-0.1.0.0: unregistering (local file changes: app/Main.hs)
palindrome-checker> build (lib + exe) with ghc-9.8.4
Preprocessing library for palindrome-checker-0.1.0.0..
Building library for palindrome-checker-0.1.0.0..
Preprocessing executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
Building executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.8.4/build/palindrome-checker-exe/palindrome-checker-exe [Objects changed]
ld: warning: -U option is redundant when using -undefined dynamic_lookup
palindrome-checker> copy/register
Installing library in /.../palindrome-checker/.stack-work/install/aarch64-osx/e7436b3217cf53d6b9beeebd2bdc5dd8dcab6f8595b8bde259631442ce745f25/9.8.4/lib/aarch64-osx-ghc-9.8.4/palindrome-checker-0.1.0.0-AsDJJAXtgGvCwcL0FmK5ox
Installing executable palindrome-checker-exe in /.../palindrome-checker/.stack-work/install/aarch64-osx/e7436b3217cf53d6b9beeebd2bdc5dd8dcab6f8595b8bde259631442ce745f25/9.8.4/bin
Registering library for palindrome-checker-0.1.0.0..
Enter a word and I'll let you know if it's a palindrome!
Racer
it's not!
% stack run
Enter a word and I'll let you know if it's a palindrome!
Race car
it is!
%