How do you convert a character to a real in SML? - sml

If I'm trying to do this:
Convert #ā€Nā€ to a real
What ML expression would do this?
In the same way that:\
str = Char -> string
chr = int -> Char
ord = Char -> int
real = int -> real
Is there an ML expression like the ones stated above that can convert char -> real?
The input would be something like:
real #"N";
and the output would be:
val it = "whatever the value is": real

No you would need to use two functions:
real(ord(#"N"));
Breakdown:
ord(#"N"); ...... converts to ascii value, 78.
real(78); .......... converts int to real, 78.0.

Related

How to split string to characters in Fortran?

I am working on ciphering program, where ull put text and it will convert it into coded message. I stick and point where I dont know how to split len into sepparate characters. Is it even possible?
Yes, you can split a string into a character array. If chararray = "abcdefg" then you can have an array like ca(1) = chararray(1:1), ca(2) = chararray(2:2) ...
ca(7:7) = chararray(7)
This can also be done in a do-loop using the iachar and achar functions.
program test
character*7 :: stringin
integer :: single_character
stringin = 'message'
do i = 1, len(stringin), 1
single_character = iachar(stringin(i:i))
write(*,*) achar(single_character)
end do
end program
This creates an integer representation of the letters with iachar and converts them back with achar. This may be helpful if using a mathematical expression to do the encoding.
More information can be found here

Combining string modification and concatenation in haskell

So my problem is to take a string in haskell and to modify it so that if there are certain characters, they are changed to other characters, and I have created a helper function to do this, however there is one case where if the character is '!' then it become '!!!111oneone', so i figure to do this you would need to concatenate the current string with '!!111oneone', the trouble is that my function was working with chars however to do this we would need to work with the string, how would you combine this, ie a helper to modify the chars if necessary and implementing the conversion if there is a '!'.
Here is what i have so far
convert :: String -> String
convert [] = []
convert (x:xs) =
| x == '!' = !helper
| otherwise = converthelper x
Assuming your helper is something like
helper :: Char -> String
helper '!' = "!!!111oneone"
helper c = [c]
then you can use concatMap to map helper over each character in your string, and then concatenate the results into a single string.
convert :: String -> String
convert = concatMap helper
-- convert msg = concatMap helper msg
The trick is that your helper promotes every character to a list of characters; most characters just become the corresponding one-character string, but ! becomes something more.
(Note that concatMap forms the basis of the Monad instance for lists. You could also write convert msg = msg >>= helper.)

How to split String to int and bytestring literal

I have the following string:
str = "3, b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'"
I need to split it in order to get two variables, an int and a bytestring like so:
number = 3
bytestring = b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'
What I tried doing:
number, bytestring = [s for s in str.split(", ")]
int_number = int(number)
bytestring_in_bytes = bytestring.encode()
This unfortunately didn't work for the bytestring and I ended up with something like this:
bytesring_in_bytes = b"b'\\xf3\\xc71\\xe9\\xad_\\xce\\x8bI\\x1c\\x04Y\\xd5z\\xa2Q'"
Any idea how to get the bytestring from the string?
What you here have seems like the textual representation (in Python the repr(..)) of a bytestring.
You can use ast.literal_eval(..) to convert this to a bytestring:
from ast import literal_eval
bytestring_in_bytes = literal_eval(bytestring)
Note that in case it contains a string, int, etc., then the type of bytestring_in_bytes will be a str, int, etc. as well.

Haskell output a list of ascii value

For example I have a haskell list [72,73,74,75], how can i output this list as a string?, all elements in the list are ascii value.
You can combine map, that applies a function to each element of a list, and the chr function, that convert an Int value to its Char equivalent:
> map chr [72,73,74,75]
"HIJK"
You can convert an Int code point to a Char using chr :: Int -> Char; a String is just a list of Chars. Note that this'll work for any Unicode code point, not just ASCII, which is something you should be doing anyway.
You can find functions like this using Hoogle; just type something like Int -> Char, and it'll give you functions that match that type.
You can use 'chr' from the module Char to convert the integer values to characters:
import Char
intListToString l = [ chr x | x <- l ]
main = do
putStrLn $ "the string: " ++ (intListToString [72,73,74,75])
Running the above with 'runghci' gives:
the string: HIJK
Do you want this list as a straight string or a list with commas? Unless you want to convert ASCII char values to their character counterparts (which was already covered), you can do the following:
concatMap show [72,73,74,75]
will give you a "72737475" string and
init $ tail $ show [72,73,74,75]
will give you a "72,73,74,75" string

Pattern matching from a [String] in Haskell

I'm following an introductory course on functional programming, where we use Haskell.
Part of an excercise is to write a parser for the input string.
However I can't solve the following error, or get what is actually happening.
Parser.hs:29:71:
Couldn't match expected type `String' with actual type `Char'
In the first argument of `readPoint', namely `start'
In the expression: readPoint start
In the expression:
(readLines track, readPoint start, readLine finish)
The error originates from this line:
readTrack str = parseTrack (lines str) where
parseTrack (start : finish : track) = (readLines track, readPoint start, readLine finish)
What I expected to happen is that the input string got split into a list of lines, which get passed to parseTrack.
parseTrack would then use pattern matching to name the top two strings(lines) from the list and the rest.
However what I believe is happening is that finish is the top element from the list, and start gets assigned the top char from that string.
I would really like to know how to solve this problem and what is actually happening.
Thanks a lot!
Parser.hs
module Parser where
import Types
readFloat :: String -> Float
readFloat str = case reads str of
[] -> error "not a floating point number"
(p,_):_ -> p
readInt :: String -> Int
readInt str = case reads str of
[] -> error "not an integer"
(p,_):_ -> p
readPoint :: String -> Point
readPoint str = parsePoint (words str) where
parsePoint (x : y : _) = (readInt x, readInt y)
readLine :: String -> Line
readLine str = parseLine (words str) where
parseLine (x1 : y1 : x2 : y2 : _) = ((readInt x1, readInt y1), (readInt x2, readInt y2))
readLines :: String -> [Line]
readLines str = parseLines (lines str) where
parseLines (line : rest) = readLine line : parseLines rest
readTrack :: String -> Track
readTrack str = parseTrack (lines str) where
parseTrack (start : finish : track) = (readLines track, readPoint start, readLine finish)
Types.hs
module Types where
type Vector2D = (Int, Int)
type Point = Vector2D
type Line = (Point, Point)
type Velocity = Vector2D
type CarState = (Position, Velocity)
type Position = Vector2D
type Trace = [Position]
type Track = ([Line], Point, Line)
Your variable track was actually a list of single lines, not a string with '\n's in it. Since you've already split it into lines, you can just map readLine over it, giving:
readTrack str = parseTrack (lines str) where
parseTrack (start:finish:tracks)
= (map readLine tracks, readPoint start, readLine finish)
Here tracks :: [String], which is why you can map readLine on them all - you don't need to use readLines to split it into lines first. (You can tell it's a list because it's the final thing to the right hand side of a :.)
You say
However what I believe is happening is that finish is the top element from the list, and start gets assigned the top char from that string.
Well what was happening was: because you asked for readLines track as the first output, Haskell started there, and since you declared
readLines :: String -> [Line]
that meant that track had to be a String - that's the only thing readLines can deal with.
First, you need to remember that : has an element on its left and a list on its right, so in
3:4:stuff
stuff has to be [Integer] because it's on the right of some Integer elements. Similarly,
c:"a string"
means c has to be a Char because String = [Char].
In your code, we've worked out that track is a String, so that means that when you write
(start : finish : track)
both start and finish have to be elements you can put at the front of a String, so both start and finish have to be Char.
Haskell then looks at your code readPoint start, but because it's worked out that start has type Char, but
readPoint :: String -> Point
it complains that Char and String don't match.
I think you made the mistake because you forgot that readLines takes a single string, but it felt (from the name) like it should happily take a list of strings. Your parseLines looks like it does a similar thing, but it takes a list of strings, so copes, whereas readLines takes a single string with newline characters, so can't cope with a list.
UPD. Oh, sorry, I didn't get that track means multiple tracks and has to be of type [String]. So the answer from AndrewC fits more.
Since in haskell the pattern (x:xs) means that if x has type a then xs has to be of type [a] your pattern in parseTrack means in types smth like (a : a : [a]).
Compiler wanna evaluate type a and first what it see on the right is readLines track. Func readLines has type String -> [Line] so compiler has track as String which means [a] is of type String. Also in haskell String is [Char] so a is Char.
But you need a as String. So you just need to take first three strings and throw out the rest tail of [String]. In types it'll mean smth like (String : String : String : [String]). For it you can rewrite matching pattern in parseTrack to:
parseTrack (start : finish : track : _)