Term to packet erlang. Constructing a binary - syntax

I have the following code:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/integer-unit:8 >>.
However, when I run:
term_to_packet("Hello").
I get an error:
exception error: bad argument in function term_to_packet line 20
where line 20 corresponds to the last line of the term_to_packet function.
I'm not quite sure what's throwing this error.

B is a binary, but in the binary construction on the last line you specify that it is an integer. This seems to work:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/binary-unit:8 >>.

Related

How to solve the error ' [not a vector ]'

I ran this code to find the norm of some fundamnetal units of a biqaudratic number field, but I faced following problem
for (q=5, 200, for(p=q+1, 200, if (isprime(p)==1 && isprime(q)==1 ,k1=bnfinit(y^2-2*p,1); k2=bnfinit(y^2-q,1); k3=bnfinit(y^2-2*p*q,1); ep1=k1[8][5][1]; ep2=k2[8][5][1]; ep3=k3[8][5][1]; normep1=nfeltnorm(k1,ep1); normep2=nfeltnorm(k2,ep2); normep3=nfeltnorm(k3,ep3); li=[[q,p], [normep1, normep2, normep3]]; lis4=concat(lis4,[li]))))
and it works for small p and q. However, when I ran that for p and q greater than 150, it gives the following error:
First, I didn't use the flag=1 for bnf, but after adding that, still I get the same error.
Please, do not use indexing like ...[8][5][1] to get the fundamental units (FU). It seems that bnfinit omits FU matrix for some p and q. Instead, use the member function fu to receive FU. Please, find the example below:
> [q, p] = [23, 109];
> k = bnfinit(y^2 - 2*p*q, 1);
> k[8][5]
[;]
> k[8][5][1] \\ you will get the error here trying to index the empty matrix.
...
incorrect type in _[_] OCcompo1 [not a vector] (t_MAT).
> k.fu
[Mod(-355285121749346859670064114879166870*y - 25157598731408198132266996072608016699, y^2 - 5014)]
> norm(k.fu[1])
1

Debugging <<loop>> error message in haskell

