haskell. words into binary numbers
edited everything
I need to convert words into binary numbers. With a bit help of yours I got this
import Data.Char
blCo :: String -> String
blCo xs = concatMap lett2num) xs
lett2num :: Char -> Int
lett2num y
| (ord 'A' <= ord y) ord 'z') = (ord y - ord 'a') +1
num2bin :: Int -> String
num2bin n
| n >= 0 = ctive number"
where n2b 0 = []
n2b n = n `mod` 2 : n2b (n `div` 2)
GHC tells me a mistake.I dont undertsand it mistake:
Couldn't match expected type `Char' against inferred type `String'
In the first argument of `(:)', namely `num2bin (lett2num开发者_StackOverflow中文版 x)'
In the expression: num2bin (lett2num x) : blCo xs
In the definition of `blCo':
blCo (x : xs) = num2bin (lett2num x) : blCo xs
There are quite a few type errors here, from just a cursory glance.
You've declared blCo
as String -> Integer
. So it takes a string, and returns an integer. But the String argument, x
, is passed into lett2num
which is supposed to take an character - this isn't going to work, and that's what the error message means. Perhaps you meant to declare the line as
blCo [x] = num2bin(lett2num x)
so that x
would match the character value in a one-character string? In any case, that's what's causing the compiler error - you're passing a String
into a function that expects a char
.
Additionally, blCo
is supposed to return an Integer
, but its return value overall is going to be the output of num2bin
(with some argument) - and this function returns a String
, so this can never work either. Without seeing what lett2bin
does (and/or what your approach is), I can't say what's wrong - but it seems that something, somewhere doesn't add up.
So what's it to be - are you trying to output a String (the binary representation of a number-string passed in)? Or are you trying to output an integer of some kind?
1) Please write clearer questions including the compiler error, if there is one.
2) Please include all your code, when possible. This code is missing lett2num.
3) Some issues:
You need to import Data.Char
to get the ord
and chr
functions:
import Data.Char
Your guard on blCo is nonsensical, remove it. Also, that second definition will never run because the first one always matches, that is in blCo x
the x
will always match the input. So you need to rewrite blCo.
EDIT: There's still a bit wrong here. I suggest you go on IRC and talk it out in real time so you get more understanding in addition to correct code.
1) The typing for starters: you call a function (num2bin
) that is supposed to return a String
and concatenate it on a list using :
which would result in :: [String]
) but want to end up with only a String
. I'm guessing you wanted to use blCo xs = concatMap (num2bin . lett2num) xs
.
2) blCo
is recursive on the list but doesn't define what to do when the list hits []
. One option is to redefine it to use concatMap
, making it non-recursive.
3) You missed part of my point on importing Data.Char
. The point is to have the full code on SO so people can copy/paste your code to help you more easily, not just that you needed it on you private copy.
精彩评论