計算機科学のブログ

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

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

palindrome-checker.cabalファイルのlibraryのexposed-modulesのLibをPalindromeに書き換えればいい。

palindrome-checker.cabal

cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- 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:         Author name here
maintainer:     example@example.com
copyright:      2022 Author name here
license:        BSD3
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:
      Palindrome
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable palindrome-checker-exe
  main-is: Main.hs
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
  default-language: Haskell2010

test-suite palindrome-checker-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
  default-language: Haskell2010

コード

app/Main.hs

module Main where

import Palindrome

main :: IO ()
main = print isPalindrome

コード

src/Palindrome.hs

{-# LANGUAGE OverloadedStrings #-}

module Palindrome
  ( isPalindrome,
  )
where

isPalindrome :: [Char]
isPalindrome = "isPalindrome"

入出力結果(Terminal, Zsh)

% stack build                      
palindrome-checker-0.1.0.0: unregistering (local file changes: app/Main.hs src/Palindrome.hs)
palindrome-checker> build (lib + exe)
Preprocessing library for palindrome-checker-0.1.0.0..
Building library for palindrome-checker-0.1.0.0..
[2 of 2] Compiling Palindrome
Preprocessing executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
Building executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-checker-exe/palindrome-checker-exe ...
palindrome-checker> copy/register
Installing library in /Users/…/palindrome-checker/.stack-work/install/x86_64-osx/e39fa63852b3f10f810d09adf64598107c29e3897fb5f28ebd525048b485f8ab/8.10.7/lib/x86_64-osx-ghc-8.10.7/palindrome-checker-0.1.0.0-HO7CoLYbgortDs5KN64VY
Installing executable palindrome-checker-exe in /Users/…/palindrome-checker/.stack-work/install/x86_64-osx/e39fa63852b3f10f810d09adf64598107c29e3897fb5f28ebd525048b485f8ab/8.10.7/bin
Registering library for palindrome-checker-0.1.0.0..
% stack exec palindrome-checker-exe
"isPalindrome"
%