因此,如果我想构建一个n 0和1 1的循环列表,以下哪种方式更好/更便宜?还有更好/更便宜的方式吗?考虑到n是一个Integer并且可能很大(尽管实际上它不会超过2 ^ 32).
aZerosAndOnes :: Integer -> [Int]
aZerosAndOnes n
| n >= 0 = cycle (genericReplicate n 0 ++ [1])
| otherwise = []
Run Code Online (Sandbox Code Playgroud)
与
bZerosAndOnes :: Integer -> [Int]
bZerosAndOnes n
| n >= 0 = tail (cycle (1 : genericReplicate n 0))
| otherwise = []
Run Code Online (Sandbox Code Playgroud) 当我编写一个简单的脚本并将其传递给runhaskell时,它工作正常,但是当我添加一个shebang并尝试直接执行它时.脚本是这样的:
#!/usr/local/bin/runhaskell
import Data.List (intercalate)
main :: IO ()
main = putStrLn $ intercalate " " $ map show [1..10]
Run Code Online (Sandbox Code Playgroud)
如果我按预期尝试$ runhaskell count.hsbash打印1 2 3 4 5 6 7 8 9 10,但如果我尝试,./count.hs我会收到以下错误:
./count.hs: line 3: syntax error near unexpected token `('
./count.hs: line 3: `import Data.List (intercalate)'
Run Code Online (Sandbox Code Playgroud)
这个错误是源于bash还是runhaskell?我该如何解决?
对于将函数映射到列表中每个第n个元素的函数:
mapEvery :: Int -> (a -> a) -> [a] -> [a]
mapEvery n f = zipWith ($) (drop 1 . cycle . take n $ f : repeat id)
Run Code Online (Sandbox Code Playgroud)
是否可以foldr像普通一样实现这个map?
编辑:在标题中,将"文件夹"更改为"折叠".自动更正...
我怎样才能创建一个Observable,我可以直接将事件推送到,比如Bacon.js的Bus?
如果我的项目结构如下:
project/
src/
Foo.hs
Bar.hs
Run Code Online (Sandbox Code Playgroud)
使用文件Foo.hs:
module Foo where
foo :: String
foo = "foo"
Run Code Online (Sandbox Code Playgroud)
和Bar.hs:
module Bar where
import Foo
bar :: String
bar = foo ++ "bar"
Run Code Online (Sandbox Code Playgroud)
如果我的当前目录是src,并且我输入ghci并运行:l Bar.hs,我得到预期的输出:
[1 of 2] Compiling Foo ( Foo.hs, interpreted )
[2 of 2] Compiling Bar ( Bar.hs, interpreted )
Ok, modules loaded: Bar, Foo.
Run Code Online (Sandbox Code Playgroud)
但是,如果我移动到project目录(这是我宁愿留下并运行vim/ghci /等),并尝试:l src/Bar.hs,我得到:
src/Bar.hs:3:8:
Could not find module ‘Foo’
Use -v to see a list of the files …Run Code Online (Sandbox Code Playgroud) 在Haskell语法中,我们可以有一个(抽象)类型[a -> b],它是函数a到b的列表.具体类型是这样的[Int -> Int],例如map (*) [1..10].是否可以在类型中包含级联函数列表[a -> b, b -> c, c -> d, ...]?列表中的各个元素都是不同的(我认为)所以我认为这不可能.但是依赖类型是否可能?它的类型签名是什么(最好是伪Haskell语法)?
haskell ×5
bacon.js ×1
bash ×1
fold ×1
ghc-mod ×1
ghci ×1
javascript ×1
optimization ×1
rxjs ×1
scripting ×1
shell ×1
type-systems ×1
types ×1