例如,我有两个列表
A = [6, 7, 8, 9, 10, 11, 12]
subset_of_A = [6, 9, 12]; # the subset of A
the result should be [7, 8, 10, 11]; the remaining elements
Run Code Online (Sandbox Code Playgroud)
在python中是否有内置函数来执行此操作?
假设我有以下数据模型,用于跟踪棒球运动员,球队和教练的统计数据:
data BBTeam = BBTeam { teamname :: String,
manager :: Coach,
players :: [BBPlayer] }
deriving (Show)
data Coach = Coach { coachname :: String,
favcussword :: String,
diet :: Diet }
deriving (Show)
data Diet = Diet { dietname :: String,
steaks :: Integer,
eggs :: Integer }
deriving (Show)
data BBPlayer = BBPlayer { playername :: String,
hits :: Integer,
era :: Double }
deriving (Show)
Run Code Online (Sandbox Code Playgroud)
现在让我们说经常是牛排狂热分子的经理想要吃更多的牛排 - 所以我们需要能够增加经理人饮食中的牛排含量.以下是此功能的两种可能实现:
1)这使用了大量的模式匹配,我必须得到所有构造函数的所有参数排序正确...两次.它似乎不会很好地扩展或者非常易于维护/读取.
addManagerSteak :: BBTeam -> BBTeam
addManagerSteak (BBTeam …Run Code Online (Sandbox Code Playgroud) 假设我想完全重新安装GHC/HP.我想(尽可能多的迷信)删除以前的安装中的任何东西.我实际需要删除什么(以及在哪里)?
编辑:我在OSX上,但是如果这些信息一般适用于所有系统,我会更好奇.
编辑2:到目前为止,我们有:
OSX:
/Library/Frameworks/GHC.framework/
〜/ .cabal/
在/ usr/bin中/ -符号链接
我将添加(基于此处定义的"前缀":http://www.vex.net/~trebla/haskell/sicp.xhtml#storage):
前缀/ lib/
前缀/共享/
前缀/ bin/
前缀/ share/doc/
/ usr(/ local)/ lib/[ghc-version]
/ usr(/ local)/ share/doc/ghc/html/libraries/ - documentation
/ usr(/ local)/ share/doc/ghc/
/ usr(/ local)/ bin
/ var/lib/[ghc-version]
/ etc/[ghc-version]
〜/ .ghc /
编辑3:
OS X:
〜/ Library/Haskell
Linux:
??
Windows:
??
Haskeline使得获取文件名标签完成功能非常容易:
module Main where
import System.Console.Haskeline
import System.Environment
mySettings :: Settings IO
mySettings = defaultSettings {historyFile = Just "myhist"}
main :: IO ()
main = do
args <- getArgs
let inputFunc = getInputLine
runInputT mySettings $ withInterrupt $ loop inputFunc 0
where
loop inputFunc n = do
minput <- handleInterrupt (return (Just "Caught interrupted"))
$ inputFunc (show n ++ ":")
case minput of
Nothing -> return ()
Just s -> do
outputStrLn ("line " ++ show n ++ ":" ++ …Run Code Online (Sandbox Code Playgroud) 我如何"杀死"一个耗时太长的纯计算?我试过了
import System.Timeout
fact 0 = 1
fact n = n * (fact $ n - 1)
main = do maybeNum <- timeout (10 ^ 7) $ (return . fact) 99999999
print maybeNum
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.替换(return . fact) 99999999为"真正的"IO功能getLine,这可以按预期工作.
是否有简单的函数来舍入a Double或Float指定的位数?我在这里搜索过Hoogle(for (Fractional a) => Int -> a -> a),但没有找到任何东西.