这可能是这篇文章的重复.但它得到的唯一答案对我来说似乎不起作用,所以我在这里发布自己的案例,希望找到一个具体的解决方案.我在Linux Ubuntu Trusty上.
我有一个专有的C库libMyLib.so,位于/ usr/local/lib(包含在LD_LIBRARY_PATH中).
我用uskell的方式写了这个C库的haskell绑定.Main.hs:
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
import Foreign.C.Types
foreign import ccall safe "mycfunction" c_myCfunction :: CInt -> IO (CInt)
-- etc...
main = do -- ...
Run Code Online (Sandbox Code Playgroud)
我可以用ghci来测试这个程序,将它传递给库和它可以找到它的目录:ghci /usr/local/lib/ -lMyLib它工作得很好.
现在,独立地,我正在尝试开始使用堆栈,并按照本指南,我能够构建几个非平凡的程序示例.
但是,我无法堆叠构建上面的FFI Main.hs程序,因为它抱怨丢失的C库:
第一次尝试 :
me@user:~/myProject$ stack build
myProject-0.1.0.0: configure
Configuring myProject-0.1.0.0...
myProject-0.1.0.0: build
Preprocessing library myProject-0.1.0.0...
In-place registering myProject-0.1.0.0...
Preprocessing executable 'myProject-exe' for myProject-0.1.0.0...
Linking .stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/myProject-exe/myProject-exe ...
.stack-work/dist/x86_64-linux/Cabal-1.22.5.0/build/myProject-exe/myProject-exe-tmp/MyModule.o: In function `c8QI_info':
(.text+0x102b): undefined reference to …Run Code Online (Sandbox Code Playgroud) 我在Windows 7 64位上.
我的程序需要从外部源检索一些文本(Utf8编码),用它做一些事情,然后将其保存到磁盘.原始文本使用"\ r \n"序列来表示换行符(我很高兴保持这种方式).
问题:当使用Data.Text.writeFile时,每个"\ r \n"序列似乎被翻译为"\ r \n \n \n",即每个'\n'被翻译为"\ r \n",甚至当它在原始文本中已经以'\ _'开头时.据我所知,在Windows操作系统上写入文件时,'\n'应转换为"\ r \n",如果前面没有'\ r',则将"\ r \n"转换为"\ r \n \n \n"似乎不正确.
使用ByteString.writeLine应用于textUtf8版本的文本工作得很好(没有额外的"\ r"插入"\ r \n"序列)
一个简单的例子:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString as B
import qualified Data.Text as T
import qualified Data.Text.IO as T (writeFile)
import qualified Data.Text.Encoding as T (encodeUtf8)
str = "Line 1 is here\r\nLine 2 is here\r\nLine 3 is here" :: T.Text
main = do
B.writeFile "byt.bin" $ T.encodeUtf8 …Run Code Online (Sandbox Code Playgroud) 我正在使用时间序列,type TSeries = [(Day, Double)]但需要将第一个Day元素转换为Double以进行进一步处理(例如绘图等).
将日期范围映射到相应的双范围[lobound,upbound],其中最早的日期映射到lobound,最新的日期映射到upbound,是一个基本的转换.为了实现它,我首先需要获得日期范围的最小值和最大值.我遇到了性能问题,但我不确定为什么以及如何解决它.
这是代码(不假设时间序列是排序的):
module Main where
import Data.Time (Day, fromGregorian, diffDays)
type TSeries = [(Day, Double)]
-- time-series to (Double, Double) mapping function
toDbl :: (Day -> Double) -> TSeries -> [(Double, Double)]
toDbl mapX ts = map (\(d,x) -> (mapX d, x)) ts
-- Day to Double mapping function - fast
mapDays1 :: (Day, Double) -> (Day, Double) -> Day -> Double
mapDays1 (d0,x0) (d1,x1) d = ((fromIntegral $ diffDays d …Run Code Online (Sandbox Code Playgroud) 我定义了一个简单的代数(具体)数据类型,MyType:
data MyTpe = MyBool Bool | MyInt Int
Run Code Online (Sandbox Code Playgroud)
...我试图找到一种方法将"转换"任意函数(a-> b),其中a和b是Bool或Int,进入相关的(MyType-> MyType)函数.
这样做,它将(a-> b)转换为Maybe(MyType-> MyType)(参见下面的[1]):
import Data.Typeable
data MyTpe = MyBool Bool | MyInt Int deriving Show
liftMyType :: (Typeable a, Typeable b) => (a -> b) -> Maybe (MyTpe -> MyTpe)
liftMyType f = case castIntInt f of
Just g -> Just $ liftIntInt g
Nothing ->
case castIntBool f of
Just g -> Just $ liftIntBool g
Nothing ->
case castBoolInt f of
Just g -> …Run Code Online (Sandbox Code Playgroud) 我需要将x11 包与lts-8.2 解析器一起使用,当我运行命令时stack build,我得到以下信息:
X11-1.8: configure
Progress: 1/2
-- While building package X11-1.8 using:
/tmp/stack7885/X11-1.8/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0 /setup/setup --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.2.0 configure --with-ghc=/home/userXYZ/.stack/programs/x86_64-linux/ghc-8.0.2/bin/ghc --with-ghc-pkg=/home/userXYZ/.stack/programs/x86_64-linux/ghc-8.0.2/bin/ghc-pkg --user --package-db=clear --package-db=global --package-db=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/pkgdb --libdir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/lib --bindir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/bin --datadir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/share --libexecdir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/libexec --sysconfdir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/etc --docdir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/doc/X11-1.8 --htmldir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/doc/X11-1.8 --haddockdir=/home/userXYZ/.stack/snapshots/x86_64-linux/lts-8.2/8.0.2/doc/X11-1.8 --dependency=base=base-4.9.1.0 --dependency=data-default=data-default-0.7.1.1-7EGYJX2YWqWJRbPFUHNoCr
Process exited with code: ExitFailure 1
Logs have been written to: /home/userXYZ/Documents/programming/haskell/projects/MyStack/screen-linux/.stack-work/logs/X11-1.8.log
[1 of 1] Compiling Main ( /tmp/stack7885/X11-1.8/Setup.hs, /tmp/stack7885/X11-1.8/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup/Main.o )
/tmp/stack7885/X11-1.8/Setup.hs:6:29: warning: [-Wdeprecations]
In the use of ‘defaultUserHooks’
(imported from Distribution.Simple):
Deprecated: "Use simpleUserHooks or autoconfUserHooks, unless you need …Run Code Online (Sandbox Code Playgroud) 我需要编写一个函数,它接受一个对象(obj类型)和一个键(也是一个obj类型),如果对象恰好是一个 Map,Map<'k,'v>那么它是 any,然后提取它的键和值。
困难在于我无法使用泛型类型对函数进行参数化,并且我们无法对泛型类型的对象进行模式匹配。
我不熟悉 F# 反射,但是一旦我知道它的键,我就找到了一种获取 Map 值的方法。使用此示例代码:
module TestItem =
open System
open Microsoft.FSharp.Reflection
// some uninteresting types for this example, could be anything arbitrary
type Foo = {argF1 : string; argF2 : double; argF3 : bool[]}
type Bar = {argB1 : string; argB2 : double; argB3 : Foo[]}
// and their instances
let foo1 = {argF1 = "foo1"; argF2 = 1.0; argF3 = [| true |]} …Run Code Online (Sandbox Code Playgroud)