【发布时间】:2018-06-16 00:02:54
【问题描述】:
因此,我开始学习一些 Haskell(无聊的夜班),并编写了一个程序,该程序可以通过计算我在任何给定期间的班次来简化我的假期计划。
import System.Environment
import Data.List
import Data.List.Split
import Data.Time
import Data.Time.Calendar.WeekDate (toWeekDate)
-- merge xs and ys lists alternating value from each [ xs(0),ys(0),(xs(1),ys(1)..xs(n),ys(n) ]
merge :: [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = x : y : merge xs ys
-- get part of list from index 'start' to 'stop'
slice :: Int -> Int -> [a] -> [a]
slice start_from stop_at xs = fst $ splitAt (stop_at - start_from) (snd $ splitAt start_from xs)
timeFormat = "%d-%m-%Y"
timeFormatOut :: Day -> String
timeFormatOut = formatTime defaultTimeLocale "%d-%m-%Y"
-- parses Strings to DateTime Day1
parseMyDate :: String -> Day
parseMyDate = parseTimeOrError True defaultTimeLocale timeFormat
-- 8 week shift rotation
shiftRotation = cycle ["NAT","NAT","NAT","NAT","NAT","NAT","NAT","-","-","-","-","-","-","-","DAG","DAG","DAG","DAG","-","AFT","AFT","-","-","DAG","DAG","DAG","-","-","DAG","DAG","DAG","-","DAG","DAG","DAG","DAG","DAG","-","DAG","DAG","-","-","AFT","AFT","AFT","AFT","AFT","-","-","DAG(r)","DAG(r)","DAG(r)","DAG(r)","DAG(r)","(r)","(r)"]
hs_findshift :: String -> String -> String -> IO String
hs_findshift anchor start end = do
let dayZero = parseMyDate anchor
let startDate = parseMyDate start
let endDate = parseMyDate end
let startPos = fromIntegral (diffDays startDate dayZero)
let endPos = fromIntegral (diffDays endDate dayZero) + 1
let period = slice startPos endPos shiftRotation
let dates = map (timeFormatOut) [startDate..endDate]
let listStr = (concat(intersperse "," (merge dates period)))
putStrLn listStr
这很好用。现在我尝试将其导出到 C# 应用程序,这样我就可以制作一个漂亮的界面。我遇到了一些麻烦。我加了
module Shiftlib where
import Foreign.C.String
import Foreign.C.Types
到顶部。在导入的正下方,我添加了一个块来将输入和输出转换为 C 类型。
foreign export ccall findshift :: CString -> CString -> CString -> IO CString
findshift :: CString -> CString -> CString -> IO CString
findshift a s e = do
anchor <- peekCString a
start <- peekCString s
end <- peekCString e
result <- hs_findshift anchor start end
return $ newCString result
现在它无法编译。似乎“return $ newCString result”返回一个“外部”调用不会接受的 IO(IO CString)。
:l shiftlib
[1 of 1] Compiling Shiftlib ( shiftlib.hs, interpreted )
shiftlib.hs:53:1: error:
* Illegal foreign declaration: requires unregisterised, llvm (-fllvm) or native code generation (-fasm)
* When checking declaration:
foreign export ccall "findshift" findshift
:: CString -> CString -> CString -> IO CString
|
53 | foreign export ccall findshift :: CString -> CString -> CString -> IO CString
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
shiftlib.hs:61:5: error:
* Couldn't match type `IO CString' with `GHC.Ptr.Ptr CChar'
Expected type: IO CString
Actual type: IO (IO CString)
* In a stmt of a 'do' block: return $ newCString result
In the expression:
do anchor <- peekCString a
start <- peekCString s
end <- peekCString e
result <- hs_findshift anchor start end
....
In an equation for `findshift':
findshift a s e
= do anchor <- peekCString a
start <- peekCString s
end <- peekCString e
....
|
61 | return $ newCString result
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
我似乎无法绕过它。如何从我的小模块返回 CString?
【问题讨论】:
-
merge (x:xs) ys = x : merge ys xs更好。
标签: haskell types module io c-strings