This question already has answers here:
How to get normal value from IO action in Haskell
(2 answers)
A Haskell function of type: IO String-> String
(4 answers)
haskell, how to unwrap IO monad [duplicate]
(1 answer)
Closed 1 year ago.
I am developing in Haskell and I had to use System.Random.
Now I have a IO [Int] and I would like to parse it to get the Int out of it and then use it to perform an action on a list.
I did:
function :: IO [Int] -> [a] -> a
function (x:xs) list = (list !! x)
But I get an error on (x:xs):
Couldn't match expected type 'IO [Int]' with actual type '[a0]'.
How do I parse the IO [Int] to use the Int inside?
Related
This question already has an answer here:
Unary minus and floating point number in OCaml
(1 answer)
Closed 4 years ago.
I am trying write a function that takes as input a list of floating point numbers
and returns a list in which all negative numbers are removed from the input list and then all resulting positive numbers are negated.
let f (mylist: float list) : float list =
let f1 = List.filter (fun x -> x < 0.0) mylist in
List.map (fun x -> (-x*1.0)) f1
I have a type error:
Error: This expression has type float but an expression was expected of type int
Why is that?
Thank you.
I figured this out.
For the last time, to negate a floating-point number, the appropriate operator is:
(-.x)
List.map is of type
- : ('a -> 'b) -> 'a list -> 'b list = <fun>
It's easy for me to understand the following code:
List.map (fun x -> x+1) [1;2;3;4];;
which adds 1 to each element of the list so it returns the following list :
- : int list = [2;3;4;5]
Now this is in an exercise where I'm asked to indicate the type of this :
List.map (fun p -> p 7) [ (fun n m -> n + m) ];;
I don't understand at all what it means to be honest.
What does p 7 mean ?
Why is there a function in the list ?
The type is
- : (int -> int) list = [<fun>]
But I can't understand why.
What does it mean when fun is between brackets ?
Thank you.
What does p 7 mean?
It means the application of function p to argument 7 .... You might spend some time reading the wikipage on λ-calculus (at least to learn about functional abstraction)
Read also about currying.
Why is there a function in the list ?
In Ocaml, functions are values, so you can have list of functions. If it was not a list of functions, you'll get a typing error. If you think more, you can understand what kind of functions are allowed.
What does it mean when fun is between brackets ?
The toplevel is not able to print functional values (implemented as closures). It shows them as <fun>. For a simpler example, pass fun x -> x+1;; (then try also fun y -> y;;) to your REPL.
(the rest of the exercise is left to the reader)
I'm trying to implement flatten : 'a list list -> 'a list list in SML.
I thought this should be relatively straight forward with higher order functions. My implementation is
val flatten = List.reduce (op #) []
However I'm getting a bizarre error message: "append.sml:1.6-1.36 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)". Thus when I try to flatten an int list list I get a type error:
:> flatten [[1,2],[3]];
stdIn:2.1-2.20 Error: operator and operand don't agree [literal]
operator domain: ?.X1 list list
operand: int list list
in expression:
flatten ((1 :: 2 :: nil) :: (3 :: nil) :: nil)
As the error message hints, you ran into the value restriction -- see here for an explanation. The solution is very simple: just "eta-expand" your definition, i.e., make the parameter explicit instead of relying on partial application:
fun flatten xs = List.reduce op# [] xs
This question already has answers here:
What do Clojure symbols do when used as functions?
(1 answer)
Explain Clojure Symbols
(3 answers)
Closed 8 years ago.
I refer you to this short code:
('a 1)
;==> nil
('a 1 2)
;=>2
why can the symbol a be used as a function in this context?
It allows you to look up a map with the symbol as a key, in a nice syntax.
Below are equivalent:
('a my-hash-map)
(get my-hash-map 'a) ; returns value associated with 'a or nil if not present
For further convenience, you can also supply default value as optional second argument:
('a my-hash-map 42) ; returns 42 if my-hash-map does't have the key 'a
As to you example, ('a 1) returns nil because (get 1 'a) returns nil, and ('a 1 2) returned supplied default value, 2.
In OCaml, is there a way to refer to the cons operator by itself?
For example, I can use (+) and ( * ) as int -> int -> int functions, but I cannot use (::) as a 'a -> 'a list -> 'a list function, as the following example show:
# (+) 3 5;;
- : int = 8
# ( * ) 4 6;;
- : int = 24
# (::) 1 [2;3;4];;
Error: Syntax error: operator expected.
Is there a way to produce a result like (::) other than with fun x y -> x::y? And does anyone know why (::) wasn't implemented in OCaml?
Adding to the answer of #seanmcl,
Actually OCaml supports a prefix form of (::):
# (::)(1, []);;
- : int list = [1]
This is in the uncurried form, corresponding with the fact that all the OCaml variant constructors are not curried and cannot be partially applied. This is handled by a special parsing rule just for (::), which is why you got a rather strange error message Error: Syntax error: operator expected..
Update:
Upcoming OCaml 4.02 removes this parsing rule, therefore this is no longer available.
No. Cons (::) is a constructor, constructors can not be infix operators. The allowed infix symbols are here:
http://caml.inria.fr/pub/docs/manual-caml-light/node4.9.html
Some workarounds are (as you mention) the verbose
(fun x l -> x :: l)
and defining your own nontraditional infix cons
let (+:) x l = x :: l
As of Ocaml 4.03, you can now use cons (in the List module). That is, cons x xs is the same as x :: xs.
It's also possible to just define your own cons function:
let cons = fun a list -> a :: list