小编med*_*ans的帖子

在Haskell中编程时如何纠正我的OOP倾向

在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)

oop haskell functional-programming

13
推荐指数
2
解决办法
670
查看次数

NixOS和ghc-mod - 找不到模块

我正在试验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中问题的确切复制.

emacs haskell nixos

7
推荐指数
1
解决办法
2054
查看次数

如何避免丑陋的代码在Haskell(LANGUAGE扩展)中解决这个问题?

我正在尝试编写一个模拟世界上几种生物的程序.基本上,这个词会在一个生物列表上发送一条消息,每个生物都会给出他的反应,从而改变世界.

我在以下骨架中简化了我想写的内容:

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 pragma

5
推荐指数
1
解决办法
290
查看次数

NixOS初学者:xixad和haskellmode在NixOS 14.04中

我正在尝试在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.

我把这些问题放在一起,因为我怀疑它们都是由于我对某些事情的根本不理解造成的,所以原因可能很常见.

xmonad nixos

5
推荐指数
1
解决办法
2830
查看次数

手动实例显示定义导致堆栈空间溢出

当我手动编写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)

stack haskell functional-programming overflow

3
推荐指数
1
解决办法
111
查看次数

混合 Threepenny-Gui 和 StateT

我有一个关于 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)

haskell state-monad monad-transformers threepenny-gui

2
推荐指数
1
解决办法
292
查看次数