简体   繁体   中英

A filter that contain a list in the beginning in OCaml

I want to create a function that return the next number in binary form. For example :

000 -> 001 -> 010 -> 011 -> 100 -> 101 -> 110 -> 111. My function is :

let rec next_number = function 
             | [] -> []
             | (a,b)::[] -> if b = 0 then (a,1)::[] else (a,0)::[]
             | l::(a,value1)::(b,value2)::[] -> if (value2 = 0) then l::(a,value1)::(b,1)::[]
                                                else
                                                    begin
                                                         if value1 = 0 then l::(a,1)::(b,0)::[]
                                                         else (next_number l)::(a,0)::(b,0)::[]
                                                    end
             ;;

When I compile I got the error : This expression (l in l::(a,value1)::(b,value2)::[]) has type 'a * int but an expression was expected of type ('a * int) list

How to tell OCaml that the filter is a list then a couple then couple then an empty list not 3 couples and then empty list.

Thanks for your help.

There's no pattern that matches a prefix of a list. If you imagine a value as a tree, a pattern always matches a full subtree starting at some point. The prefix of a list doesn't have this form. When you pick out the full subtree you get the whole list.

If your algorithm needs to work from the back of the list toward the front, you may want to reverse the list before starting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM