小编Bak*_*riu的帖子

'try'可以决定程序何时停止

我有这个功能:

isUndefined :: () -> Bool    
isUndefined x = case unsafePerformIO $ (try (return $! x) :: IO (Either SomeException ())) of
                    Left _ -> True
                    Right _ -> False
Run Code Online (Sandbox Code Playgroud)

然后:

isUndefined () = False
isUndefined undefined = True
Run Code Online (Sandbox Code Playgroud)

解决停止问题.当然,这也可以扩展到其他类型.

我的问题:这怎么可能?难道Control.Exception.try真的破事吗?

haskell halting-problem

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

"func2(func1 arg)","(func2.func1)arg"或"func2 $ func1 arg"?

我想知道你会选择哪个选项?

putStrLn (show randomNum)
putStrLn $ show randomNum
(putStrLn . show) randomNum
Run Code Online (Sandbox Code Playgroud)

所有选项在语法上都是正确的.

  • 括号确保show首先执行并且putStrLn只获得一个参数.

  • $运营商负责展会功能和randomNum和执行showrandomNum,所以putStrLn只得到一个说法.

  • .操作者取putStrLn功能,show功能和randomNum与第一执行showrandomNum,然后putStrLn对结果.

但是在这个例子中,更像是类似于orkell还是更有意义?

haskell

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

将数字提高到小数(Data.Ratio)?

2^(2%1)GHCi中没有类型检查的表达式,错误消息是神秘的.为什么这不起作用,我需要改变什么?

我不能转换为另一种类型,我想要这样的表达式27^(1%3).

haskell types exponentiation ghci

2
推荐指数
2
解决办法
529
查看次数

重复输入列表中的每个连续元素比前一个元素多一个

我想在输入字符串中重复每个连续字符多于前一个字符,从第一个字符的单个匹配开始:

例如

rep "abcd" == "abbcccdddd"
Run Code Online (Sandbox Code Playgroud)

我做了这个代码,但这不起作用,String但为Int和产生正确的结果Char.

rep [] =[]
rep (x:xs) =[ (x:xs)!!y| y<-[0..(length xs)]  , _<- [1..y+1]]
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

string haskell list

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

理解`sequence_`

All About Monads解释说sequence_:

sequence_功能(注意下划线)具有相同的行为sequence,但不返回结果的列表.当只有monadic计算的副作用很重要时,它是有用的.

然后,看看TestSequence.hs:

import Control.Monad

f :: String -> IO ()
f x = print x

run :: [String] -> IO ()
run xs = sequence_ . map f $ xs
Run Code Online (Sandbox Code Playgroud)

我可以运行它:

?: run ["foo", "bar"]
"foo"
"bar"
Run Code Online (Sandbox Code Playgroud)

sequence_呼吁unsafePerformIOIO ()得到的结果,即()

并且,sequence_气馁?或者,它是否IO Monad仅仅用于"在世界末日"运行一系列IO行动?

monads haskell

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

使用Aeson/JSON自动派生自定义数据类型的实例

如果我有自定义数据类型用于使用Aeson解析JSON

data Response = Response
    { response :: [Body]
    } deriving (Show)

instance FromJSON Response where
    parseJSON (Object v) = Response <$> v .: "response"
    parseJSON _ = mzero

data Body = Body
    { body_id               :: Int
    , brandId               :: Int
    } deriving (Show)

instance FromJSON Body where
    parseJSON (Object v) = Body
        <$> v .: "id"
        <*> v .: "brandId"
    parseJSON _ = mzero

raw :: BS.ByteString
raw = "{\"response\":[{\"id\":5977,\"brandId\":87}]}"
Run Code Online (Sandbox Code Playgroud)

赠送:

