相关疑难解决方法(0)

读取和表示指定要使用的数据类型的输入

我想读一些本身指定要使用的数据类型的数据.

例如,假设可能存在以下用户输入:

integer pair 1 2
integer triple 1 2 3
real pair 1 2
real triple 1 2 3
Run Code Online (Sandbox Code Playgroud)

并且有一种数据类型来表示它:

data (MValue a) => T a = TP (Pair a) | TT (Triple a)
  deriving (Show, Eq)

data Pair a = Pair a a deriving (Show, Eq)
data Triple a = Triple a a a deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)

允许的值类型必须属于MValue类:

class (Num a, Read a) => MValue a where
  typename :: a -> String
  readval  :: [String] -> …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell

5
推荐指数
1
解决办法
321
查看次数

在Haskell中,如何强制表达式为函数的返回类型?

我想概括这个函数,(Integral a, Integral b, Bounded b) => a -> [b]但我不知道如何强制maxBound得到结果的类型.这可能吗?

go :: Integral a => a -> Maybe (Word8, a)
go 0 = Nothing
go x 
    | x < 0 = error "Negative numbers are not acceptable"
    | otherwise = Just $ (remainder, quotient)
    where
        quotient = fromInteger $ (toInteger x) `div` (toInteger (maxBound :: Word8))
        remainder = fromInteger $ (toInteger x) `mod` (toInteger (maxBound :: Word8))

int2WordList :: Integral a => a -> [Word8]
int2WordList …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell types integer

1
推荐指数
1
解决办法
203
查看次数

在 Haskell 中,如何使用继承的参数显式地为这个特定的子函数签名?

说你有这个功能

import Control.Monad
import Control.Monad.ST
import Data.STRef

f :: (Num a, Ord a) => STRef s a -> ST s ()
f i = loop
        where
            loop = do
                _i <- readSTRef i
                writeSTRef i (_i - 1)
                when (_i > 1) loop
Run Code Online (Sandbox Code Playgroud)

inloop的主体,i被隐式定义,因为它是来自 的参数f。但是我在给loop. Hie 告诉我它应该是ST s (),所以我写loop :: ST s ()了上面loop的定义。

但是 ghc 抱怨它无法将sfromloopsfrom匹配f。由于loop没有参数,它 …

haskell signature

1
推荐指数
1
解决办法
57
查看次数

标签 统计

haskell ×3

polymorphism ×2

integer ×1

signature ×1

types ×1