首页 > 解决方案 > Haskell 练习解构记录语法

问题描述

开始练习以复习所学的 Haskell 技能。

module Clock (addDelta, fromHourMin, clockDecons) where

data Clock = Clock { hours :: Int 
                   , mins  :: Int 
                   } deriving Show 

fromHourMin :: Int -> Int -> Clock
fromHourMin hour min = Clock {hours = hour, mins = min}

-- toString :: Clock -> String
clockDecons clock = (hs,ms) 
  where hs = hours 
        ms = mins

addDelta :: Int -> Int -> Clock -> Clock
addDelta hour min clock = undefined

一整天后可能有点阴云密布,但为什么我会得到:

<interactive>:15:1: error:
    • No instance for (Show (Clock -> Int))
        arising from a use of ‘print’
        (maybe you haven't applied a function to enough arguments?)
    • In a stmt of an interactive GHCi command: print it

我什至还没有开始创建时钟的字符串实例。

标签: haskellrecord

解决方案


你可能是说

clockDecons :: Clock -> (Int, Int)
clockDecons clock = (hours clock, mins clock) 

推荐阅读