?> decode raw :: Maybe Response
Just (Response …
Run Code Online (Sandbox Code Playgroud)

generics json haskell custom-data-type aeson

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

无法将预期类型"a"与实际类型"Double"匹配:

我通过从ghci检查它来声明类型声明但是加载模块会给出错误:

even-fibbanaci-sum.hs:7:12: error:
     Couldn't match expected type ‘a’ with actual type ‘Double’
     ‘a’ is a rigid type variable bound by
       the type signature for:
         getFib :: forall b a. (Integral b, Floating a) => b -> a
       at even-fibbanaci-sum.hs:4:12
     In the expression: ((phi ^ n) - (minusphi ^ n)) / sqrt 5
     In an equation for ‘getFib’:
         getFib n = ((phi ^ n) - (minusphi ^ n)) / sqrt 5    • Relevant bindings include
       getFib :: b -> a (bound …
Run Code Online (Sandbox Code Playgroud)

haskell

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

减少Python脚本中的RAM使用量

我写了一个快速的小程序来从教科文组织网站上删除书籍数据,其中包含有关书籍翻译的信息.代码正在按照我的意愿行事,但是当它处理大约20个国家时,它正在使用~6GB的RAM.由于我需要处理大约200个,这对我来说不起作用.

我不确定所有RAM的使用来自哪里,所以我不确定如何减少它.我假设它是包含所有书籍信息的字典,但我并不积极.我不确定我是否应该简单地让程序为每个国家运行一次,而不是处理它们中的很多?或者,如果有更好的方法吗?

这是我第一次写这样的东西,而且我是一个非常新手,自学成才的程序员,所以请指出代码中的任何重大缺陷,或者你提出的改进技巧可能与问题没有直接关系在眼前.

这是我的代码,提前感谢任何帮助.

from __future__ import print_function
import urllib2, os
from bs4 import BeautifulSoup, SoupStrainer

''' Set list of countries and their code for niceness in explaining what
is actually going on as the program runs. '''
countries = {"AFG":"Afghanistan","ALA":"Aland Islands","DZA":"Algeria"}

'''List of country codes since dictionaries aren't sorted in any
way, this makes processing easier to deal with if it fails at
some point, mid run.'''
country_code_list = ["AFG","ALA","DZA"]

base_url = "http://www.unesco.org/xtrans/bsresult.aspx?lg=0&c="
destination_directory = "/Users/robbie/Test/"
only_restable = SoupStrainer(class_="restable") …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

我如何在Python3.3中使用fork()

我如何在Python3.3中使用fork()这是我的代码:

输入:

#!/usr/bin/env python
import os

def Child_process():
    print("We are in Child_Process")
    print("My PID: %d"%os.getpid())
    print("Child_Process is exiting")

def Parent_process():
    print("-------Parent_process---------")
    wpid = os.fork()
    if wpid==0:
        print("wpid is 0 means We are in Child_process")
        print("Child :%d"%wpid)
        Child_process()
    else:
        print("Execute Parent_process")
        print("Parent_process %d"%wpid)
        Parent_process()

Parent_process()
Run Code Online (Sandbox Code Playgroud)

输出:

C:\Python33\python.exe C:/Users/Iem-Prog/Desktop/Py/Fork

Traceback (most recent call last):

  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 21, in <module>
-------Parent_process---------
    Parent_process()
  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 11, in Parent_process
    wpid = os.fork()

AttributeError: 'module' object has no attribute 'fork'
Run Code Online (Sandbox Code Playgroud)

python windows

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

从定义中编写函数?

首先,我想让大家都知道我对Haskell很新,所以为了增加知识等,我一直在尝试问题,而且我很困惑.我想我差不多了,但一些更有经验的建议将不胜感激.这是问题:

一个体育团队的名字和他们在上一场比赛中得分的数量就像这样("Newcastle",[3,3,3,0]).此数据由类型定义建模:

type TName = String
type Points = [Int]
type Team = (TName,Points)
Run Code Online (Sandbox Code Playgroud)

从这里我必须定义以下函数,如果他们的积分总和更大,则命令一个团队高于另一个团队:

sortPoints :: [Team] -> [Team]
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的:

sortPoints :: [Team] -> [Team]
sortPoints [_,()] -> []
sortPoints [_,(x:xs)] = sum[x|x<-xs]
Run Code Online (Sandbox Code Playgroud)

一旦我到达这里,我不太确定如何添加检查点总和的条件,任何指针都会非常感激,因为我仍然会接受很多Haskell功能.

haskell types function definition

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