小编Lan*_*ton的帖子

Org-mode中代码和逐字的区别?

有人知道组织模式~code~=verbatim=标记之间的预期差异吗?在两种情况下导出为HTML都会产生<code>标签.提前致谢!

emacs org-mode

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

过滤仅包含文件的路径列表

如果我有一个列表FilePaths,如何过滤它们只返回常规文件(即不是符号链接或目录)?

例如,使用 getDirectoryContents

main = do
    contents <- getDirectoryContents "/foo/bar"
    let onlyFiles = filterFunction contents in
        print onlyFiles
Run Code Online (Sandbox Code Playgroud)

其中"filterFunction"是一个只返回FilePaths表示文件的函数.

答案可能仅适用于Linux,但首选跨平台支持.

[编辑]只是使用didDirectoryExist不能按预期工作.此脚本打印目录中所有内容的列表,而不仅仅是文件:

module Main where

import System.Directory
import Control.Monad (filterM, liftM)

getFiles :: FilePath -> IO [FilePath]
getFiles root = do
    contents <- getDirectoryContents root
    filesHere <- filterM (liftM not . doesDirectoryExist) contents
    subdirs <- filterM doesDirectoryExist contents
    return filesHere

main = do
    files <- getFiles "/"
    print $ files
Run Code Online (Sandbox Code Playgroud)

此外,变量子目录只包含".""..".

io haskell file path

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

使用long where语句编码风格不好?

我是Haskell的新手,所以我对编码风格了解不多.我有一个链接很多随机生成器的函数.这种代码是否被认为是错误的风格,其中我在where声明后有~10行?如果是这样,有哪些替代方案?

#!/usr/bin/env runhaskell
{-# LANGUAGE UnicodeSyntax #-}
module Main where

makeDummy :: RandomGen g ? [String] ? FilePath ? g ? (FilePath, g)
makeDummy words root gen0 = (fullPath, gen7)
  where
      (numWordsInTitle, gen1) = randomR (1 :: Int, 4 :: Int) gen0 -- unused
      (title, gen2) = randomChoice words gen1
      (year, gen3) = randomR (1800 :: Int, 2100 :: Int) gen2
      (resNum, gen4) = randomChoice ["1080", "720", "480"] gen3
      (resLetter, gen5) = randomChoice ["P", "p", "i", "I"] gen4 …
Run Code Online (Sandbox Code Playgroud)

random haskell coding-style where

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

如何在Idris中调用子流程?

Idris标准库(或第三方库)中是否有一些模块可以允许用户使用Shell扩展到另一个程序?我在考虑像Python subprocess和Haskell 这样的模块System.Process

理想情况下,我想以编程方式与该流程进行交互(写入其stdin,从其stdout读取等)。

functional-programming process idris

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

Python - 由变量定义的精确参数数量

我正在创建一个方法来构造一个匿名方法来返回多个变量的函数,例如f(x,y,z)= b.我希望用户能够传递变量列表:

def get_multivar_lambda(expression, variables=["x"])
Run Code Online (Sandbox Code Playgroud)

然后我希望返回的匿名函数采用精确的len(variables)参数(基于列表索引的位置或基于列表中字符串的关键字).我知道我可以使用*args和检查长度,但这似乎不优雅.

这可能吗?我怎么能这样做?

这是我如何为一个变量(其中seval是一个来自模块simple_eval)的例子:

def get_lambda(expression, variable="x"):                                       
    return lambda arg: seval(expression.replace(variable, str(arg))) 
Run Code Online (Sandbox Code Playgroud)

这是我如何通过检查arguments*传递的长度来做到的:

def get_multivar_lambda(expression, variables=["x"]):

    def to_return(*arguments):
        if len(variables) != len(arguments):
            raise Exception("Number of arguments != number of variables")
        for v, a in zip(variables, arguments):
            expression.replace(v, a)
        return seval(expression)

    return to_return
Run Code Online (Sandbox Code Playgroud)

编辑:我从用户输入中获取表达式和变量,因此一种安全的方法是最好的.

python lambda anonymous-function variadic-functions

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

如何在Coq中将"+ 1"(加一)重写为"S"(succ)?

我有以下引理证明不完整:

Lemma (s_is_plus_one : forall n:nat, S n = n + 1).
Proof.
  intros.
  reflexivity.
Qed.
Run Code Online (Sandbox Code Playgroud)

这个证明失败了

Unable to unify "n + 1" with "S n".
Run Code Online (Sandbox Code Playgroud)

似乎eq_S是证明这一点的方法,但我无法应用它(它不承认n + 1S n:) Error: Unable to find an instance for the variable y..我也试过ring,但找不到关系.当我使用rewrite它时,它只会降低到相同的最终目标.

我怎样才能完成这个证明?

coq successor-arithmetics coq-tactic

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

Http只能获得httpbin.org

当运行以下程序elm-reactor(或使用elm-make该问题)时,我发现它实际上是唯一的网页GEThttpbin.否则,我会看到Http.NetworkError,即使在可靠的网站上,例如" http://google.com "或" http://stackoverflow.com ".关于为什么会这样,我完全糊涂了,有人能指出我的错误吗?

module Main (..) where

import Http
import Html exposing (..)
import Effects
import Html.Events as Events
import StartApp
import Task


-- MODEL


type alias Model =
  { output : String }


type Action
  = Response String
  | Request String
  | HTTPError Http.Error



-- UPDATE


update : Action -> Model -> ( Model, Effects.Effects Action )
update act mod =
  case act of
    Response str -> …
Run Code Online (Sandbox Code Playgroud)

http task elm

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

Coq let子句中的多个分配

我是Coq的新手,只是想弄清楚基本语法。如何添加多个子句let?这是我要编写的函数:

Definition split {A:Set} (lst:list A) :=
  let
    fst := take (length lst / 2) lst
    snd := drop (length lst / 2) lst
  in (fst, snd) end.
Run Code Online (Sandbox Code Playgroud)

这是错误:

语法错误:在[constr:operconstr级别200]之后(在[constr:binder_constr]中)应为'in'。

我猜想它in的定义是fst

syntax-error let coq

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

在Go中获取PHP配置变量

我想得到命令的输出,php -r 'echo get_cfg_var("some_var");'以检查预定义的值.目前,我有以下代码:

variableName := "default_mimetype"
cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'")
out, err := cmd.CombinedOutput()
Run Code Online (Sandbox Code Playgroud)

跑完之后

err.Error() 回报 "exit status 254"

out"PHP Parse error: syntax error, unexpected end of file in Command line code on line 1"

导致此错误的原因是什么?我没有正确地逃避一些事情吗?

php go

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

IO Buol类型的反转返回值

我有一个返回类型的函数IO Bool.我想使用这个函数作为参数filterM,但我真正想做的是反转它的输出.我已经尝试了一些效果(not . f),但not并不热衷于IO氛围.我该如何反转IO Bool

这是一个最小的工作示例:

#!/usr/bin/env runhaskell
{-# LANGUAGE UnicodeSyntax #-}
module Main where
import Prelude.Unicode

userEnteredStr ? String ? IO Bool
userEnteredStr str = do
    input ? getLine
    return (input ? str)

-- doesn't work. How would I write this function? 
--userDidntEnterStr ? String ? IO Bool
--userDidntEnterStr str = not . userEnteredStr

main = do result ? userEnteredStr "y"
          print result
Run Code Online (Sandbox Code Playgroud)

对不起,如果这是基本的!我在Hoogle上找不到类型的功能,但IO Bool -> …

monads haskell boolean functor

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