I have a problem in CLEAN, how can I make lowercase all letter in a string? I can do it through an char array, but i need to do it with a string too.
I have the code below so far:
module Something
import StdEnv, StdLib
arrayLower:: [Char] -> [Char]
arrayLower[x:xs] = (map toLower [x:xs])
stringLower:: String -> String
stringLower_ = ""
stringLowers = toString (arrayLower s)
Start:: String
Start = stringLower"SSSsss"
Your first case
stringLower _ = ""
means that stringLower applied to anything is the empty string.
I'm surprised that you didn't get a warning for the redundant second case.
A String is an array (unboxed, so it's a {#Char}), and you say that you already know how to do this with arrays, but your arrayLower is defined for lists of Char ([Char]), not arrays.
This, using an array comprehension, works for me:
stringLower :: String -> String
stringLower s = {toLower c \\ c <-: s}
Related
I would like to collect user input (a list of number separated by a space) split it in to an array and transform the data in it from string to float.
Basically i want to recreate this python code in Julia:
userlist = input('[+]type in a list of number separated by a space: ').split()
for i in range(len(userlist)): userlist[i] = float(userlist[i])
i tried this but didn't work:
print("type in a list of number separated by a space: ")
userinput = readline()
userlist = rsplit(userinput, " ")
for i in 0:length(userlist)
userlist[i] = userlist[i]::Float64
end
You're close. You parse a String into Float64 with parse, not a type assertion.
userlist[i] = parse(Float64, userlist[i])
This still won't quite work, since userlist is an array of strings and can't store floats (arrays in Julia are stored with their type by default, for efficiency). You could make a new array and then do the for loop like you have been, but you can also just use map.
userlist = map(x -> parse(Float64, x), userlist)
I would recommend using DelimitedFiles as it is usually more robust (you always end up with user inputting some wrong data etc.:
readdlm(IOBuffer(readline()))
For an example:
julia> readdlm(IOBuffer(readline()))
1 2 3
1×3 Matrix{Float64}:
1.0 2.0 3.0
Hi newbie here and I am trying to master recursive functions in Erlang. This function looks like it should work, but I cannot understand why it does not. I am trying to create a function that will take N and a string and will print out to stdout the string the number of times.
My code:
-module(print_out_n_times).
-export([print_it/2).
print_it(0, _) ->
"";
print_it(N, string) ->
io:fwrite(string),
print_it(N - 1, string).
The error I get is:
** exception error: no function clause matching print_it(5, "hello')
How can I make this work ?
Variables in Erlang start with a capital letter. string is an atom, not a variable named "string". When you define a function print_it(N, string), it can be called with any value for the first argument and only the atom string as the second. Your code should work if you replace string with String:
print_it(N, String) ->
io:fwrite(String),
print_it(N - 1, String).
I am trying to convert the following variable:
- final "in1.txt";
val it = [|[#"S",#".",#".",#"."],[#".",#".",#".",#"."],[#"W",#".",#"X",#"W"],
[#".",#".",#"X",#"E"]|] : char list array
from 'char list array' to 'char array array' in SMLNJ. The only reason I want to do this is because I need to be able to randomly iterate through this data, to perform a Dijkstra-like algorithm for a school project (if there 's a more efficient way to make this data iteratable, I am all ears). Is there a way to do this? The function that reads the input file and returns the above is this (I found it in Stack Overflow):
fun linelist file =
let
open Char
open String
open List
val instr = TextIO.openIn file
val str = TextIO.inputAll instr
in
tokens isSpace str
before
TextIO.closeIn instr
end
fun final file =
let
fun getsudo file = map explode (linelist file)
in
Array.fromList (getsudo file)
end
and the input files that need to be processed are like the one that follows:
S...
....
W.XW
..XE
You might want to try a different way to read this (space delivery) map (to help Lakis -- yes I am a classmate of yours).
fun parse file =
let
fun next_String input = (TextIO.inputAll input)
val stream = TextIO.openIn file
val a = next_String stream
val lista = explode(a)
in
lista
end
Parse is a function that gets all the contents from a text file and saves them in string a. Then, the function explode (function of String Signature of the SML NJ) creates a list, called lista. The elements of the list are the characters of the string a in the same order.
Then, you can create another function that saves the contents of the list to an array. Each row of the array will contain the characters of the list until #"\n" comes up.
I need to pass two list as command line arguments in ocaml.
I used the following code to access it in the program.
let list1=Sys.argv.(1);;
let list2=Sys.argv.(2);;
I need to have the list1 and list2 as list of integers.
I am getting the error
This expression has type string but an expression was expected of type
int list
while processing.
How can I convert that arguments to a list of integers.
The arguments are passed in this format [1;2;3;4] [1;5;6;7]
Sys.argv.(n) will always be a string. You need to parse the string into a list of integers. You could try something like this:
$ ocaml
OCaml version 4.01.0
# #load "str.cma";;
# List.map int_of_string (Str.split (Str.regexp "[^0-9]+") "[1;5;6;7]");;
- : int list = [1; 5; 6; 7]
Of course this doesn't check the input for correct form. It just pulls out sequences of digits by brute force. To do better you need to do some real lexical analysis and simple parsing.
(Maybe this is obvious, but you could also test your function in the toplevel (the OCaml read-eval-print loop). The toplevel will handle the work of making a list from what you type in.)
As Sys.argv is a string array, you need to write your own transcription function.
I guess the simplest way to do this is to use the Genlex module provided by the standard library.
let lexer = Genlex.make_lexer ["["; ";"; "]"; ]
let list_of_string s =
let open Genlex in
let open Stream in
let stream = lexer (of_string s) in
let fail () = failwith "Malformed string" in
let rec aux acc =
match next stream with
| Int i ->
( match next stream with
| Kwd ";" -> aux (i::acc)
| Kwd "]" -> i::acc
| _ -> fail () )
| Kwd "]" -> acc
| _ -> fail ()
in
try
match next stream with
| Kwd "[" -> List.rev (aux [])
| _ -> fail ()
with Stream.Failure -> fail ()
let list1 = list_of_string Sys.argv.(1)
let list2 = list_of_string Sys.argv.(2)
Depending on the OCaml flavor you want to use, some other library may look more interesting. If you like yacc, Menhir may solve your problem in a few lines of code.
I'm trying to read text from a file in SML. Eventually, I want a list of individual words; however, I'm struggling at how to convert between a TextIO.elem to a string. For example, if I write the following code it returns a TextIO.elem but I don't know how to convert it to a string so that I can concat it with another string
TextIO.input1 inStream
TextIO.elem is just a synonym for char, so you can use the str function to convert it to a string. But as I replied to elsewhere, I suggest using TextIO.inputAll to get a string right away.
Here is a function that takes an instream and delivers all (remaining) words in it:
val words = String.tokens Char.isSpace o TextIO.inputAll
The type of this function is TextIO.instream -> string list.