小编Yof*_*ofe的帖子

如何将这个python翻译成Haskell?

我正在学习Haskell,作为练习,我正在尝试将代码后面的read_from函数转换为Haskell.取自Peter Norvig的Scheme翻译.有这么简单的方法吗?

def read(s):
    "Read a Scheme expression from a string."
    return read_from(tokenize(s))

parse = read

def tokenize(s):
    "Convert a string into a list of tokens."
    return s.replace('(',' ( ').replace(')',' ) ').split()

def read_from(tokens):
    "Read an expression from a sequence of tokens."
    if len(tokens) == 0:
        raise SyntaxError('unexpected EOF while reading')
    token = tokens.pop(0)
    if '(' == token:
        L = []
        while tokens[0] != ')':
            L.append(read_from(tokens))
        tokens.pop(0) # pop off ')'
        return L
    elif ')' == token:
        raise SyntaxError('unexpected )') …
Run Code Online (Sandbox Code Playgroud)

haskell

11
推荐指数
2
解决办法
4466
查看次数

如何优化此haskell功能

我需要在调色板中找到最接近ps给定颜色的颜色p.如何在nearestColor不改变Pixel8或类型的情况下尽可能快地完成 功能PixelRGB8.到目前为止,我已尝试内联.

import qualified Data.Vector as V

type Pixel8 = Word8    

data PixelRGB8 = PixelRGB8 {-# UNPACK #-} !Pixel8 -- Red
                           {-# UNPACK #-} !Pixel8 -- Green
                           {-# UNPACK #-} !Pixel8 -- Blue
           deriving (Eq, Ord, Show)

nearestColor :: PixelRGB8 -> Vector PixelRGB8 -> PixelRGB8
nearestColor p ps = snd $ V.minimumBy comp ds
  where
    ds = V.map (\px -> (dist2Px px p, px)) ps
    comp a b = fst …
Run Code Online (Sandbox Code Playgroud)

optimization haskell

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

标签 统计

haskell ×2

optimization ×1