我有一个基于NumPy的库,有一些类重载算术运算.由于大量的错误检查,内部有点毛茸茸,但我遇到了一个严重的问题,我是怎么做的.
该库背后的想法是使程序员使用最少的语法和努力使其非常容易和直观.因此,我希望很容易组合不同数据类型的数组,并简单地将较窄的数据类型转换为更广泛的情况.
例如,如果我有两个数组,一个使用dtype float64,另一个使用dtype complex128,当将它们添加到一起时,我想将其转换float64为complex128,但如果是,float64并且complex192我想转换为它.但是,如果它是一个组合float64和complex64,没有不失精确度两者之间的有效转换float64,所以我想都转换为complex128.
我立即看到了这个问题,即如果我希望我的库完全可靠,我必须寻找每种类型的组合并确定它们最窄的常见加宽类型(想想最不常见的多重类型).我不希望一切最广泛的类型可能转换,因为这很快成为记忆效率低下,而且我经常存储在内存中非常大的阵列.
有没有一种好方法可以确定两种NumPy类型之间最窄的常见加宽类型?
表示通过输入更新状态的最佳方法是什么?
我模拟物理系统.它有状态(坐标,速度).状态由模拟更新,该模拟从中获取一些参数(力)stdin.结果stdout在每个模拟循环后进行.
程序应在N个循环后停止.
我已经完成了readIORef,writeIORef但这很难看.
我正在尝试创建一个短函数来从整数列表中删除奇数元素,我的代码是:
removeOdds :: [Int] -> [Int]
removeOdds lst = [ x | x <- lst, x `mod` 2]
Run Code Online (Sandbox Code Playgroud)
编译返回 -
Type error in boolean qualifier
*** Term : x `mod` 2
*** Type : Int
*** Does not match : Bool
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢!
我对Haskell中的不同类型有一些问题,我该如何解决这个问题?
无法将预期类型Integer与实际类型匹配Int -> t0 -> t0
谢谢
isPrime :: Int -> Bool
isPrime number
| (number == 1) || (number == 2) = True
| even number = False
| otherwise = checkDiv number (fromInteger (`sqrt` number))
checkDiv :: Int -> Int -> Bool
checkDiv number divisor
| number == 2 = True
| (floor number `mod` divisor) == 0 = False
| otherwise = checkDiv number $ divisor - 1
Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,并尝试在讲义中遵循脚本.我创建了一个名为lecture.hs的文件
root (a, b, c) = ((-b -r)/e, (-b + r)/e))
where d = b*b - 4*a*c
r = sqrt d
e = 2*a
Run Code Online (Sandbox Code Playgroud)
当我加载这个文件来拥抱它显示
ERROR "lecture.hs":3 - Syntax error in input (unexpected `=')
Run Code Online (Sandbox Code Playgroud)
当我把它加载到ghci它显示
[1 of 1] Compiling Main ( lecture.hs, interpreted )
lecture.hs:3:14: parse error on input `='
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
请帮帮我.谢谢!
我是Haskell的新手,我想写一个简单的函数来计算字符串中子字符串的出现次数.
例如:"There is an apple"我想计算"is"句子中的次数,在这种情况下结果应该是1.
这就是我尝试过的:
countOf :: String -> Int
countOf x = length [n | n <- words x, filter "is" x]
Run Code Online (Sandbox Code Playgroud)
根据我所研究的它应该有效,但事实并非如此.我真的不知道如何解决问题,也不知道我得到的错误信息是什么意思:
input:1:41:
Couldn't match expected type `Bool' with actual type `[a0]'
In the return type of a call of `filter'
In the expression: filter "a" x
In a stmt of a list comprehension: filter "a" x
Run Code Online (Sandbox Code Playgroud) 我读了一些关于monad的帖子和博客,也许,只是,没什么..但是没有真正得到它:/在给定的代码中,我必须实现"latestActivity"函数.在我看来它应该工作,但我不知道如何正确使用"只是".也许有人能够帮助我.
module LatestActivity where
{-
Write a function 'latestActivity' that finds that last time a specific user
has sent a message. It is given a user name and a list of messages. A message
consists of the time it was sent, the user name of the user who sent it and
the message content. If there is no message from the given user in the list
the function yields 'Nothing'. Otherwise it yields 'Just' the latest time stamp
of the …Run Code Online (Sandbox Code Playgroud) 我在Haskell中使用两个功能:
基本上我想在匹配给定字符串中的特定字符之前获取字符
这是我的代码:
before :: Char -> [Char] -> Char
before x str = trackelement x ' ' str
trackelement :: Char -> Char -> [Char] -> Char
trackelement x y (z:zs)
| x == z = y
| otherwise = trackelement x z (zs)
Run Code Online (Sandbox Code Playgroud)
我的问题是当我尝试: before 'l' "luis"
答案是:(' '当然,在'l'没有任何东西之前),我想成为''或 Nothing
我尝试通过trackelement x '' str而不是trackelement x ' ' str但我遇到此错误Syntax error on ''str
你能给我一些建议吗?
在PHPStorm中,当您在Javascript中选择变量名称时,它会突出显示具有浅色背景的该变量的所有其他事件.你如何改变这种背景的颜色?
我想有一个异构的名单String和[String],因此:
strs = ["h", ["x", "y"], "i", ["m", "n", "p"]]
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用自定义数据类型执行此操作:
data EitherOr t = StringS t | StringL [t]
eitherOrstrs :: [EitherOr String]
eitherOrstrs = [StringS "h", StringL ["x", "y"], StringS "i", StringL ["m", "n", "p"]]
Run Code Online (Sandbox Code Playgroud)
但我很好奇这是否可能没有任何样板,strs如上所述.
到目前为止,我已经尝试过:
{-# LANGUAGE ExistentialQuantification #-}
class Listable a where
toListForm :: [String]
instance Listable String where
toListForm s = [s]
instance Listable [String] where
toListForm = id
strs :: forall a. Listable a => [a]
strs …Run Code Online (Sandbox Code Playgroud)