Hello i am encountering this error message in a Haskell program and i do not know where is the loop coming from.There are almost no IO methods so that i can hook myself to them and print the partial result in the terminal.
I start with a file , i read it and then there are only pure methods.How can i debug this ?
Is there a way to attach to methods or create a helper that can do the following:
Having a method method::a->b how can i somehow wrap it in a iomethod::(a->b)->IO (a->b) to be able to test in in GHCI (i want to insert some putStrLn-s etc ?
P.S My data suffer transformations IO a(->b->c->d->......)->IO x and i do not know how to debug the part that is in the parathesis (that is the code that contains the pure methods)
Types and typeclass definitions and implementations
data TCPFile=Rfile (Maybe Readme) | Dfile Samples | Empty
data Header=Header { ftype::Char}
newtype Samples=Samples{values::[Maybe Double]}deriving(Show)
data Readme=Readme{ maxClients::Int, minClients::Int,stepClients::Int,maxDelay::Int,minDelay::Int,stepDelay::Int}deriving(Show)
data FileData=FileData{ header::Header,rawContent::Text}
(>>?)::Maybe a->(a->Maybe b)->Maybe b
(Just t) >>? f=f t
Nothing >>? _=Nothing
class TextEncode a where
fromText::Text-> a
getHeader::TCPFile->Header
getHeader (Rfile _ ) = Header { ftype='r'}
getHeader (Dfile _ )= Header{ftype='d'}
getHeader _ = Header {ftype='e'}
instance Show TCPFile where
show (Rfile t)="Rfile " ++"{"++content++"}" where
content=case t of
Nothing->""
Just c -> show c
show (Dfile c)="Dfile " ++"{"++show c ++ "}"
instance TextEncode Samples where
fromText text=Samples (map (readMaybe.unpack) cols) where
cols=splitOn (pack ",") text
instance TextEncode Readme where
fromText txt =let len= length dat
dat= case len of
6 ->Prelude.take 6 .readData $ txt
_ ->[0,0,0,0,0,0] in
Readme{maxClients=Prelude.head dat,minClients=dat!!1,stepClients=dat!!2,maxDelay=dat!!3,minDelay=dat!!4,stepDelay=dat!!5} where
instance TextEncode TCPFile where
fromText = textToFile
Main
module Main where
import Data.Text(Text,pack,unpack)
import Data.Text.IO(readFile,writeFile)
import TCPFile(TCPFile)
main::IO()
main=do
dat<-readTcpFile "test.txt"
print dat
readTcpFile::FilePath->IO TCPFile
readTcpFile path =fromText <$> Data.Text.IO.readFile path
textToFile::Text->TCPFile
textToFile input=case readHeader input >>? (\h -> Just (FileData h input)) >>? makeFile of
Just r -> r
Nothing ->Empty
readHeader::Text->Maybe Header
readHeader txt=case Data.Text.head txt of
'r' ->Just (Header{ ftype='r'})
'd' ->Just (Header {ftype ='d'})
_ -> Nothing
makeFile::FileData->Maybe TCPFile
makeFile fd= case ftype.header $ fd of
'r'->Just (Rfile (Just (fromText . rawContent $ fd)))
'd'->Just (Dfile (fromText . rawContent $ fd))
_ ->Nothing
readData::Text->[Int]
readData =catMaybes . maybeValues where
maybeValues=mvalues.split.filterText "{}"
#all the methods under this line are used in the above method
mvalues::[Text]->[Maybe Int]
mvalues arr=map (\x->(readMaybe::String->Maybe Int).unpack $ x) arr
split::Text->[Text]
split =splitOn (pack ",")
filterText::[Char]->Text->Text
filterText chars tx=Data.Text.filter (\x -> not (x `elem` chars)) tx
I want first to clean the Text from given characters , in our case }{ then split it by ,.After the text is split by commas i want to parse them, and create either a Rfile which contains 6 integers , either a Dfile (datafile) which contains any given number of integers.
Input
I have a file with the following content: r,1.22,3.45,6.66,5.55,6.33,2.32} and i am running runghc main 2>err.hs
Expected Output : Rfile (Just (Readme 1.22 3.45 6.66 5.55 6.33 2.32))
In the TextEncode Readme instance, len and dat depend on each other:
instance TextEncode Readme where
fromText txt =let len= length dat
dat= case len of
To debug this kind of thing, other than staring at the code, one thing you can do is compile with -prof -fprof-auto -rtsopts, and run your program with the cmd line options +RTS -xc. This should print a trace when the <<loop>> exception is raised (or if the program loops instead, when you kill it (Ctrl+C)). See the GHC manual https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/runtime_control.html#rts-flag--xc
As Li-yao Xia said part of the problem is the infinite recursion, but if you tried the following code, then the problem still remains.
instance TextEncode Readme where
fromText txt =let len= length [1,2,3,4,5,6] --dat
dat= case len of
The second issue is that the file contains decimal numbers but all the conversion function are expecting Maybe Int, changing the definitions of the following functions should give the expected results, on the other hand probably the correct fix is that the file should have integers and not decimal numbers.
readData::Text->[Double]
--readData xs = [1,2,3,4,5,6,6]
readData =catMaybes . maybeValues where
maybeValues = mvalues . split . filterText "{}"
--all the methods under this line are used in the above method
mvalues::[Text]->[Maybe Double]
mvalues arr=map (\x->(readMaybe::String->Maybe Double).unpack $ x) arr
data Readme=Readme{ maxClients::Double, minClients::Double,stepClients::Double,maxDelay::Double,minDelay::Double,stepDelay::Double}deriving(Show)

Expected parenthesis errors gfortran

