我现在只在Haskell工作了两天,并想知道下面两个函数定义之间的区别是:
Prelude> let swap (x1:x2:xs) = x2:x1:xs
Prelude> swap [1..5]
[2,1,3,4,5]
Prelude> let swap' (x1:x2:xs) = [x2] ++ [x1] ++ xs
Prelude> swap' [1..5]
[2,1,3,4,5]
Run Code Online (Sandbox Code Playgroud)
也就是说,是什么让x2:x1:xs与[x2] ++ [x1] ++ xs不同?拜托,谢谢.
在这一个人的头上划了一天.
我的代码中有一些函数看起来像这样:
function :: IO (Maybe Whatever)
function = do
monadFun
yaySomeIO
status <- maybeItWillFail
if checkStatus status -- Did we succeed?
then monadTime >>= return . Just . processItPurely
else return Nothing
Run Code Online (Sandbox Code Playgroud)
ghci将以交互方式加载和运行它,没有任何问题,ghc将愉快地编译它.然而,通过cabal运行这个给了我:
myProgram.hs:94:16:
Unexpected semi-colons in conditional:
if checkStatus status; then monadTime >>= return . Just . processItPurely; else return Nothing
Perhaps you meant to use -XDoAndIfThenElse?
Run Code Online (Sandbox Code Playgroud)
无论这个-XDoAndIfThenElse选项是什么,我似乎无法在任何文档中的任何地方找到它的痕迹.为什么是cabal(或者就此而言,这是什么?)对我使用IT首先放置的分号大吼大叫?或者在if-then-else语句中使用monadic表达式只是一个坏主意?
请注意,cabal根本没有抱怨这个:
case checkStatus status of
True -> monadTime >>= return . Just . processItPurely
_ -> return Nothing …Run Code Online (Sandbox Code Playgroud) 所以,我写了一个简洁的小程序,可以分析日文文本,并向用户提供有关汉字使用的各种统计数据,我想向全世界发布这个程序.问题是,我不知道如何创建"发布".
我意识到*nix系统经常将可执行文件(或符号链接)放在像/ usr/bin这样的地方,并且构建脚本经常自动将它们放在那里,但事实证明我去了Haskell并写了这个东西.
然后只使用阴谋!
......我听你说.好吧,我会,除了我的程序有大量的数据文件,它读出来,当然程序需要知道这些文件的位置.通过使用cabal,可执行文件不会被抛到一些奇怪的项目文件路径a:
/usr/share/haskell/cabal/morecabal-1.0.4/myproject-1.3.4.1.a/thisisridiculous/
目前我从其源目录中运行可执行文件,它正在"./data"中查找数据文件.是否有典型的安装路径格式,所以我可以提前告诉我的程序在源查找数据的位置?
我最终的目标是使其成为Arch Linux软件包.任何人都可以帮助我开始吗?
对于好奇,git repo就在这里.在此先感谢您提供任何帮助.
每个人都很好(你当地时间).
我浏览了Real World Haskell关于外部函数接口的章节,并在此处进行了一些后续阅读.我现在正在尝试绑定到C函数,我想对一些事情做一些澄清.
以下是相当清楚的:
foreign import ccall unsafe "math.h sin" c_sin :: CDouble -> CDouble
Run Code Online (Sandbox Code Playgroud)
我可以加载这个和在ghci中使用它的代码,一切都很好.它甚至可以在emacs的Haskell模式中加载嵌入式ghci.我觉得这很适合测试.
math是一个系统库,所以这是直截了当的.
现在来自Real World Haskell的一个例子:
foreign import ccall unsafe "pcre.h pcre_compile" c_pcre_compile :: ...
Run Code Online (Sandbox Code Playgroud)
我故意遗漏了剩下的功能签名.现在,我无法在Haskell模式下加载它.我见过的所有例子都说必须这样做:
ghci -lpcre
Run Code Online (Sandbox Code Playgroud)
我做了什么,并立即确认正确加载的东西:
GHCi, version 7.6.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading object (dynamic) /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libpcre.so ... done
final link ... done
Run Code Online (Sandbox Code Playgroud)
然后我可以加载我的绑定代码并测试,但...... …
GHC 7.8中的OverloadedLists语言编译器非常有吸引力,所以我决定尝试一下:
{-# LANGUAGE OverloadedLists #-}
import Data.Set (Set)
import qualified Data.Set as Set
mySet :: Set Int
mySet = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
编译器给了我:
No instance for (GHC.Exts.IsList (Set Int))
arising from an overloaded list
In the expression: [1, 2, 3]
In an equation for ‘mySet’: mySet = [1, 2, 3]
No instance for (Num (GHC.Exts.Item (Set Int)))
arising from the literal ‘1’
In the expression: 1
In the expression: [1, 2, 3]
In an equation for ‘mySet’: mySet = [1, 2, …Run Code Online (Sandbox Code Playgroud) 所以我从for循环中堆叠的一些过滤器中获得了一些有趣的行为.我将从演示开始:
>>> x = range(100)
>>> x = filter(lambda n: n % 2 == 0, x)
>>> x = filter(lambda n: n % 3 == 0, x)
>>> list(x)
[0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96]
Run Code Online (Sandbox Code Playgroud)
在这里,我们得到预期的输出.我们在过滤器内的过滤器内有一个范围,过滤条件按我们希望的方式堆叠.现在我的问题来了.
我编写了一个函数来计算数字的相对素数.它看起来像这样:
def relative_primes(num):
'''Returns a list of relative primes, relative to the given number.'''
if num == 1:
return []
elif is_prime(num):
return list(range(1, num))
result = range(1, num)
for factor in prime_factors(num): …Run Code Online (Sandbox Code Playgroud) 我是一名正在研究Scala的Haskeller.我遇到的不是代码,而是导入/包.
我有两个文件,Test.scala和Lists.scala.
// Lists.scala
package problems
object Lists {
def last(list: List[Any]): Option[Any] = list match {
case Nil => None
case x :: Nil => Some(x)
case _ :: xs => last(xs)
}
}
Run Code Online (Sandbox Code Playgroud)
和:
// Test.scala
import problems._
object Test extends App {
println("Starting tests...")
println(last(List(1,2,3,4,5)))
}
Run Code Online (Sandbox Code Playgroud)
Test.scala不编译.运行scalac Test.scala Lists.scala收益率:
Test.scala:5: error: not found: value last
println(last(List(1,2,3,4,5))
Run Code Online (Sandbox Code Playgroud)
然而改写last为Lists.last使它成功.这不是打败了import problems._通配符吗?我注意到数学函数可以在没有先做math.的情况下编写import math._.为什么这对我的文件也不起作用?
真正的目标:我只是希望能够制作一个包,然后 …
我在互联网上发现的一切都告诉我,emacs默认禁用鼠标滚动,但是当我的拇指太靠近我的笔记本电脑上的触控板时,我非常热情并滚动到它的内容.我没有允许这样做,这是我的.emacs来证明它:
;; Line by line scrolling
(setq scroll-step 1)
;; Turn off backup file creation
;;(setq make-backup-files nil)
;; Enable backup files
(setq make-backup-files t)
;; Save all backup files to this directory
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))
;; Show column number
(column-number-mode 1)
;; Fix C indenting
(setq c-default-style "bsd"
c-basic-offset 8)
Run Code Online (Sandbox Code Playgroud)
每隔一段时间,我的拇指就会刷上履带板,突然间我会在另一条线的中间打字,或者从我刚刚到达的地方下方打字.我怎么能阻止这种情况发生?
编辑 运行Mx自定义选项鼠标滚轮模式产生:
鼠标滚轮模式:[隐藏值] [切换]关闭(无)
[国家]:在定制之外改变; 在这里进行操作可能不可靠.
如果启用了鼠标滚轮模式,则为非零.[Hide Rest]
参见Easy Customization 命令')
或调用函数`mouse-wheel-mode'.团体:[鼠标]mouse-wheel-mode' for a description of this minor mode.
Setting this variable …