計算機科学のブログ

型の紹介 型の基礎 関数の型 複数の引数を持つ関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT2(型の紹介)、LESSON 11(型の基礎)、11.2(関数の型)、複数の引数を持つ関数、クイックチェック 11-2の解答を求めてみる。

コード

lesson/app/Main.hs

module Main where

import Lib (p)

main :: IO ()
main = p

lesson/src/Lib.hs

module Lib
  ( p,
  )
where

n :: Int
n = 123

s :: String
s = "Happy St"

t :: String
t = "Haskell Town"

makeAddress :: Int -> String -> String -> (Int, String, String)
makeAddress number street town = (number, street, town)

makeAddressNumber :: String -> String -> (Int, String, String)
makeAddressNumber = makeAddress n

makeAddressNumberStreet :: String -> (Int, String, String)
makeAddressNumberStreet = makeAddress n s

address :: (Int, String, String)
address = makeAddress n s t

address1 :: (Int, String, String)
address1 = makeAddressNumber s t

address2 :: (Int, String, String)
address2 = makeAddressNumberStreet t

p :: IO ()
p = do
  mapM_ print [address, address1, address2]

入出力結果(Terminal, Zsh)

% stack build
lesson> build (lib + exe)
Preprocessing library for lesson-0.1.0.0..
Building library for lesson-0.1.0.0..
[2 of 2] Compiling Lib
Preprocessing executable 'lesson-exe' for lesson-0.1.0.0..
Building executable 'lesson-exe' for lesson-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/lesson-exe/lesson-exe ...
lesson> copy/register
Installing library in /Users/…/lesson/.stack-work/install/x86_64-osx/7091f650d3dd5d7cbe41c419933d22622a88b13592d474a4616bbcad00d1cf98/9.0.2/lib/x86_64-osx-ghc-9.0.2/lesson-0.1.0.0-CIOIkpTgcfQI9akDJnwnSd
Installing executable lesson-exe in /Users/…/lesson/.stack-work/install/x86_64-osx/7091f650d3dd5d7cbe41c419933d22622a88b13592d474a4616bbcad00d1cf98/9.0.2/bin
Registering library for lesson-0.1.0.0..
% stack exec lesson-exe
(123,"Happy St","Haskell Town")
(123,"Happy St","Haskell Town")
(123,"Happy St","Haskell Town")
%