Jak*_*ger 1 io haskell types module c-strings
因此,我深入了解了一些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
Run Code Online (Sandbox Code Playgroud)
这很好用.现在我尝试将其导出到C#应用程序,这样我就可以创建一个漂亮的界面.我遇到了一些麻烦.我补充道
module Shiftlib where
import Foreign.C.String
import Foreign.C.Types
Run Code Online (Sandbox Code Playgroud)
到顶部.在导入下,我添加了一个块来将输入和输出转换为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
Run Code Online (Sandbox Code Playgroud)
现在它不编译.似乎"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.
Run Code Online (Sandbox Code Playgroud)
我似乎无法解决它.如何从我的小模块返回CString?
newCString :: String -> IO String
result :: String
newCString result :: IO String
return :: a -> IO a
return $ newCString result :: IO (IO String)
Run Code Online (Sandbox Code Playgroud)
当你已经拥有你想要的东西时,简单地停下来newCString result.