English 中文(简体)
用Haskell书写(Bool)
原标题:Writing [Bool] to binary file using Haskell
  • 时间:2011-10-11 10:57:44
  •  标签:
  • haskell

在Haskell开展工作,我正试图将一大批女bo写到双亲档案中。

我可以书写文字(字数为8字),但可以说明如何从8个Bool的名单中转换为8个语言。

这里我迄今为止所做的事情:

toByte :: [Bool] -> Word8
toByte list = toByteh list 0 0

toByteh :: [Bool] -> Int -> Word8 -> Word8
toByteh list 8 _ = 0
toByteh list i result 
    | head list == True = toByteh (tail list) (i + 1) (result .|. (2^i :: Word8))
    | otherwise = toByte_h (tail list) (i + 1) result

When I use this I just get a 0 byte. Can anyone see where this isn t working? Or are there better ways of doing this?

最佳回答

页: 1 我认为你打算退回<代码>result。 相反:

toByteh list 8 result = result
问题回答

写这种职务总是很明智,但如果你开始感觉到的话,你也可以这样做。

toByte = foldl (wordit -> 2*word + (if bit then 1 else 0)) 0 

(如果你愿意,名单的头部就算是最低的轨道,倒转)。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签