在Haskell中编程时,我遇到了这个经常出现的问题.在某些时候,我尝试模拟OOP方法.在这里,我为我发现的flash游戏编写了一些AI,我想将各个部分和级别描述为一个部分列表.
module Main where
type Dimension = (Int, Int)
type Position = (Int, Int)
data Orientation = OrienLeft | OrienRight
data Pipe = Vertical | Horizontal | UpLeft | UpRight | DownLeft | DownRight
data Tank = Tank Dimension Orientation
data Bowl = Bowl Dimension
data Cross = Cross
data Source = Source Dimension
-- desired
-- data Piece = Pipe | Tank | Bowl | Cross | Source
-- So that I can put them in a list, and …
Run Code Online (Sandbox Code Playgroud) 我正在试验emacs中的ghc-mod插件和NixOS 14.04之间的交互问题.基本上,一旦通过安装包nix-env -i
,它们可以从ghc和ghci中看到,由haskell-mode识别,但ghc-mod找不到.
为避免信息重复,您可以在错误故障单https://github.com/kazu-yamamoto/ghc-mod/issues/269中找到所有详细信息以及VM中问题的确切复制.
我正在尝试编写一个模拟世界上几种生物的程序.基本上,这个词会在一个生物列表上发送一条消息,每个生物都会给出他的反应,从而改变世界.
我在以下骨架中简化了我想写的内容:
module Structure0 where
type Message = String
class Creature a where
processInput :: a -> Message -> Message
class World a where
processAction :: a -> b -> Message -> a
getCreatures :: a -> [b]
---- USAGE EXAMPLE ----
data Parrot = Parrot Int deriving Show
instance Creature Parrot where
processInput p s = s
data ParrotWorld = ParrotWorld [Parrot]
instance World ParrotWorld where
processAction w p s = w
getCreatures (ParrotWorld ps) = ps
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我认为World类定义中的参数b可以假设属于Creature类的所有数据值,如:
processAction …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在haskell中设置一个用于代码开发的NixOS VM,并且在基本安装xmonad和emacs时遇到了麻烦.我的相关部分/etc/nixos/configuration.nix
是
environment.systemPackages = with pkgs; [
emacs
emacs24Packages.haskellMode
xlibs.xmessage
haskellPackages.haskellPlatform.ghc
haskellPackages.xmobar
haskellPackages.xmonad
haskellPackages.xmonadContrib
haskellPackages.xmonadExtras
];
Run Code Online (Sandbox Code Playgroud)
xmonad:当我尝试编译代码时,xmonad抱怨它无法找到模块XMonad.Util.EZConfig.xmonad.hs
使用ghc 编译是可以的,我也可以将模块加载到ghci中.在#nixos频道上,我被告知使用函数ghcWithPackages,但我无法解决问题.此外,我想首先理解为什么会出现这个问题,因为在我看来这是一个非常简单的用例.xmonad.hs
我遇到的最小问题是:
import XMonad
import XMonad.Util.EZConfig
main = xmonad $ defaultConfig
{ modMask = mod4Mask
, terminal = "konsole"
}
`additionalKeysP`
[ ("M-e", spawn "emacs")
, ("M-f", spawn "firefox")
]
Run Code Online (Sandbox Code Playgroud)emacs:在安装包haskellmode之后(查看上面的configuration.nix),我无法在emacs中输入haskell-mode.
我把这些问题放在一起,因为我怀疑它们都是由于我对某些事情的根本不理解造成的,所以原因可能很常见.
当我手动编写PhisicalCell数据类型的简单show实例时,程序会占用所有空间.在派生他自己的Show版本时,这不会发生.为什么?
这是我正在编写的代码的精简版本:
import Data.Array
type Dimensions = (Int, Int)
type Position = (Int, Int)
data PipeType = Vertical | Horizontal | UpLeft | UpRight | DownLeft | DownRight deriving (Show)
data PhisicalCell = AirCell
| PipeCell PipeType
| DeathCell
| RecipientCell Object
-- deriving (Show) SEE THE PROBLEM BELOW
data Object = Pipe { pipeType :: PipeType -- tipo di tubo
, position :: Position -- posizione del tubo
, movable :: Bool -- se posso muoverlo
}
| Bowl …
Run Code Online (Sandbox Code Playgroud) 我有一个关于 Threepenny-Gui 与 StateT 交互的问题。考虑这个玩具程序,每次单击按钮时,都会在列表中添加一个“Hi”项:
import Control.Monad
import Control.Monad.State
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core hiding (get)
main :: IO ()
main = startGUI defaultConfig setup
setup :: Window -> UI ()
setup w = void $ do
return w # set title "Ciao"
buttonAndList <- mkButtonAndList
getBody w #+ map element buttonAndList
mkButtonAndList :: UI [Element]
mkButtonAndList = do
myButton <- UI.button # set text "Click me!"
myList <- UI.ul
on UI.click myButton $ \_ -> element myList …
Run Code Online (Sandbox Code Playgroud)