計算機科学のブログ

型の紹介 カスタム型の作成 型シノニムを使用する typeキーワード

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT2(型の紹介)、LESSON 12(カスタム型の作成)、12.1(型シノニムを使用する)のクイックチェック12-1の解答を求めてみる。

コード

type PatientName = (String, String)

patientInfo :: PatientName -> Int -> Int -> String
patientInfo (fname, lname) age height =
  let name = lname ++ ", " ++ fname
      ageHeight = "(" ++ show age ++ "yrs. " ++ show height ++ "in.)"
   in name ++ " " ++ ageHeight

入出力結果(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 sample01
[1 of 1] Compiling Main             ( sample01.hs, interpreted )
Ok, one module loaded.
(0.06 secs,)
*Main
λ> patientInfo "John" "Doe" 43 74

<interactive>:2:1: error:
    • Couldn't match expected type ‘t0 -> t’ with actual type ‘[Char]’
    • The function ‘patientInfo’ is applied to four arguments,
      but its type ‘PatientName -> Int -> Int -> [Char]’ has only three
      In the expression: patientInfo "John" "Doe" 43 74
      In an equation for ‘it’: it = patientInfo "John" "Doe" 43 74
    • Relevant bindings include it :: t (bound at <interactive>:2:1)

<interactive>:2:13: error:
    • Couldn't match type ‘[Char]’ with ‘(String, String)’
      Expected type: PatientName
        Actual type: [Char]
    • In the first argument of ‘patientInfo’, namely ‘"John"’
      In the expression: patientInfo "John" "Doe" 43 74
      In an equation for ‘it’: it = patientInfo "John" "Doe" 43 74

<interactive>:2:20: error:
    • Couldn't match expected type ‘Int’ with actual type ‘[Char]’
    • In the second argument of ‘patientInfo’, namely ‘"Doe"’
      In the expression: patientInfo "John" "Doe" 43 74
      In an equation for ‘it’: it = patientInfo "John" "Doe" 43 74
(0.07 secs,)
*Main
λ> patientInfo ("John", "Doe") 43 74
"Doe, John (43yrs. 74in.)"
it :: String
(0.03 secs, 85,000 bytes)
*Main
λ> patientInfo  ("Jane", "Smith") 25 62
"Smith, Jane (25yrs. 62in.)"
it :: String
(0.00 secs, 84,576 bytes)
*Main
λ> :quit
Leaving GHCi.
%