I am trying to compile some fortran code using gfortran v.4.8.2.
When I compile, I get the following errors:
ant_driver.f90:185.72:
if (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns)
1
Error: Syntax error in IF-expression at (1)
ant_driver.f90:187.119:
ry%thck(ew,ns) = -rhoo/rhoi * (model%geometry%topg(ew,ns)- model%climate%eus(ew,
1
Error: Expected a right parenthesis in expression at (1)
ant_driver.f90:188.27:
endif
1
Error: Expecting END DO statement at (1)
ant_driver.f90:248.82:
odel%geometry%usrf(ew,ns) = - (model%geometry%topg(ew,ns)-model%climate%eus(ew,
1
Error: Expected a right parenthesis in expression at (1)
ant_driver.f90:260.107:
model%geometry%usrf(ew,ns) - (model%geometry%topg(ew,ns)-model%climate%eus(ew,
1
Error: Expected a right parenthesis in expression at (1)
ant_driver.f90:174.30:
call glide_set_eus(model,eus)
1
Error: Rank mismatch in argument 'inarray' at (1) (scalar and rank-2)
The errors relate to the following lines of code:
185-188:
if (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns) < 0 .and. model%geometry%thck(ew,ns) == 0 .and. &
mask(ew,ns) == 1) then
model%geometry%thck(ew,ns) = -rhoo/rhoi * (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns))
endif
248:
model%geometry%usrf(ew,ns) = - (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns)) * rhoo/rhoi &
+ (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns))
260:
model%geometry%thck(ew,ns) = model%geometry%usrf(ew,ns) - (model%geometry%topg(ew,ns)-model%climate%eus(ew,ns))
174:
call glide_set_eus(model,eus)
Any help much appreciated. Thank you for your time.
Your errors are due to line truncation. For fixed-form Fortran a line length of 72 characters is specified and for free-form Fortran it should be 132 characters. You source should be interpreted as free-form due to the .f90 extension but you can force free-form in gfortran with -ffree-form. You can also alter the maximum line lengths in fixed- and free-form source with the options -ffixed-line-length-n and -ffree-line-length-n respectively, where n is the number of characters and values of 0 or none mean unlimited line length.

How to control lineno in sourcecode directive?

I want to write a document which includes a part of source code with line numbers.
Example(whitch I expected):
123. int myFunc() {
124. return 100;
125. }
When I use sourcecode/code-block directive, line number is always started from 1.
Currently, I tried following methods, but these are not worked.
Is there any way to control line numbers?
A: use 'linenostart' option.
.. sourcecode:: cpp
:linenos:
:linenostart: 123
int myFunc() {
return 100;
}
=> it was result in error "unknown option: lineno"
B: use literal include.
.. literalinclude:: my_func.cpp
:language: cpp
:linenos:
:lines: 123-125
=> it was result in no error, but line number is start from 1.
Thanks in advance.

Why does this VBS code fail with a "Type mismatch: 'CInt'" error?

I am experiencing difficulty with the following VBS code. It works only sometimes, and even then it fails quickly. Why?
Dim Butt
Set Butt = CreateObject("InternetExplorer.application")
Butt.visible = True
Butt2 = InputBox("Put the link to one hat you would like to snipe.", "Hat Selection")
Butt3 = InputBox("Enter the maximum amount of Robux you will spend on this hat.", "Maximum Payment")
Dim Proace
Set Proace = CreateObject("Microsoft.XMLHTTP")
Proace.Open "GET", "http://www.roblox.com", False
Proace.Send
Do
Do While Butt.Busy
WScript.sleep 200
Loop
St00f = CInt(Replace(Mid(St00f, (InStr(St00f, ">R$")+3), 8), "</b>", ""))
If St00f <= CInt(Butt3) Then
Butt.Navigate "javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(""ctl00$cphRoblox$TabbedInfo$UserSalesTab$lstItemsForResale$ctrl0$lnkBuyNow"",%20"""",%20true,%20"""",%20"""",%20false,%20true))"
Exit Do
End If
Loop
Do While Butt.Busy
WScript.sleep 200
Loop
MsgBox("Congratulations! Your snipe was successful! You sniped "&Butt2&" for "&Butt3&" Robux!")
Butt.Quit
Set Butt = Nothing
Set Proace = Nothing
WScript.Quit
Error:
Script: C:\Users\John\Downloads\SingleHatSniper.vbs
Line: 14
Char: 1
Error: Type mismatch: 'CInt'
Code: 800A000D
Source: Microsoft VBScript runtime error
Please help me, I'm not that great with VBS. That much is clear, my friend helped me write this.
As you might have known by now, this is where the error occurs
St00f = CInt(Replace(Mid(St00f, (InStr(St00f, ">R$")+3), 8), "</b>", ""))
And that line does these things
InStr that returns the numeric position of the first occurrence of ">R$"
Its then added with 3 to get the index after the string"R$"
Now Mid splits the string St00f with start index after "R$" to a length of 8
Then Replace takes the split string and replaces an occurrence of "</b>" with ""
At last CInt converts the string to an integer or more correctly * converts any number to the variant of subtype Integer*
And you are getting the error at the CInt conversion.
If I were at your place, I will split this line by line by keeping only one function per line and then try something like MsgBox for the output after each line and find what's wrong with that.
The key is the variable St00f and what that variable holds.
Happy Coding :)
The "Type mismatch" error indicates that your Replace(...) did not return a valid numerical string:
>> i = CInt("4711")
>>
>> i = CInt("999999999999")
>>
Error Number: 6
Error Description: Overflow
>> i = CInt("no number")
>>
Error Number: 13
Error Description: Type mismatch
>> i = CInt("")
>>
Error Number: 13
Error Description: Type mismatch
Consider using IsNumeric() before you apply CInt().
CInt can handle betweeen -32,768 and 32,767
Use CLng instead of CInt
Copied from Cint overflow error when value exceeds 100,000+

Resources